forked from miciek/grokkingfp-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch02_ShoppingCartDiscounts.java
211 lines (171 loc) · 6.63 KB
/
ch02_ShoppingCartDiscounts.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ch02_ShoppingCartDiscounts {
static class ShoppingCartBad { // named ShoppingCart in the book
private List<String> items = new ArrayList<>();
private boolean bookAdded = false;
public void addItem(String item) {
items.add(item);
if(item.equals("Book")) {
bookAdded = true;
}
}
public int getDiscountPercentage() {
if(bookAdded) {
return 5;
} else {
return 0;
}
}
public List<String> getItems() {
return items;
}
}
static class ShoppingCartCopying { // named ShoppingCart in the book
private List<String> items = new ArrayList<>();
private boolean bookAdded = false;
public void addItem(String item) {
items.add(item);
if(item.equals("Book")) {
bookAdded = true;
}
}
public int getDiscountPercentage() {
if(bookAdded) {
return 5;
} else {
return 0;
}
}
public List<String> getItems() {
return new ArrayList<>(items);
}
}
static class ShoppingCartWithRemove { // named ShoppingCart in the book
private List<String> items = new ArrayList<>();
private boolean bookAdded = false;
public void addItem(String item) {
items.add(item);
if(item.equals("Book")) {
bookAdded = true;
}
}
public int getDiscountPercentage() {
if(bookAdded) {
return 5;
} else {
return 0;
}
}
public List<String> getItems() {
return new ArrayList<>(items);
}
public void removeItem(String item) {
items.remove(item);
if(item.equals("Book")) {
bookAdded = false;
}
}
}
static class ShoppingCartRecalculating { // named ShoppingCart in the book
private List<String> items = new ArrayList<>();
public void addItem(String item) {
items.add(item);
}
public int getDiscountPercentage() {
if(items.contains("Book")) {
return 5;
} else {
return 0;
}
}
public List<String> getItems() {
return new ArrayList<>(items);
}
public void removeItem(String item) {
items.remove(item);
}
}
static class ShoppingCart {
public static int getDiscountPercentage(List<String> items) {
if(items.contains("Book")) {
return 5;
} else {
return 0;
}
}
}
public static void main(String[] args) {
{
ShoppingCartBad cart = new ShoppingCartBad(); // ShoppingCart in the book
cart.addItem("Apple");
assert (cart.getDiscountPercentage() == 0);
System.out.println(cart.getDiscountPercentage());
cart.addItem("Lemon"); // not in the book, but lemons are great!
assert (cart.getDiscountPercentage() == 0);
System.out.println(cart.getDiscountPercentage());
cart.addItem("Book");
assert (cart.getDiscountPercentage() == 5);
System.out.println(cart.getDiscountPercentage());
// PROBLEM 1:
List<String> itemsBad = cart.getItems();
itemsBad.remove("Book");
assert(!cart.getItems().contains("Book")); // no book is in the cart
assert(cart.getDiscountPercentage() == 5); // BUT THE DISCOUNT IS 5!
}
// SOLUTION 1: COPYING
{
ShoppingCartCopying cart = new ShoppingCartCopying(); // ShoppingCart in the book
cart.addItem("Apple");
assert (cart.getDiscountPercentage() == 0);
cart.addItem("Lemon"); // not in the book, adding anything that isn't a "Book" shouldn't affect the result
assert (cart.getDiscountPercentage() == 0);
cart.addItem("Book");
assert (cart.getDiscountPercentage() == 5);
List<String> itemsCopying = cart.getItems();
itemsCopying.remove("Book");
assert (cart.getItems().contains("Book")); // book is in the cart
assert (cart.getDiscountPercentage() == 5); // so the discount is 5
}
// PROBLEM 2:
{
ShoppingCartWithRemove cart = new ShoppingCartWithRemove(); // ShoppingCart in the book
cart.addItem("Book");
cart.addItem("Book"); // adding a second book
assert (cart.getDiscountPercentage() == 5); // calling getDiscountPercentage() returns 5
cart.removeItem("Book");
assert (cart.getItems().contains("Book")); // a book is in the cart
assert (cart.getDiscountPercentage() == 0); // BUT THE DISCOUNT IS 0!
}
// SOLUTION 2: RECALCULATING
{
ShoppingCartRecalculating cart = new ShoppingCartRecalculating(); // ShoppingCart in the book
cart.addItem("Book");
cart.addItem("Book"); // adding a second book
assert (cart.getDiscountPercentage() == 5); // calling getDiscountPercentage() returns 5
cart.removeItem("Book");
assert (cart.getItems().contains("Book")); // a book is in the cart
assert (cart.getDiscountPercentage() == 5); // and the discount is 5
}
// PROBLEM 3
// so much code to calculate a simple discount...
// SOLUTION 3: JUST A FUNCTION
List<String> empty = new ArrayList<>();
assert(ShoppingCart.getDiscountPercentage(empty) == 0);
List<String> justApple = Arrays.asList("Apple");
assert(ShoppingCart.getDiscountPercentage(justApple) == 0);
List<String> appleAndBook = Arrays.asList("Apple", "Book");
assert(ShoppingCart.getDiscountPercentage(appleAndBook) == 5);
// imperative usage
List<String> items = new ArrayList<>();
assert(ShoppingCart.getDiscountPercentage(items) == 0);
System.out.println(ShoppingCart.getDiscountPercentage(items));
items.add("Apple");
assert(ShoppingCart.getDiscountPercentage(items) == 0);
System.out.println(ShoppingCart.getDiscountPercentage(items));
items.add("Book");
assert(ShoppingCart.getDiscountPercentage(items) == 5);
System.out.println(ShoppingCart.getDiscountPercentage(items));
}
}