Adding to a HashMap difficulty? (Logic)

I am creating a shopping cart program where there are three classes, the item, the item ordered, and the shopping cart. I have everything right in Item, and ItemOrder, but am having some difficulties adding values to my HashMap in the shopping cart. Basically if the user is adding items to their shopping cart, if they change the quantity from 3 to 5 for an item, the order for that item should be replaced with the new price. This is what I am having trouble with, because I am not sure (logically) what to really do. This is an assignment, so any hints or examples would be nice.

here is my Item class

package model;

import java.math.BigDecimal;
import java.util.Objects;

public final class Item {
   private String theName;
   private BigDecimal thePrice;
   private int theBulkQuantity;
   private BigDecimal theBulkPrice;


public Item(final String theName, final BigDecimal thePrice) {
    this.theName = theName;
    this.thePrice = thePrice;
    this.theBulkQuantity = 0;
    this.theBulkPrice = null;


}

 /**
  * 
  * @param theName
  * @param thePrice
  * @param theBulkQuantity
  * @param theBulkPrice
  */
   public Item(final String theName, final BigDecimal thePrice, final int theBulkQuantity,
            final BigDecimal theBulkPrice) {
    this.theName = theName;
    this.thePrice = thePrice;
    this.theBulkQuantity = theBulkQuantity;
    this.theBulkPrice = theBulkPrice;
    if (thePrice.doubleValue() < 0 || theBulkPrice.doubleValue() < 0) {
        throw new IllegalArgumentException("Price must be greater than 0.");

    }
    if (theBulkQuantity < 0) {
        throw new IllegalArgumentException("Quantity must be greater than 0. ");
    }
    Objects.requireNonNull(theName, "The item name must not be null");
    Objects.requireNonNull(thePrice, "Price must not be null.");
    Objects.requireNonNull(theBulkPrice, "Bulk price must not be null.");

}

public BigDecimal getPrice() {

    return thePrice;
}


public int getBulkQuantity() {

    return theBulkQuantity;
}


public BigDecimal getBulkPrice() {

    return theBulkPrice;
}

public void setBulkQuantity(int theBulkQuantity) {
    this.theBulkQuantity = theBulkQuantity;

}
public boolean isBulk() {
    if(theBulkQuantity == 0 || theBulkPrice == null){
        return false;
    }else{

    return true;
    }
}

@Override
public String toString() {
   // StringBuffer eachItem= new StringBuffer();
    final StringBuilder eachItem = new StringBuilder();
    if (isBulk()) {

        eachItem.append(theName);
        eachItem.append(", ");
        eachItem.append('$');
        eachItem.append(thePrice);
        eachItem.append('(');
        eachItem.append(theBulkQuantity);
        eachItem.append(" for ");
        eachItem.append(theBulkQuantity);
        eachItem.append(theBulkPrice);
        eachItem.append(')');

        //return eachItem.toString();
    } else {
        eachItem.append(theName);
        eachItem.append(", ");
        eachItem.append('$');
        eachItem.append(thePrice);
        //return eachItem.toString();
    }
    return eachItem.toString();
}


@Override
public boolean equals(final Object theOther) {
    final boolean result;
    if (this == theOther) {
        result = true;
    } else if (theOther == null || getClass() != theOther.getClass()) {
        result = false;
    } else {
        final Item other = (Item) theOther;
        result = theName.equals(other.theName) && thePrice == other.thePrice 
                && theBulkPrice == other.theBulkPrice 
                && theBulkQuantity == other.theBulkQuantity;

    }
    return result;

}


@Override
public int hashCode() {

    return Objects.hash(thePrice, theName);
}

}

here is my ItemOrder

 package model;



 public final class ItemOrder {
   private Item theItem;
   private int theQuantity;


public ItemOrder(final Item theItem, final int theQuantity) {
    this.theItem = theItem;
    this.theQuantity = theQuantity;
    if (theQuantity < 0) {
        throw new IllegalArgumentException("Quantity must be greater than 0.");
    }

}


public Item getItem() {

    return theItem;
}
public int getQuantity(){
    return theQuantity;

}



@Override
public String toString() {
    final StringBuilder order = new StringBuilder(128);
    order.append("Item: ");
    order.append(theItem);
    order.append("\nQuantity: ");
    order.append(theQuantity);

    return order.toString();
  }

}

Here is the class Im having issues with (this is only half the code because the other half is not necessary for you to see.

    package model;


import java.math.BigDecimal;
import java.util.HashMap;

public class ShoppingCart {
    private HashMap <String, Integer> myItems;


/**
 * Constructor, creates empty shopping cart.
 */
public ShoppingCart() {
   myItems = new HashMap<String, Integer>();
   // myItems = new ArrayList<ItemOrder>();   

}
public void add(final ItemOrder theOrder) {
    myItems.put(theOrder.getItem().toString(), theOrder.getQuantity());
    System.out.println(myItems); // testing my put()

    //if (theOrder.equals(myItems)) { // use overridden .equals in Item
        /* What I am trying to do here is that if theOrder equals an order
         in my hashMap, then replace it and put in the new order,
         I don't really know what to put in the if statement */
    //}



}

Answers


A Map is like a Math Function F, that, given a Key k, you get a Value v: F(k) = v

Your shopping cart is using the Item.toString itself as the Key, and the Quantity as the Value.

That doesn't do much sense. You already have this informations on the ItemOrder object.

So, it would be better if you use a List<ItemOrder> as you shoppingCart, instead of the HashMap.

public class ShoppingCart {
    private List<ItemOrder> myItems = new ArrayList<ItemOrder>();


public void add(final ItemOrder theOrder) {
    myItems.add(theOrder);
    for(ItemOrder order : myItens) {
    //Print each order
    }
}

While using a List<ItemOrder> makes more sense, I think it's worth mentioning that there is no difference between adding an item to the HashMap and updating a value for an item since HashMap's cannot contain duplicate keys.

So, say you start with an empty cart and want to add 4 bananas.

myItems.put("banana", 4);

Then, you can either change the quantity by simply doing

myItems.put("banana", 6);

Or, you could "add" more bananas by doing

if (myItems.containsKey("banana")) {
    myItems.put("banana", myItems.get("banana") + 2));
}

Either way, the HashMap only has one entry for "banana".


Need Your Help

Symfony 2.0 - routing

symfony php-5.3

How can I set up routing in symfony to be like that(if one rule won't work, next should be grabbed):

What's the right way to keep my program from ending while waiting on events

c# multithreading events

I have an app that sets some events and needs to wait for those events to occur before exiting. What's the best way do this? I understand that Thread.Sleep is a poor choice for various reasons