Question: public class Item { private String name; private double price; private int bulkQuantity; private double bulkprice; private int id; public Item(String name, double price, int
public class Item {
private String name; private double price; private int bulkQuantity; private double bulkprice; private int id;
public Item(String name, double price, int bulkQuantity, int id) { this.name = name; this.price = price; this.bulkQuantity = bulkQuantity; this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
public int getBulkQuantity() { return bulkQuantity; }
public void setBulkQuantity(int bulkQuantity) { if (bulkQuantity <= this.bulkQuantity) { this.bulkQuantity = bulkQuantity; } }
public double getBulkprice() { return bulkprice; }
public void setBulkprice(double bulkprice) { this.bulkprice = bulkprice; }
@Override public String toString() { return this.getName() + "\t BulkQuantity: " + this.bulkQuantity + "\tPrice: " + this.price;
}
public void decreaseBulkQuantity(int quantity) { this.bulkQuantity -= quantity; }
public void plusBulkQuantity(int quantity) { this.bulkQuantity += quantity; } }
///////////////////////////////////////////////////////////////////////////////////////////////
public class Itemorder {
private Item item; private int quantity;
public Itemorder(Item item, int quantity) { this.item = item; this.quantity += quantity; }
public int getQuantity() { return quantity; }
public double getprice() { return this.getItem().getPrice() * quantity; }
public Item getItem() { return item; }
public String show() { return this.getItem().getName() + "\tQuantity: " + this.quantity + "\tPrice" + this.getprice(); } }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class ShoppingCart {
public static void main(String[] args) { Scanner in = new Scanner(System.in); Item Bread = new Item("Bread", 1, 100, 1); Item Milk = new Item("Milk", 0.60, 50, 2); Item Bananas = new Item("Bananas", 0.40, 20, 3); Item[] listItem = {Bread, Milk, Bananas}; int[] listQuantity = {0, 0, 0}; Itemorder order1 = new Itemorder(listItem[0], listQuantity[0]); Itemorder order2 = new Itemorder(listItem[1], listQuantity[1]); Itemorder order3 = new Itemorder(listItem[2], listQuantity[2]); Itemorder[] listOrder = {order1, order2, order3}; ArrayList
while (loop) { System.out.println("Welcome to the market" + " ................................" + " what do you want to do? " + " 1.Buying Products In The Store" + " 2.Display Cart" + " 3.Finish"); System.out.print("Your choice: "); int chose = in.nextInt(); switch (chose) { case 1: System.out.println(" "); for (Item Items : listItem) { System.out.println((i) + ".Name: " + Items.toString()); i++; loop = false; } boolean loop2 = true; while (loop2) { System.out.println(" 1.Add to cary" + " 2.Remove From cart" + " 3.Back to back"); System.out.print("Your choice: "); loop2 = true; int shoce = in.nextInt(); switch (shoce) { case 1: System.out.print("Chose the product you whant to buy:"); int toBuyProduct = in.nextInt(); if (toBuyProduct == 1 || toBuyProduct == 2 || toBuyProduct == 3) { Item tem = listItem[toBuyProduct - 1]; System.out.print("Enter a quantity:"); int q = in.nextInt(); if (listItem[toBuyProduct - 1].getBulkQuantity() < q) { if (l == 3) { System.out.println("-----------------------BYE!----------------------- "); System.exit(0); } else { System.out.println("----------------------------Error in input----------------------------"); System.out.println(" Invalid c1`hoica"); System.out.println("Try again :( " + (l) + " )"); l++; loop2 = true; } } else { listQuantity[toBuyProduct - 1] += q; listItem[toBuyProduct - 1].decreaseBulkQuantity(listQuantity[toBuyProduct - 1]); market.add(listOrder[toBuyProduct - 1]); total += listOrder[toBuyProduct - 1].getprice(); System.out.println(" "); int w = 1; for (Item Items : listItem) { System.out.println(w + ".Name: " + Items.toString()); w++; } loop2 = true; } break; } else { if (m2 == 3) { System.out.println("-----------------------BYE!----------------------- "); System.exit(0); } else { m2++; System.out.println(" Invalid c1`hoica"); System.out.println("Try again :( " + (m2 - 1) + " )"); m2++; int ii = 0; System.out.println(" "); for (Item Items : listItem) { ii++; System.out.println(ii + ".Name: " + Items.toString()); i++; } loop2 = true; loop = false; } } case 2: boolean loop3 = true; while (loop3) { System.out.println(" 1.Delete all cart" + " 2.Delete part of the cart" + " 3.Back to back"); System.out.print("Your choice: "); System.out.println(" "); int sh = in.nextInt(); switch (sh) { case 1: System.out.print("Chose the product you whant to Remove: "); int choi = in.nextInt(); for (int q = 0; q < market.size(); q++) { if (market.get(i).getItem().getName() == listItem[choi - 1].getName()) { listItem[choi - 1].plusBulkQuantity(listQuantity[choi - 1]); listQuantity[choi - 1] = 0; market.remove(market.get(i)); } else { System.out.println("Not found"); } } break; case 2: System.out.print("Chose the product you whant to Remove: "); int choi2 = in.nextInt(); System.out.print("Enter a quantity: "); int qq = in.nextInt(); for (int q = 0; q < market.size(); q++) { if (market.get(i).getItem().getName() == listItem[choi2 - 1].getName()) { listItem[choi2 - 1].plusBulkQuantity(qq); listQuantity[choi2 - 1] -= qq; } else { System.out.println("Not found"); } } break; case 3: loop = true; loop2 = false; loop3 = false; break; default: if (m3 == 3) { System.out.println("-----------------------BYE!----------------------- "); System.exit(0); } else {
System.out.println(" Invalid c1`hoica"); System.out.println("Try again :( " + (m3 - 1) + " )"); m3++; int ii = 0; // System.out.println(" "); int iii = 0; for (Item Items : listItem) { iii++; System.out.println(iii + ".Name: " + Items.toString()); i++; loop = false; loop2 = false; loop3 = true; } } } } break;
case 3: loop = true; loop2 = false; break; default: if (e == 3) { System.out.println("-----------------------BYE!----------------------- "); System.exit(0); } else { e++; System.out.println(" Invalid c1`hoica"); System.out.println("Try again :( " + (e) + " )"); e++; loop = true; loop2 = false; } break; } } break;
case 2: System.out.println(" -----------------------Your purchases-----------------------"); System.out.println("You have to pay: "); for (Itemorder itemorder1 : market) { System.out.println(itemorder1.show()); } System.out.println("Total purchases: " + total + " "); boolean loop4 = true; while (loop4) { System.out.println("what do you want?" + " 1.Back to back" + " 2.Exit"); System.out.print("Your choice: "); int choic = in.nextInt(); switch (choic) { case 1: loop = true; loop4 = false; System.out.println(" "); break; case 2: System.out.println("-----------------------BYE!----------------------- "); System.exit(0); break; default: if (p4 == 3) { System.out.println("-----------------------BYE!----------------------- "); System.exit(0); } else { p4++; System.out.println(" Invalid c1`hoica"); System.out.println("Try again :( " + (p4 - 1) + " )"); loop = false; loop4 = true; break; } break; } } break; case 3: System.out.println(" -----------------------BYE!----------------------- "); System.exit(0); break; default: if (p == 3) { System.out.println(" -----------------------ERROR in INPUT!----------------------- "); System.exit(0); } else { p++; System.out.println(" Invalid c1`hoica"); System.out.println("Try again :( " + (p - 1) + " ) "); } loop = true;
break; } } } }
JAVA
What is the solution to the problem in Display Cart and Remove From cart
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
