这个一个简单的购物车代码,可以多次下单结算,但是我输入不同货物数量的时候,它总是读取最后一个我输入的数字。
class Supplie {
int id,qty;
String name;
double price;
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public Supplie(int id, String name,double price,int qty) {
//eliminate the confusion between class attributes and parameters with the same name of supplies
this.id=id;
this.name=name;
this.price=price;
this.qty=qty;
}
}
```java
static List<Supplie>supplieList = new ArrayList<>();//help adding messages of supplies
static List<Supplie>orderList = new ArrayList<>();//help adding ordered supplies
public static void main(String[] args) {
// TODO Auto-generated method stub
Supplies();
Scanner s = new Scanner(System.in);
int quantity=1;
while (true){
HomePage();
int num= s.nextInt();
//scan the next id, load into next page
switch(num) {
case 1:
while(true) {
ShowSupplieList();
int id= s.nextInt();
//scan the next id to order supplies
boolean a= id==0;
if(a) {
break;//press 0 back to home page
}
Supplie supplie= supplieList.get(id-1);
System.out.println("You have ordered: "+ supplie.name);
System.out.println("Entre quantity/quantities you want to order for Children:");
quantity=s.nextInt();
orderList.add(supplie);
System.out.println("You have ordered: "+ supplie.name+ "*"+quantity);
//Store data of supplies's message that user ordered
}
case 2:
ShowCart(quantity);
//show supplies's message that user ordered
break;
case 3:
Checkout();
//show the total price of supplies that user oedered
return;
}
}
}
//create and give value to list of supplies
static void Supplies() {
//ordered collection of supplies
Supplie supplie= new Supplie(1, "Food and snake packs",350.32,1);
supplieList.add(supplie);
Supplie supplie2= new Supplie(2, "summer cloth suit(Average size)", 150.00,1);
supplieList.add(supplie2);
supplieList.add(new Supplie(3,"winter cloth suit(Average size)", 250.21,1));
supplieList.add(new Supplie(4, "First aid kit",214.52,1));
supplieList.add(new Supplie(5, "notesbook",12.50,1));
}
//create HomePage
static void HomePage() {
System.out.println("Hi, welcome to Light of Children."+ '\n'+
"Help us to give needs for children."+ '\n'+
"----------------Home---------------"+'\n'+
"1. Order supplies for children"+ '\n'+
"2. Check Cart"+'\n'+
"3. Checkout"+'\n'+
"------press 0 back to HomePage-----");
}
//show supplies list
static void ShowSupplieList() {
System.out.println("----------List of supplies---------");
System.out.println("Enter no. to add to cart");
for(int i =0; i< supplieList.size();i++) {
Supplie supplie= supplieList.get(i);
System.out.println(supplie.id
+ "\t"+ supplie.name+
"\t"+ supplie.price+ "Rand"+"\t"+ supplie.qty);
}
System.out.println("------press 0 back to HomePage-----");
}
//show cart which the user ordered
static void ShowCart(int Nquantity) {
System.out.println("All the supplies you ordered for children:");
for(Supplie supplie:orderList ) {
System.out.println(supplie.id+ "\t"+ supplie.name+ "\t"+
supplie.price*Nquantity+ "\t"+supplie.qty+"*"+Nquantity);
}
}
//final check out calculation
static void Checkout() {
double total=0;
for(Supplie supplie: orderList)
total+=supplie.price;
System.out.println("The total price is: "+ total +"Rand."+"\n"+"Thank you for your support!");
char a ='-';
for(int i=0;i<=34;i++) {
//use char and for loop to shorter the printing
System.out.print(a);
}
}
```
搞不懂问题想表达啥,这让我很难办啊!阿sir