forked from HelloWorld521/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShoppingCart.java
More file actions
48 lines (45 loc) · 1014 Bytes
/
ShoppingCart.java
File metadata and controls
48 lines (45 loc) · 1014 Bytes
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
package com.briup.bean;
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;
public class ShoppingCart {
private Map<Long, OrderLine> map =
new TreeMap<Long,OrderLine>();
public Map<Long, OrderLine> getMap() {
return map;
}
public void addOrderline(OrderLine line){
OrderLine l =
map.get(line.getBook().getId());
if(l!=null){
long num = l.getNum()+line.getNum();
l.setNum(num);
}else{
map.put(line.getBook().getId(), line);
}
}
public void modifyLine(Long bookid,Long num){
OrderLine l = map.get(bookid);
l.setNum(num);
}
public void dropLine(Long bookid){
map.remove(bookid);
}
public double getCost(){
Collection<OrderLine> lines =
map.values();
double cost = 0;
for(OrderLine line:lines){
long num = line.getNum();
double price = line.getBook().getPrice();
cost = cost+num*price;
}
return cost;
}
public OrderLine getLine(Long bookid){
return map.get(bookid);
}
public void dropAll(){
map.clear();
}
}