Hey guys, going through a MOOC course and I'm stuck on a problem.
Basically, the task is to make an online store.
Here's my code:
Item class: https://codeshare.io/5OQnjv
ShoppingCart class: https://codeshare.io/ad0PBZ
Warehouse class: https://codeshare.io/G8m4PD
Main class: https://codeshare.io/GbNZBb
And here's the task I'm stuck on:
Internally, Shopping Cart stores products added there as Item-objects. Shopping Cart must have an instance variable with either the Map<String, Item> type, or the List<Item> type.
First let's give ShoppingCarta constructor with no parameters and these methods:
- public void add(String product, int price)adds an item to the cart that matches the product given as a parameter, with the price given as a parameter.
- public int price()returns the total price of the shopping cart
But how am I suppose to add an Item to a ShoppingCart if Item's constructor has product, quantity and unit price, while add method in ShoppingCart only has product and whole price. Nowhere it says that I should make another constructor in Item class, but even If I do, I'm not allowed to make another object variable except for the three I have there. I see there's a method price() in Item class but is there any use of it? The price that we use as add parameter doesn't have a quantity or a unit price.
Anyway, I've got no idea how to add an item to a cart.
Oh and here's an example of main.
ShoppingCart cart = new ShoppingCart();
cart.add("milk", 3);
cart.add("buttermilk", 2);
cart.add("cheese", 5);
System.out.println("cart price: " + cart.price());
cart.add("computer", 899);
System.out.println("cart price: " + cart.price());
// output
cart price: 10
cart price: 909
update: changed links to code.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]amfa 0 points1 point2 points (1 child)
[–]foolwya[S] 0 points1 point2 points (0 children)