-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShoppingCart.java
84 lines (77 loc) · 2.25 KB
/
ShoppingCart.java
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
import java.util.Date;
import java.util.List;
public class ShoppingCart {
private Date created;
private List<LineItem> lineItemList;
private Account account;
private User user;
//we removed the creation date
public ShoppingCart( Account account, User user) {
this.created =new Date();
this.account = account;
this.user = user;
lineItemList = null;
}
public User getUser() {
return user;
}
public boolean addUser(User user) {
if (user != null) {
if (this.user == null){
this.user = user;
this.user.addShoppingCart(this);
return true;
}
if (this.user.getShoppingCart() != null && !equals(this.user.getShoppingCart())) {
return false;
}
User old = this.user;
this.user.addShoppingCart(this);
old.deleteShoppingCart(this);
//old.deleteShoppingCart(old.getShoppingCart());
return true;
}
return false;
}
public Account getAccount() {
return account;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public boolean addLineItem(LineItem lineItem){
boolean added = false;
if (lineItemList.contains(lineItem)){
return false;
}
lineItemList.add(lineItem);
added = lineItem.addShoppingCart(this);
if (added == false)
lineItemList.remove(lineItem);
return added;
}
public boolean deleteLineItem(LineItem li){
boolean deleted = false;
int ind = 0;
if(!lineItemList.contains(li)){
return false;
}
ind = lineItemList.indexOf(li);
lineItemList.remove(ind);
deleted = li.addShoppingCart(this);
if (deleted == false){
lineItemList.add(ind,li);
}
return deleted;
}
public boolean setAccount(Account account){
if (account == null) {
return false;
}
this.account=account;
return true;
}
}