-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCart.java
More file actions
116 lines (105 loc) · 3.06 KB
/
Cart.java
File metadata and controls
116 lines (105 loc) · 3.06 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package onlineshop;
import onlineshop.merchandise.Book;
import onlineshop.merchandise.CartItem;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.SessionScope;
import java.util.ArrayList;
import java.util.List;
@Component
@SessionScope
public class Cart {
final static int MAX_QUANTITY = 15;
private List<CartItem> items = new ArrayList<>();
public List<CartItem> getItems() {
return items;
}
/**
* Counts cart items and their quantity
* @return numberOfItems {@link Integer}
*/
public int getNumOfItems() {
int numOfItems = 0;
for (CartItem item : items) {
numOfItems += item.getQuantity();
}
return numOfItems;
}
/**
* Sums all cart items, taking their quantity into account
* @return formattedNumber {@link String}
*/
public String getGrandTotal() {
double total = 0;
for (CartItem item : items) {
total += item.getSubtotal();
}
return Shop.df.format(total);
}
/**
* Adds an article/book to the cart
* @param book {@link Book}
*/
public void addArticle(Book book) {
CartItem item = findItem(book.getArticleNo());
if (item == null) {
item = new CartItem(book);
items.add(item);
}
// limit quantity
if (item.getQuantity() <= MAX_QUANTITY) {
item.increaseQuantity();
}
}
public boolean increaseQuantity(int articleNo) {
CartItem existingItem = findItem(articleNo);
if (existingItem != null) {
existingItem.increaseQuantity();
return true;
}
return false;
}
/**
* Decreases the quantity of an existing article.
* If quantity sinks below 1, it removes the article and returns 'false'.
* @param articleNo {@link Integer}
* @return isArticleNotRemoved {@link Boolean}
*/
public boolean decreaseQuantity(int articleNo) {
CartItem existingItem = findItem(articleNo);
if (existingItem != null) {
existingItem.decreaseQuantity();
if (existingItem.getQuantity() < 1) {
items.remove(existingItem);
return false;
}
return true;
}
return false;
}
/**
* Removes an article from the cart
* @param articleNo {@link Integer}
* @return wasSuccesful {@link Boolean}
*/
public boolean removeArticle(int articleNo) {
CartItem existingItem = findItem(articleNo);
if (existingItem != null) {
items.remove(existingItem);
return true;
}
return false;
}
/**
* Finds an article by its article number
* @param articleNo {@link Integer}
* @return existingItem {@link CartItem}
*/
private CartItem findItem(int articleNo) {
for (CartItem item : items) {
if (item.getArticleNo() == articleNo) {
return item;
}
}
return null;
}
}