-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.java
More file actions
543 lines (499 loc) · 19.5 KB
/
Copy pathInventory.java
File metadata and controls
543 lines (499 loc) · 19.5 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/**
* Inventory class
*
* @author jusps
* @version 2
*/
import java.util.ArrayList;
import java.util.Scanner;//debug
/**
*
* @author jusps
*/
public class Inventory implements BookstoreSpecification {
private ArrayList<Book> stockBook = new ArrayList<Book>();
private ArrayList<CD> stockCD = new ArrayList<CD>();
private ArrayList<DVD> stockDVD = new ArrayList<DVD>();
private double totalValue = 0;
/**
* gets the number of different items in stock, as in the size of the total ArrayList of the items
* (byte) type codes: 0 is book, 1 is CD, 2 is DVD
*
* @param byte type
* @return size of ArrayList
*/
public int getUniqueProductStock(byte type) {
//type 0=book, 1=CD, 2=DVD
switch(type) {
case 0:
//failsafe if empty
if (stockBook.isEmpty()) return 0;
//return the size of the arrayList
return stockBook.size();
case 1:
//failsafe if empty
if (stockCD.isEmpty()) return 0;
//return the size of the arrayList
return stockCD.size();
case 2:
//failsafe if empty
if (stockDVD.isEmpty()) return 0;
//return the size of the arrayList
return stockDVD.size();
}
//failsafe if the type provided was invalid
return 0;
}
/**
* gets the number of products in stock, by adding the stock of each item in the ArrayList
* (byte) type codes: 0 is book, 1 is CD, 2 is DVD
*
* @param byte type
* @return int total
*/
public int getProductStock(byte type) {
//make integer total that will count up and be returned
int total = 0;
//type 0=book, 1=CD, 2=DVD
switch(type) {
case 0:
//failsafe if empty
if (stockBook.isEmpty()) return 0;
//add up total and return it
for (Product i : stockBook) total+= i.getStock();
return total;
//lol I don't have to break because it already returned lets gooooo
case 1:
//failsafe if empty
if (stockCD.isEmpty()) return 0;
//add up total and return it
for (Product i : stockCD) total+= i.getStock();
return total;
case 2:
//failsafe if empty
if (stockDVD.isEmpty()) return 0;
//add up total and return it
for (Product i : stockDVD) total+= i.getStock();
return total;
}
//failsafe if the type provided was invalid
return 0;
}
/**
* gets the stock of a particular product of the type and at the index provided
* (byte) type codes: 0 is book, 1 is CD, 2 is DVD
*
* @param byte type
* @param int index
* @return int stock
*/
public int getProductStock(byte type, int index) {
//type 0=book, 1=CD, 2=DVD
switch(type) {
case 0:
return stockBook.get(index).getStock();
case 1:
return stockCD.get(index).getStock();
case 2:
return stockDVD.get(index).getStock();
}
//failsafe if type provided was invalid
return 0;
}
/**
* gets the name of a particular product of the type and at the index provided
* (byte) type codes: 0 is book, 1 is CD, 2 is DVD
*
* @param byte type
* @param int index
* @return String name
*/
public String getName(byte type, int index) {
//type 0==book, 1==CD, everything else==DVD
if (type == 0) return stockBook.get(index).getName();
if (type == 1) return stockCD.get(index).getName();
return stockDVD.get(index).getName();
}
/**
* gets the description of a particular product of the type and at the index provided
* (byte) type codes: 0 is book, 1 is CD, 2 is DVD
*
* @param byte type
* @param int index
* @return String description
*/
public String getDescription(byte type, int index) {
//type 0==book, 1==CD, everything else==DVD
if (type == 0) return stockBook.get(index).getDescription();
if (type == 1) return stockCD.get(index).getDescription();
return stockDVD.get(index).getDescription();
}
/**
* gets the price of a particular product of the type and at the index provided
* (byte) type codes: 0 is book, 1 is CD, 2 is DVD
*
* @param byte type
* @param int index
* @return int price
*/
public int getPrice(byte type, int index) {
//type 0==book, 1==CD, everything else==DVD
if (type == 0) return stockBook.get(index).getPrice();
if (type == 1) return stockCD.get(index).getPrice();
return stockDVD.get(index).getPrice();
}
/**
* restocks books using the name in the parameter, and the number for the amount to restock
*
* @param String name
* @param int number
* @param boolean hardcover
*/
public void restockBook(String name, int number, boolean hardcover) {
//failsafe if empty
if (stockBook.isEmpty()) {
stockBook.add(new Book(name, "no description", hardcover));
stockBook.get(0).addStock(number);
} else {
//create book as link to product
Product book = stockBook.get(0);
boolean found = false;
//cycle through to find the book
//FOR SOME REASON I CAN'T ITERATE USING book, SO I HAVE TO MAKE A NEW BOOK ITERATOR
//a wasteful shame if you ask me.
for (Book bookIterator : stockBook) {
if (bookIterator.getName().equals(name) && bookIterator.isHardCover() == hardcover) {
book = bookIterator;
found = true;
break;
}
}
//if it was found, increase stock, if it wasn't create new product
if (found) {
book.addStock(number);
} else {
//find where to put it in the list and keep alphabetical order
int index = 0;
while (index < stockBook.size() && stockBook.get(index).getName().compareTo(name) < 0) index++;
stockBook.add(index, new Book(name, "no description", hardcover));
stockBook.get(index).addStock(number);
}
}
//recalculate the total inventory value
recalculateTotalInventoryValue();
}
/**
* restocks CDs using the name in the parameter, and the number for the amount to restock
*
* @param String name
* @param int number
*/
public void restockCD(String name, int number, boolean isRom) {
//failsafe if empty
if (stockCD.isEmpty()) {
stockCD.add(new CD(name, "no description", isRom));
stockCD.get(0).addStock(number);
} else {
//create CD as link to product
Product CD = stockCD.get(0);
boolean found = false;
//cycle through to find the CD
//FOR SOME REASON I CAN'T ITERATE USING CD, SO I HAVE TO MAKE A NEW CD ITERATOR
//a wasteful shame if you ask me.
for (CD CDIterator : stockCD) {
if (CDIterator.getName().equals(name)) {
CD = CDIterator;
found = true;
break;
}
}
//if it was found, increase stock, if it wasn't create new product
if (found) {
CD.addStock(number);
} else {
//find where to put it in the list and keep alphabetical order
int index = 0;
while (index < stockCD.size() && stockCD.get(index).getName().compareTo(name) < 0) index++;
stockCD.add(index, new CD(name, "no description", isRom));
stockCD.get(index).addStock(number);
}
}
//recalculate the total inventory value
recalculateTotalInventoryValue();
}
/**
* restocks CDs using the name in the parameter, and the number for the amount to restock
*
* @param String name
* @param int number
*/
public void restockDVD(String name, int number, boolean isBluRay) {
//failsafe if empty
if (stockDVD.isEmpty()) {
stockDVD.add(new DVD(name, "no description", isBluRay));
stockDVD.get(0).addStock(number);
} else {
//create DVD as link to product
Product DVD = stockDVD.get(0);
boolean found = false;
//cycle through to find the DVD
//FOR SOME REASON I CAN'T ITERATE USING DVD, SO I HAVE TO MAKE A NEW DVD ITERATOR
//a wasteful shame if you ask me.
for (DVD DVDIterator : stockDVD) {
if (DVDIterator.getName().equals(name)) {
DVD = DVDIterator;
found = true;
break;
}
}
//if it was found, increase stock, if it wasn't create new product
if (found) {
DVD.addStock(number);
} else {
//find where to put it in the list and keep alphabetical order
int index = 0;
while (index < stockDVD.size() && stockDVD.get(index).getName().compareTo(name) < 0) index++;
stockDVD.add(index, new DVD(name, "no description", isBluRay));
stockDVD.get(index).addStock(number);
}
}
//recalculate the total inventory value
recalculateTotalInventoryValue();
}
/**
* restocks products using the type parameter, the name in the parameter, and the number for the amount to restock
* (byte) type codes: 0 is book, 1 is CD, 2 is DVD
*
* @param byte type
* @param int index
* @param int amount
*/
public void restockProduct(byte type, int index, int amount) {
switch(type) {
case 0:
if (stockBook.size() > index && index > -1) {
stockBook.get(index).addStock(amount);
}
break;
case 1:
if (stockCD.size() > index && index > -1) {
stockCD.get(index).addStock(amount);
}
case 2:
if (stockDVD.size() > index && index > -1) {
stockDVD.get(index).addStock(amount);
}
}
//recalculate the total inventory value
recalculateTotalInventoryValue();
}
/**
* return dollar amount for current inventory of products * that is in stock
*
* @return total in stock value
*/
public double inventoryValue() {
//return the total value
return totalValue;
}
/**
* calculate dollar amount for current inventory of products * that is in stock
* This is so that the number can be calculated once, rather than once per frame. It is automatically called when anything has been restocked or sold
*
* @return total in stock value
*/
private void recalculateTotalInventoryValue() {
//reset to zero before adding to it
totalValue = 0;
//add the total amount in stock × the price
//also btw the price is stored as an int, with a .99 hard coded in, so I have to add 0.99 to the price
for (Book book : stockBook) totalValue += book.getStock() * (book.getPrice()+0.99);
for (CD cd : stockCD) totalValue += cd.getStock() * (cd.getPrice()+0.99);
for (DVD dvd : stockDVD) totalValue += dvd.getStock() * (dvd.getPrice()+0.99);
}
/**
* changes name of a particular product of the type and at the index provided, using the new name provided
* (byte) type codes: 0 is book, 1 is CD, 2 is DVD
*
* @param byte type
* @param int index
* @param String newName
*/
public void changeName(byte type, int index, String newName) {
//type 0==book, 1==CD, 2==DVD
switch(type) {
case 0: stockBook.get(index).setName(newName); break;
case 1: stockCD.get(index).setName(newName); break;
case 2: stockDVD.get(index).setName(newName); break;
}
}
/**
* sets description of a particular product of the type and at the index provided, using the description provided
* (byte) type codes: 0 is book, 1 is CD, 2 is DVD
*
* @param byte type
* @param int index
* @param String newDescription
*/
public void setDescription(byte type, int index, String newDesc) {
//type 0==book, 1==CD, 2==DVD
switch(type) {
case 0: stockBook.get(index).setDescription(newDesc); break;
case 1: stockCD.get(index).setDescription(newDesc); break;
case 2: stockDVD.get(index).setDescription(newDesc); break;
}
}
/**
* sets price of a particular product of the type and at the index provided, using the price provided
* (byte) type codes: 0 is book, 1 is CD, 2 is DVD
*
* @param byte type
* @param int index
* @param int price
*/
public void setPrice(byte type, int index, int pric) {
//type 0==book, 1==CD, 2==DVD
switch(type) {
case 0: stockBook.get(index).setPrice(pric); break;
case 1: stockCD.get(index).setPrice(pric); break;
case 2: stockDVD.get(index).setPrice(pric); break;
}
//recalculate the total inventory value
recalculateTotalInventoryValue();
}
/**
* deletes particular product of the type and at the index provided
* (byte) type codes: 0 is book, 1 is CD, 2 is DVD
*
* @param byte type
* @param int index
*/
public void delete(byte type, int index) {
//type 0==book, 1==CD, 2==DVD
switch(type) {
case 0: stockBook.remove(index); break;
case 1: stockCD.remove(index); break;
case 2: stockDVD.remove(index); break;
}
}
/**
* finds the index based on the type and name provided
* (byte) type codes: 0 is book, 1 is CD, 2 is DVD
*
* @param byte type
* @param String name
* @return int index
*/
public int findIndex(byte type, String name) {
int lrmid[] = new int[3];
//type 0==book, 1==CD, 2==DVD
switch(type) {
case 0:
//make the left right and middle variables for binary search
// [0] == L, [1] == M, [2] == R
lrmid[2] = stockBook.size()-1;
lrmid[0] = 0;
while (lrmid[0] <= lrmid[2]) {
lrmid[1] = (lrmid[0]+lrmid[2])/2;
if (stockBook.get(lrmid[1]).getName().compareTo(name) < 0) {
//if its after this spot, set the left to the right of the mid
lrmid[0] = lrmid[1]+1;
} else if (stockBook.get(lrmid[1]).getName().compareTo(name) > 0) {
//if its before this spot, set the right to the left of the mid
lrmid[2] = lrmid[1]-1;
} else {
return lrmid[1];
}
}
break;
case 1:
//make the left right and middle variables for binary search
// [0] == L, [1] == M, [2] == R
lrmid[2] = stockCD.size()-1;
while (lrmid[0] <= lrmid[2]) {
lrmid[1] = (lrmid[0]+lrmid[2])/2;
if (stockCD.get(lrmid[1]).getName().compareTo(name) < 0) {
//if its after this spot, set the left to the right of the mid
lrmid[0] = lrmid[1]+1;
} else if (stockCD.get(lrmid[1]).getName().compareTo(name) > 0) {
//if its after this spot, set the right to the left of the mid
lrmid[2] = lrmid[1]-1;
} else {
return lrmid[1];
}
}
break;
case 2:
//make the left right and middle variables for binary search
// [0] == L, [1] == M, [2] == R
lrmid[2] = stockDVD.size()-1;
while (lrmid[0] <= lrmid[2]) {
lrmid[1] = (lrmid[0]+lrmid[2])/2;
if (stockDVD.get(lrmid[1]).getName().compareTo(name) < 0) {
//if its after this spot, set the left to the right of the mid
lrmid[0] = lrmid[1]+1;
} else if (stockDVD.get(lrmid[1]).getName().compareTo(name) > 0) {
//if its after this spot, set the right to the left of the mid
lrmid[2] = lrmid[1]-1;
} else {
return lrmid[1];
}
}
break;
}
return -1;
}
/**
* return whether or not the item is a special variant of that object
* for type 0 (books) it checks if they're hardcover or not, for type 1 (CDs) it checks whether they're cd roms or not, for type 2 (DVDs) it checks whether they're blurays or not
*
* @param byte type
* @param int index
*/
public boolean isSpecial(byte type, int index) {
//i dont feel like writing an if statement checking if its below zero so im absing it
index = Math.abs(index);
//return the unique property which happens to be a bool in each of these
switch(type) {
case 0: if (index < stockBook.size()) return stockBook.get(index).isHardCover(); break;
case 1: if (index < stockCD.size()) return stockCD.get(index).isRom(); break;
case 2: if (index < stockDVD.size()) return stockDVD.get(index).isBluRay(); break;
}
//failsafe
return false;
}
/**
* flips around whether something is special or not
* for type 0 (books) it checks if they're hardcover or not, for type 1 (CDs) it checks whether they're cd roms or not, for type 2 (DVDs) it checks whether they're blurays or not
*
* @param byte type
* @param int index
*/
public void invertSpecial(byte type, int index) {
//i dont feel like writing an if statement checking if its below zero so im absing it
index = Math.abs(index);
//return the unique property which happens to be a bool in each of these
switch(type) {
case 0: if (index < stockBook.size()) stockBook.get(index).changeWhetherHardCover(); break;
case 1: if (index < stockCD.size()) stockCD.get(index).changeWhetherRom(); break;
case 2: if (index < stockDVD.size()) stockDVD.get(index).the239thRuleOfAcquisition(); break;
}
}
/**
* Compares items at the given indices
* (byte) type codes: 0 is book, 1 is CD, 2 is DVD
*
* @param byte type
* @param int index1
* @param int index2
* @return int comparison
*/
public int compare(byte type, int index1, int index2) {
switch(type) {
case 0: return stockBook.get(index1).compareTo(stockBook.get(index2));
case 1: return stockCD.get(index1).compareTo(stockCD.get(index2));
case 2: return stockDVD.get(index1).compareTo(stockDVD.get(index2));
}
return 0;
}
}