In: Computer Science
I have 3 classes in the same package called Product, Products and ProductCart. Here are my codes->
Product
public class Product {
//instance variables
private int id;
private String name;
private double price;
private int quantity;
//constructor
public Product(int id, String name, double price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
//all setters and getters
public int getId() {
return id;
}
public void setId(int id) {
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 getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
//toString
@Override
public String toString() {
return id + ", " + name + ", $" + price + ", " + quantity+"pc";
}
}
Products
import java.util.ArrayList;
public class Products {
//for holding items
private ArrayList productList;
//initializing the products list
public Products() {
productList = new ArrayList<>();
}
//getter the product list
public ArrayList getProductList() {
return productList;
}
}
ProductCart
public class ProductCart {
//products vairable
private Products prod;
public ProductCart() {
prod = new Products();
}
//adding items
public void addItems(Product item) {
prod.getProductList().add(item);
}
//removing items
public boolean removeItem(Product item) {
return prod.getProductList().remove(item);
}
//displaying items
public void displayItem() {
for(Product item: prod.getProductList())
System.out.println(item);
}
//getting price of the item
public double getPriceOfAnItem(Product item) {
int index = prod.getProductList().indexOf(item);
if(index == -1) {
System.out.println("Product not found");
return -1;
}
else
return prod.getProductList().get(index).getPrice();
}
}
Now I want to create class where I ask the user to choose options to perform these tasks->
1. Display Store Products
2. Display Cart
0. Exit
(example if you choose 1 the console will display this-> 1 Chips 40.0 10, 2 Chocolate 60.0 6, 3 Milk 30.0 10)
(example if you choose 2 the console will display this-> 3. Add to Cart 4. Remove From Cart 0. Exit)
This is my code so far but it's not working well because i can not fix "case 4". Please modify it and fix case 4 or write a code that will help me solve the problem (you may write if else statements instead if you want)->
public static void main(String[] args) {
int user_choice = 5;
Scanner sc = new Scanner(System.in);
ProductCart pc = new ProductCart();
while (user_choice != 0) {
System.out.println("Please choose an option");
System.out.println("1.Display Store Products 2.Display Cart 3.Add
to cart 4.Remove from cart 0.Exit");
user_choice = sc.nextInt();
switch(user_choice) {
case 1:
pc.displayItem();
break;
case 2:
System.out.println("3.Add to cart 4.Remove from cart
0.Exit");
break;
case 3:
System.out.println("enter id");
int id = sc.nextInt();
System.out.println("enter name");
String name = sc.next();
System.out.println("Enter price");
double price = sc.nextDouble();
System.out.println("Enter quantity");
int quantity = sc.nextInt();
Product product = new Product(id, name, price, quantity);
pc.addItems(product);
break;
case 4:
System.out.println("enter id");
int id2 = sc.nextInt();
pc.removeItem(product);
break;
case 0:
System.out.println("Finish the operations and exit");
System.out.println("closing now");
System.exit(0);
break;
default:
System.out.println("Enter your choice again");
}
}
}
}

Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me