Hello guys, I'm trying to add an object called product into an ArrayList of orders. I'm getting an error: no suitable method found for add(Product). Did I miss anything?
This is my addOrder method:
public void addOrder(ArrayList<Order> orders, Product product){
orders.add(product);
}
I've passed the ArrayList of orders and the object Product product so that I can add the product into the list.
Under my User class, I have:
public void buyProduct(ArrayList<Product> products, ArrayList<Order> orders, Cart cart){
Scanner sc = new Scanner(System.in);
for (int i=0; i<products.size();i++){
System.out.println("-----------------------------");
System.out.println(i+1+".");
products.get(i).displayProducts(products);
}
System.out.println("-----------------------------");
System.out.print("Choose what product to buy: ");
int resp = sc.nextInt();
System.out.println("-----------------------------");
switch(resp){
case 1: {
System.out.print("Quantity: ");
int quantity = sc.nextInt();
cart.addOrder(orders, products.get(0));
products.get(0).setStocksLeft(products.get(0).getStocksLeft()-quantity);
}
break;
case 2: {
System.out.print("Quantity: ");
int quantity = sc.nextInt();
cart.addOrder(orders, products.get(1));
products.get(1).setStocksLeft(products.get(1).getStocksLeft()-quantity);
}
break;
case 3: {
System.out.print("Quantity: ");
int quantity = sc.nextInt();
cart.addOrder(orders, products.get(2));
products.get(2).setStocksLeft(products.get(2).getStocksLeft()-quantity);
}
break;
case 4: {
System.out.print("Quantity: ");
int quantity = sc.nextInt();
cart.addOrder(orders, products.get(3));
products.get(3).setStocksLeft(products.get(3).getStocksLeft()-quantity);
}
break;
default: System.out.println("Incorrect input!");
break;
}
}
The user inputs the correspoding number of the product they would want to purchase. Once they select the product, the user must input the quantity. Then I take the quantity they've inputted and deduct it from the stock.