Home > Back-end >  Please teach brothers and sisters
Please teach brothers and sisters

Time:05-10

Simulated shopping cart Java code, use numbered hasmap on behalf of the shopping cart to store items and purchase quantity, the purchase quantity how to save? Why I added quantity, run out of quantity is null? Commodity information how to write and show the shopping cart

CodePudding user response:

Don't take pictures all screenshots code

CodePudding user response:

references a zhe. 1/f, 900 response:
code stick all don't take pictures screenshots

Public class shangpin {
Int bianhao;
String name;
Int shuliang;
Double danjia;
Public shangpin (int bianhao, String name, int shuliang, double danjia) {
Super ();
Enclosing bianhao=bianhao;
this.name=name;
Enclosing shuliang=shuliang;
Enclosing danjia=danjia;
}
Public int getbianhao () {
Return bianhao;
}
Public void setBianhao (int bianhao) {
Enclosing bianhao=bianhao;
}
Public String getname () {
return name;
}
Public void elegantly-named setname (String name) {
this.name=name;
}
Public int getShuliang () {
Return shuliang;
}
Public void setShuliang (int shuliang) {
Enclosing shuliang=shuliang;
}
Public double getDanjia () {
Return danjia;
}
Public void setDanjia (double danjia) {
Enclosing danjia=danjia;
}

}

Package practise;
Import the Java. Nio. Channels. The Pipe;
Import the Java. Security. PrivateKey;
import java.util.*;
import java.util.Scanner;
Import the Java. Util. Concurrent. ConcurrentHashMap. KeySetView;
Public class gouwuche {

Public static void main (String [] args) {
Int bh=0; Int sl; Int t=0;
System. The out. Println (" * * * * * * * * * * the shopping cart * * * * * * * * * * * ");
System. The out. Println (" 1, add to cart ");
System. The out. Println (" 2, modify the purchase amount ");
System. The out. Println (" 3, delete ");
System. The out. Println (" 4, exit ");
While (t!
=4){
System. The out. Println (" please enter the option 1 ~ 4 ");
Scanner sc=new Scanner (System. In);
T=sc. NextInt ();
The switch (t) {
Case 1:
{XSSP ();
System. The out. Print (" please enter the article number: ");
Scanner sc1=new Scanner (System. In);
Bh=sc1. NextInt ();
System. The out. Print (" please enter the number of goods: ");
Sl=sc1. NextInt ();
TJGWC (bh, sl);
XSGWC (bh);
} break;
Case 2:
{
XSGWC (bh);
System. The out. Print (" please enter a number: to modify ");
Scanner sc2=new Scanner (System. In);
Int j=sc2 nextInt ();
If (m> 0 & amp; & M<=bh) {
System. The out. Print (" please input the number: ");
Int n=sc. NextInt ();
XGSL (m, n);
XSGWC (bh);
}
The else System. Out. Print (" number error!" );
} break;
Case 3:
{
XSGWC (bh);
System. The out. Print (" please enter a number to be removed: ");
Scanner sc1=new Scanner (System. In);
Int j=sc1 nextInt ();
SCSP (m, bh);
XSGWC (bh);
}break;
Case 4: System. Out. Print (" quit "); break;
Default: System. Out. Print (" input error, please enter again: "); break;
}
}
}
Private static void XSSP () {
Shangpin p1=new shangpin (1, "toothpaste", 2,2.0);
Shangpin p2=new shangpin,4.0 (2, "toothbrush", 2);
Shangpin p3=new shangpin (3, "towel", 6,5.0);
Shangpin p4=new shangpin (4, "cup", 3,10.0);
ArrayList list=new ArrayList ();
List. The add (p1);
List. The add (p2);
List. The add (p3);
List. The add (p4);
System. The out. Print (" id: "+ p1. Bianhao +" name: "+ p1. Name +" : "+ p1. Shuliang +" is the unit price: "+ p1. Danjia +" \ n ");
System. The out. Print (" id: "+ p2. Bianhao +" name: "+ p2. The name +" : "+ p2. Shuliang +" is the unit price: "+ p2. Danjia +" \ n ");
System. The out. Print (" id: "+ p3. Bianhao +" name: "+ p3. Name +" : "+ p3. Shuliang +" is the unit price: "+ p3. Danjia +" \ n ");
System. The out. Print (" id: "+ p4. Bianhao +" name: "+ p4. Name +" : "+ p4. Shuliang +" is the unit price: "+ p4. Danjia +" \ n ");
}
Private static void XSGWC int (bh) {
ArrayList list=new ArrayList ();
Map map=new HashMap();

If (the map!=null)
System. The out. Println (" product id: "+ bh +" is number of goods: "+ map. Get (bh));
The else System. Out. Print (" shopping cart is empty!!!!! ");
}

Private static void TJGWC int bh, int (sl) {
Map The map=new HashMap ();
The map. The put (" bh ", sl);
}
Private static void XGSL (int m, int n) {
Map map=new HashMap();
The map. The put (m, n);

}
Private static void SCSP (int x, int bh) {
Map map=new HashMap();

for(int i=1; i<=bh; I++)
If (x==bh) {map. Remove (x); break; }
}
}
Sorry is a bit long
  • Related