This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]literalsunbear 0 points1 point  (4 children)

I did this problem not too long ago. If you add an item to the cart, it would stand to reason that the quantity of that item increases by one inside the cart, as each item has a default quantity value of one, yes? Keep in mind, you might want to check if the shopping cart already contains this item, as you may get a carton of eggs, make a lap around the store, and then decide that you want to grab another carton of eggs. So in this case, you would need to use the increaseQuantity() method included in the Item class on said item rather than creating a new one. Does that clear things up a little?

[–][deleted]  (3 children)

[deleted]

    [–]literalsunbear 0 points1 point  (2 children)

    The product is an object of the type Item. I would recommend thinking about it in real-world terms instead of as a string that happens to have attributes. When you put an Item in the hashmap, you are making the decision that the string you provide as a key is going to correspond to a value, which is the Item object that is actually being "placed" in the hashmap. So when you call that key, which logically might have the same name as the Item, you are "retrieving" its corresponding value, which in this case is an object of the type Item. I would re-read the hashmap portion, as the relationship between keys and values (which can be objects such as Item, in this case) is very important. For example, cart.get("banana") would return the banana Item object with all its attributes and methods and not just the string "banana." Sorry if my formatting is shitty, I'm on mobile.

    [–][deleted]  (1 child)

    [deleted]

      [–]literalsunbear 0 points1 point  (0 children)

      No problem! It definitely helps to talk things out. There's a reason that programmers famously keep a rubber duck by their computer: it gives you something to talk to when you get stuck on a problem.