添加到HashMap难度? (逻辑)

时间:2016-01-20 20:08:48

标签: java hashmap

我正在创建一个购物车程序,其中有三个类,项目,订购的商品和购物车。我在Item和ItemOrder中拥有一切,但是在购物车中将值添加到我的HashMap时遇到了一些困难。基本上,如果用户正在向购物车添加商品,如果他们将商品的数量从3更改为5,则该商品的订单应替换为新价格。这就是我遇到麻烦的原因,因为我不确定(逻辑上)要做什么。这是一个赋值,所以任何提示或示例都会很好。

这是我的Item类

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);
}

}

这是我的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();
  }

}

这是我遇到问题的类(这只是代码的一半,因为 你没有必要看另一半。

    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 */
    //}



}

2 个答案:

答案 0 :(得分:1)

地图就像数学函数F,给定一个密钥k,你得到一个价值v:F(k) = v

您的购物车使用Item.toString本身作为键,数量作为值。

这没有多大意义。您已经在ItemOrder对象上拥有此信息。

因此,如果您使用List<ItemOrder>作为shoppingCart而不是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
    }
}

答案 1 :(得分:0)

虽然使用this更有意义,但我认为值得一提的是,将项添加到HashMap和更新项的值之间没有区别,因为HashMap不能包含重复键。

所以,假设你从一个空车开始,想要添加4个香蕉。

var data= $('form.form_student').serialize();

然后,您可以通过简单地改变数量

List<ItemOrder>

或者,您可以通过

“添加”更多香蕉
myItems.put("banana", 4);

无论哪种方式,HashMap只有myItems.put("banana", 6); 的一个条目。