-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasket.java
More file actions
85 lines (71 loc) · 2.02 KB
/
Basket.java
File metadata and controls
85 lines (71 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.basket.manager;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.basket.manager.exception.ItemNotFoundException;
public class Basket implements IBasket {
private String userId;
private Map<String, Item> map;
public Basket(){}
public Basket(String pUserId){
userId = pUserId;
map = new HashMap<String, Item>();
}
@Override
public boolean addItem(Item i) {
// if map already contains item, get item from map and update quantity and price
if(map.containsKey(i.getItemName())){
Item item = map.get(i.getItemName());
item.setQuantity(item.getQuantity() + i.getQuantity());
item.setPrice(item.getPrice() + i.getPrice());
return true;
}
map.put(i.getItemName(), i);
return false;
}
@Override
public boolean removeItem(String itemId) throws ItemNotFoundException {
// remove item if it is found in basket
if(map.containsKey(itemId)){
System.out.println("removing item" + map.get(itemId).getItemName());
map.remove(itemId);
return true;
}
else
throw new ItemNotFoundException("item with id " + itemId + " not found");
}
@Override
public Item getItemFromBasket(String itemId) throws ItemNotFoundException {
if(map.containsKey(itemId)){
System.out.println("removing item" + map.get(itemId).getItemName());
return map.get(itemId);
}
else
throw new ItemNotFoundException("item with id " + itemId + " not found");
}
@Override
public Collection<Item> getBasketDetails() {
return map.values();
}
@Override
public int productCount() {
return map.size();
}
@Override
public double getBasketTotalCost() {
double price = 0.0d;
Iterator<Item> itr = getBasketDetails().iterator();
while(itr.hasNext()){
Item i = itr.next();
price+=i.getPrice() * i.getQuantity();
}
return price;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}