我正在创建一个购物车程序,其中有三个类,即商品,订购商品和购物车。我在Item和ItemOrder中拥有所有功能,但是在向购物车中的HashMap添加值时遇到了一些困难。基本上,如果用户要将商品添加到购物车中,如果他们将商品的数量从3更改为5,则该商品的订单应替换为新价格。这就是我遇到的麻烦,因为我(逻辑上)不确定该做什么。这是一项任务,因此任何提示或示例都将很好。
这是我的物品类
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 */
//}
}
最佳答案
映射就像数学函数F一样,给定键k,您将获得值v:F(k) = v
您的购物车使用Item.toString本身作为键,并使用数量作为值。
那没有多大意义。您已经在ItemOrder对象上拥有此信息。
因此,如果在购物车中使用List<ItemOrder>
而不是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
}
}