There is a Hashmap (o) which takes string as a key and Order Object as value. Order has an Arraylist of OrderLines. Here I have to add multiple orders to the map. The problem is my hashmap prints out unique first and second keys (order 1 and Order 2) but the last inserted value as value for both keys (duplicate order in all entries). Can you please help me debug the problem ?
Main Class :
Map<String, Order> o = new HashMap<String, Order>();
Order c = new Order();
c.add(new OrderLine(new Item("book", (float) 12.49), 1));
c.add(new OrderLine(new Item("music CD", (float) 14.99), 1));
o.put("Order 1", c);
// Reuse cart for an other order
c.clearCart(); // this.orderLines.clear() in the order class
c.add(new OrderLine(new Item("imported box of chocolate", 10), 1));
c.add(new OrderLine(new Item("imported bottle of perfume", (float) 47.50), 1));
o.put("Order 2", c);
for (Map.Entry<String, Order> entry : o.entrySet()) {
System.out.println("*******" + entry.getKey() + entry.getValue().get(0).getItem().getDescription() + "*******");
}
Order class :
class Order {
private List<OrderLine> orderLines = new ArrayList<>();
public void add(OrderLine o) throws Exception {
orderLines.add(o);
}
public OrderLine get(int i) {
return orderLines.get(i);
}
public void clearCart() {
this.orderLines.clear();
}
}
OrderLine class:
private int quantity;
private Item item;
public OrderLine(Item item, int quantity) throws Exception {
if (item == null) {
System.err.println("ERROR - Item is NULL");
throw new Exception("Item is NULL");
}
assert quantity > 0;
this.item = item;
this.quantity = quantity;
}
public Item getItem() {
return item;
}
public int getQuantity() {
return quantity;
}
}
Item class:
class Item {
private String description;
private float price;
public Item(String description, float price) {
super();
this.description = description;
this.price = price;
}
public String getDescription() {
return description;
}
public float getPrice() {
return price;
}
}