-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay22.java
More file actions
235 lines (189 loc) · 6.79 KB
/
Day22.java
File metadata and controls
235 lines (189 loc) · 6.79 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// ---------------------------------------------------
// Author : Benjamin Kataliko Viranga
// Community : Stunt Business
// Community website : www.stuntbusiness.com
//
// 30 Days - Q&A Java basic
// Day 22 : Mini Store | Customer.
// Day 22 | IG : https://www.instagram.com/benjivrik/
// ----------------------------------------------------
// what would be the output of this program ?
import java.util.HashMap;
import java.util.List;
import java.util.NoSuchElementException;
class Customer
{
private Store store;
private String name;
private double budget;
private HashMap<String,Double> cart;
public Customer(String name, double budget)
{
this.store = null; // the store value is set using setStore()
this.name = name;
this.budget = budget;
this.cart = new HashMap<String,Double>();
}
// set the store of the customer
public void setStore(Store store)
{
this.store = store;
}
// Customer name
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
// add item to cart
// Assuming the item always exists in the store
public void addToCart(String itemName)
{
if (this.store == null)
throw new NullPointerException("A store has not been set for the customer " + this.name + ".");
// get the list of items from the store
List<Item> items = this.store.getItems();
// find the items
Item item = findItemByName(items, itemName);
if (item == null)
throw new NullPointerException("Item '" +itemName+"' not found in the store.");
// add to the cart
this.cart.put(item.getItemName(), item.getPrice());
}
// remove item from cart
public void removeFromCart(String itemName)
{
if(this.cart.containsKey(itemName))
{
this.cart.remove(itemName);
}
else
throw new NoSuchElementException("Item '" +itemName+"' not found in your cart.");
}
// Utility - find item by name
private Item findItemByName(List<Item> items, String itemName)
{
for(Item item : items)
{
if(item.getItemName().equals(itemName))
{
return item;
}
}
System.out.println("\nItem not found.\n");
return null;
}
// calculate the bill of the curstomer
public boolean calculateBill()
{
boolean canPay = false; // if the customer has enough money for proceeding with the payment
double bill = 0;
for(String item : cart.keySet())
{
bill += cart.get(item);
}
if(bill > this.budget)
{
System.out.println("\nYour bill is "+ bill + " CAD.\n");
System.out.println("However, " + this.budget + " CAD is your budget.\nYou might want to remove some items.\n");
}
else
{
System.out.println("\n> Your bill is "+ bill + " CAD.\n");
System.out.println("\n> Your budget is "+ this.budget + " CAD.\n");
// bill <= this.budget
System.out.println("\nYou are good to go. You have enough money.\n");
canPay = true;
}
return canPay;
}
// customer buying items and changing the store content
// Assumption : Supposing the customer is only adding one item for each product in the store
// i.e the user is not buying two TVs at the same time
public void buy()
{
// get the list of items from the store
List<Item> items = this.store.getItems();
if(this.calculateBill())
{
for(String itemName : cart.keySet())
{
// find the items
Item item = findItemByName(items, itemName);
// decrease the stock of the item in the store
int quantity = 1; // this value will be always be 1 based on the assumption made
// update item in the store
this.store.sell(item, quantity);
// decrease the customer budget
this.budget -= cart.get(itemName);
}
// empty the cart
this.cart.clear();
}
else
{
System.out.println("\nPlease remove some items before processing with the checkout.\n");
}
}
// display customer information
// Assuming the customer can only take one entity for each Item
public String toString()
{
System.out.println("\n************** DISPLAYING CUSTOMER INFO **************\n");
String customer = "";
customer += "\n> Customer name : "+this.name+".\n"+"> Customer budget : "+this.budget+" CAD.\n";
// get the list of items from the store
List<Item> items = this.store.getItems();
if(this.cart.size() == 0)
customer += "\nYou do not have any item in your cart.\n";
else
{
customer += "\nYour cart has the following items.\n";
for(String itemName : this.cart.keySet())
{
Item item = findItemByName(items,itemName);
if(item != null)
customer += "\n> "+itemName+ ": " + item.getPrice() + " CAD (Unit Price)\n";
else // Just be sure that item really exists in the store. Throwing this exception for letting you know
throw new NullPointerException("No items with the name '" + itemName + "' were found.");
}
}
return customer;
}
}
public class Day22 {
public static void main(String[] args)
{
Store store = new Store("Stunt-Business");
Item computer = new Item("laptop", "computer", 200);
Item tv = new Item("TV", "computer", 1200);
// display the information about your store
System.out.println(store);
System.out.println("\n>>>> Adding items in the store <<<<\n");
// adding five laptops to the store
store.addItem(computer, 5);
// add ten Tvs in the store
store.addItem(tv,10);
// display the information about your store
System.out.println(store);
// create a customer
Customer customer = new Customer("Jivrik", 4000);
// set the store the customer
customer.setStore(store);
// add laptop to cart
customer.addToCart("laptop");
// add laptop to the cart
customer.addToCart("TV");
// display customer info
System.out.println(customer);
// buy your items
customer.buy();
// display customer info
System.out.println(customer);
// display the information about your store
System.out.println(store);
}
}