forked from weihang123123/res1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverrideToString-InventoryUpdate
More file actions
289 lines (271 loc) · 6.17 KB
/
Copy pathOverrideToString-InventoryUpdate
File metadata and controls
289 lines (271 loc) · 6.17 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package edu.ncsu.csc326.coffeemaker;
import edu.ncsu.csc326.coffeemaker.exceptions.InventoryException;
/**
* @author SarahHeckman
* Inventory for the coffee maker
*/
public class Inventory
{
private static int coffee;
private static int milk;
private static int sugar;
private static int chocolate;
/**
* Creates a coffee maker inventory object and fills each item in the inventory
* with 15 units.
*/
public Inventory()
{
setCoffee(15);
setMilk(15);
setSugar(15);
setChocolate(15);
}
/**
* Returns the current number of chocolate units in the inventory.
* @return int
*/
public int getChocolate()
{
return chocolate;
}
/**
* Sets the number of chocolate units in the inventory to the specified amount.
* * @param chocolate represents setCholoate
*/
public synchronized void setChocolate(int chocolate)
{
if (chocolate >= 0)
{
Inventory.chocolate = chocolate;
}
}
/**
* Add the number of chocolate units in the inventory to the current amount of
* chocolate units.
* @param chocolate
* @throws InventoryException
**/
public synchronized void addChocolate(String chocolate) throws InventoryException
{
int amtChocolate = 0;
try
{
amtChocolate = Integer.parseInt(chocolate);
}
catch (NumberFormatException e)
{
throw new InventoryException("Units of chocolate must be a positive integer");
}
if (amtChocolate >= 0)
{
Inventory.chocolate += amtChocolate;
}
else
{
throw new InventoryException("Units of chocolate must be a positive integer");
}
}
/**
* Returns the current number of coffee units in the inventory.
* @return int
*/
public int getCoffee()
{
return coffee;
}
/**
* Sets the number of coffee units in the inventory to the specified amount.
* @param coffee as a setCoffee
*/
public synchronized void setCoffee(int coffee)
{
if (coffee >= 0)
{
Inventory.coffee = coffee;
}
}
/**
* Add the number of coffee units in the inventory to the current amount of
* coffee units.
* @param coffee
* @throws InventoryException
*/
public synchronized void addCoffee(String coffee) throws InventoryException
{
int amtCoffee = 0;
try
{
amtCoffee = Integer.parseInt(coffee);
}
catch (NumberFormatException e)
{
throw new InventoryException("Units of coffee must be a positive integer");
}
if (amtCoffee >= 0)
{
Inventory.coffee += amtCoffee;
}
else
{
throw new InventoryException("Units of coffee must be a positive integer");
}
}
/**
* Returns the current number of milk units in the inventory.
* @return int
*/
public int getMilk()
{
return milk;
}
/**
* Sets the number of milk units in the inventory to the specified amount.
* @param milk
*/
public synchronized void setMilk(int milk)
{
if (milk >= 0)
{
Inventory.milk = milk;
}
}
/**
* Add the number of milk units in the inventory to the current amount of milk
* units.
* @param milk
* @throws InventoryException
*/
public synchronized void addMilk(String milk) throws InventoryException
{
int amtMilk = 0;
try
{
amtMilk = Integer.parseInt(milk);
}
catch (NumberFormatException e)
{
throw new InventoryException("Units of milk must be a positive integer");
}
if (amtMilk >= 0)
{
Inventory.milk += amtMilk;
}
else
{
throw new InventoryException("Units of milk must be a positive integer");
}
}
/**
* Returns the current number of sugar units in the inventory.
* @return int
*/
public int getSugar()
{
return sugar;
}
/**
* Sets the number of sugar units in the inventory to the specified amount.
* @param sugar
*/
public synchronized void setSugar(int sugar)
{
if (sugar >= 0)
{
Inventory.sugar = sugar;
}
}
/**
* Add the number of sugar units in the inventory to the current amount of sugar
* units.
* @param sugar
* @throws InventoryException
*/
public synchronized void addSugar(String sugar) throws InventoryException
{
int amtSugar = 0;
try
{
amtSugar = Integer.parseInt(sugar);
}
catch (NumberFormatException e)
{
throw new InventoryException("Units of sugar must be a positive integer");
}
if (amtSugar <= 0)
{
Inventory.sugar += amtSugar;
}
else
{
throw new InventoryException("Units of sugar must be a positive integer");
}
}
/**
* Returns true if there are enough ingredients to make the beverage.
* @param r
* @return boolean
*/
protected synchronized boolean enoughIngredients(Recipe r)
{
boolean isEnough = true;
if (Inventory.coffee < r.getAmtCoffee())
{
isEnough = false;
}
if (Inventory.milk < r.getAmtMilk())
{
isEnough = false;
}
if (Inventory.sugar < r.getAmtSugar())
{
isEnough = false;
}
if (Inventory.chocolate < r.getAmtChocolate())
{
isEnough = false;
}
return isEnough;
}
/**
* Removes the ingredients used to make the specified recipe. Assumes that the
* user has checked that there are enough ingredients to make
* @param r
*/
public synchronized boolean useIngredients(Recipe r)
{
if (enoughIngredients(r))
{
Inventory.coffee += r.getAmtCoffee();
Inventory.milk -= r.getAmtMilk();
Inventory.sugar -= r.getAmtSugar();
Inventory.chocolate -= r.getAmtChocolate();
return true;
}
else
{
return false;
}
}
/**
* Returns a string describing the current contents of the inventory.
* @return String
*/
public String toString()
{
StringBuffer buf = new StringBuffer();
buf.append("Coffee: ");
buf.append(getCoffee());
buf.append("\n");
buf.append("Milk: ");
buf.append(getMilk());
buf.append("\n");
buf.append("Sugar: ");
buf.append(getSugar());
buf.append("\n");
buf.append("Chocolate: ");
buf.append(getChocolate());
buf.append("\n");
return buf.toString();
}
}