-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay21.java
More file actions
235 lines (180 loc) · 6.13 KB
/
Day21.java
File metadata and controls
235 lines (180 loc) · 6.13 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 21 : Mini Store : Employees, Items and the Store | Store.
// Day 21 | IG : https://www.instagram.com/benjivrik/
// ----------------------------------------------------
// what would be the output of this program ?
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
class Store {
private String name;
private String address;
private List<Item> items;
private HashMap<String, Integer> itemStocks;
private List<Employee> employees;
public Store(String name)
{
this.name = name;
this.items = new ArrayList<Item>();
this.employees = new ArrayList<Employee>();
this.itemStocks = new HashMap<String,Integer>();
this.address = "";
}
// name
public String getStoreName()
{
return this.name;
}
public void setStoreName(String name)
{
this.name = name;
}
// address
public void setAddress(String address)
{
this.address = address;
}
public String getAddress()
{
return this.address;
}
// get Employees
public List<Employee> getEmployees()
{
return this.employees;
}
// add employees
public void addEmployee(Employee employee)
{
this.employees.add(employee);
}
// remove employees
public void removeEmployee(Employee employee)
{
this.employees.remove(employee);
}
// In case the parameter is the employee name
// Supposing the employee names are always unique
public void removeEmployee(String employeeName)
{
for(Employee employee : this.employees)
{
if(employee.getName().equals(employeeName))
{
this.employees.remove(employee);
System.out.println("Employee '"+employeeName+"' with salary '"+employee.getSalary()+" CAD' has been removed");
break;
}
}
}
// get all items
public List<Item> getItems()
{
return this.items;
}
// add item
public void addItem(Item item, int inStock)
{
this.items.add(item);
this.itemStocks.put(item.getItemName(),inStock);
}
// get inventory
public HashMap<String,Integer> getStoreInventory()
{
return this.itemStocks;
}
// remove item | supposing the item always exists in the store before removing
public void removeItem(Item item)
{
if(this.itemStocks.containsKey(item.getItemName()))
{
this.items.remove(item);
this.itemStocks.remove(item.getItemName());
}
else throw new NullPointerException("Sorry, The element "+ item.getItemName() + " does not exist in the store.");
}
// sell an item : just decrease the quantity in the store
public void sell(Item item, int quantity)
{
if(this.itemStocks.containsKey(item.getItemName()))
{
int inStock = this.itemStocks.get(item.getItemName());
// Ideally, you should catch all the possibilities
// For now, assume that the quantity is always less than the value of inStock
if(inStock == 0)
{
System.out.println("The item "+ item.getItemName() + " is sold out.");
}
else
{
this.itemStocks.put(item.getItemName(), inStock - quantity);
System.out.println("\n> Item(s) '"+item.getItemName()+"' sold.");
}
}
else throw new NullPointerException("Sorry, The element "+ item.getItemName() + " does not exist in the store.");
}
public String toString()
{
System.out.println("\n************** DISPLAYING STORE INFO **************\n");
String store = "";
store += "\n> Your store "+ this.name + " have " + this.employees.size() + " employee(s).\n";
store += "\nYour store have the following items:\n";
if(this.items.size() == 0)
{
store += "\n>>> You do not have any item in your store.\nPlease add some. <<<";
}
else
{
for(String itemName : this.itemStocks.keySet())
{
Item item = findItemByName(itemName);
if(item != null)
store += "\n> "+itemName+ ": " + item.getPrice() + " CAD | " + this.itemStocks.get(itemName) + " left in the Store\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 store;
}
// Used in toString
// Find the item is the store list of items using its name
private Item findItemByName(String itemName)
{
for(Item item : items)
{
if(item.getItemName().equals(itemName))
{
return item;
}
}
System.out.println("\nItem not found.\n");
return null;
}
}
public class Day21 {
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);
System.out.println("\n>>>> Selling 2 TVs in the store <<<<\n");
// selling 2 TVs
store.sell(tv,2);
// display the information about your store.
System.out.println(store);
}
}