-
Notifications
You must be signed in to change notification settings - Fork 0
/
nivasharma_ruhungry_6_RUHungry.java
845 lines (748 loc) · 34.8 KB
/
nivasharma_ruhungry_6_RUHungry.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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
package restaurant;
/**
* RUHungry is a fictitious restaurant.
* You will be running RUHungry for a day by seating guests,
* taking orders, donation requests and restocking the pantry as necessary.
*
* Compiling and executing:
* 1. use the run or debug function to run the driver and test your methods
*
* @author Mary Buist
* @author Kushi Sharma
*/
public class RUHungry {
/*
* Instance variables
*/
// Menu: two parallel arrays. The index in one corresponds to the same index in the other.
private String[] categoryVar; // array where containing the name of menu categories (e.g. Appetizer, Dessert).
private MenuNode[] menuVar; // array of lists of MenuNodes where each index is a category.
// Stock: hashtable using chaining to resolve collisions.
private StockNode[] stockVar; // array of linked lists of StockNodes (use hashfunction to organize Nodes: id % stockVarSize)
private int stockVarSize;
// Transactions: orders, donations, restock transactions are recorded
private TransactionNode transactionVar; // refers to the first front node in linked list
// Queue keeps track of parties that left the restaurant
private Queue<Party> leftQueueVar;
// Tables Information - parallel arrays
// If tableSeats[i] has 3 seats then parties with at most 3 people can sit at tables[i]
private Party[] tables; // Parties currently occupying the tables
private int[] tableSeats; // The number of seats at each table
/*
* Default constructor
*/
public RUHungry () {
categoryVar = null;
menuVar = null;
stockVar = null;
stockVarSize = 0;
transactionVar = null;
leftQueueVar = null;
tableSeats = null;
tables = null;
}
/*
* Getter and Setter methods
*/
public MenuNode[] getMenu() { return menuVar; }
public String[] getCategoryArray() { return categoryVar;}
public StockNode[] getStockVar() { return stockVar; }
public TransactionNode getFrontTransactionNode() { return transactionVar; }
public TransactionNode resetFrontNode() {return transactionVar = null;} // method to reset the transactions for a new day
public Queue<Party> getLeftQueueVar() { return leftQueueVar; }
public Party[] getTables() { return tables; }
public int[] getTableSeats() { return tableSeats; }
/*
* Menu methods
*/
/**
*
* This method populates the two parallel arrays menuVar and categoryVar.
*
* Each index of menuVar corresponds to the same index in categoryVar (a menu category like Appetizers).
* If index 0 at categoryVar is Appetizers then menuVar at index 0 contains MenuNodes of appetizer dishes.
*
* 1. read the input file:
* a) the first number corresponds to the number of categories (aka length of menuVar and categoryVar)
* b) the next line states the name of the category (populate CategoryVar as you read each category name)
* c) the next number represents how many dishes are in that category - this will be the size of the linked list in menuVar for this category
* d) the next line states the name of the dish
* e) the first number in the next line represents how many ingredient IDs there are
* f) the next few numbers (all in the 100s) are each the ingredient ID
*
* 2. As you read through the input file:
* a) populate the categoryVar array
* b) populate menuVar depending on which index (aka which category) you are in
* i) make a dish object (with filled parameters -- don't worry about "price" and "profit" in the dish object for right now)
* ii) create menuNode and insert at the front of menuVar (NOTE! there will be multiple menuNodes in one index)
*
* @param inputFile - use menu.in file which contains all the dishes
*/
public void menu(String inputFile) {
StdIn.setFile(inputFile); // opens the inputFile to be read
int category = StdIn.readInt();
//System.out.println(category);
categoryVar = new String[category];
menuVar = new MenuNode[category];
for(int i = 0; i<category; i++){
categoryVar[i] = StdIn.readString();
// System.out.println("categoryVar=> "+categoryVar[i]);
int dishNum = StdIn.readInt();
// System.out.println("dishNum => "+dishNum);
for (int j = 0; j<dishNum; j++){
String p = StdIn.readLine();
String item = StdIn.readLine();
// System.out.println("item=> "+item);
int ID = StdIn.readInt();
// System.out.println("ID=> "+ID);
int[] ingredientID = new int[ID];
for(int k = 0; k<ID; k++){
ingredientID[k] = StdIn.readInt();
// System.out.print("ingredientID => "+ingredientID[k] +" ");
// System.out.println();
}
Dish dish = new Dish(categoryVar[i], item, ingredientID);
MenuNode node = new MenuNode(dish, null);
MenuNode ptr = menuVar[i];
node.setNextMenuNode(ptr);
menuVar[i] = node;
}
}
}
/**
* Find and return the MenuNode that contains the dish with dishName in the menuVar.
*
* * GIVEN METHOD *
* * DO NOT EDIT *
*
* @param dishName - the name of the dish
* @return the dish object corresponding to searched dish, null if dishName is not found.
*/
public MenuNode findDish ( String dishName ) {
MenuNode menuNode = null;
// Search all categories since we don't know which category dishName is at
for ( int category = 0; category < menuVar.length; category++ ) {
MenuNode ptr = menuVar[category]; // set ptr at the front (first menuNode)
while ( ptr != null ) { // while loop that searches the LL of the category to find the itemOrdered
if ( ptr.getDish().getName().equalsIgnoreCase(dishName) ) {
return ptr;
} else{
ptr = ptr.getNextMenuNode();
}
}
}
return menuNode;
}
/**
* Find integer that corresponds to the index in menuVar and categoryVar arrays that has that category
*
* * GIVEN METHOD *
* * DO NOT EDIT *
*
* @param category - the category name
* @return index of category in categoryVar
*/
public int findCategoryIndex ( String category ) {
int index = 0;
for ( int i = 0; i < categoryVar.length; i++ ){
if ( category.equalsIgnoreCase(categoryVar[i]) ) {
index = i;
break;
}
}
return index;
}
/*
* Stockroom methods
*/
/**
* PICK UP LINE OF THE METHOD:
* *can I insert myself into your life? cuz you always help me sort
* out my problems and bring stability to my mine*
*
* ***
* This method adds a StockNode into the stockVar hashtable.
*
* 1. get the id of the given newNode and use a hash function to get the index at which the
* newNode is being inserted.
*
* HASH FUNCTION: id % stockVarSize
*
* 2. insert at the front of the linked list at the specific index
*
* @param newNode - StockNode that needs to be inserted into StockVar
*/
public void addStockNode ( StockNode newNode ) {
// WRITE YOUR CODE HERE
int iid = newNode.getIngredient().getID();
int insrt = iid%stockVarSize;
StockNode ptr = stockVar[insrt];
if(ptr == null){
ptr = newNode;
stockVar[insrt] = newNode;
}
else{
newNode.setNextStockNode(ptr);
stockVar[insrt] = newNode;
}
}
/**
* This method finds an ingredient from StockVar (given the ingredientID)
*
* 1. find the node based upon the ingredient ID (you can go to the specific index using the hash function!)
* (a) this is an efficient search as it looks only at the linked list which the key hash to
* 2. find and return the node
*
* @param ingredientID - the ID of the ingredient
* @return the StockNode corresponding to the ingredientID, null otherwise
*/
public StockNode findStockNode (int ingredientID) {
// WRITE YOUR CODE HERE
int index = ingredientID % stockVarSize;
StockNode xmas = stockVar[index];
while(xmas != null){
if(xmas.getIngredient().getID() == ingredientID){
return xmas;
}
else{
xmas = xmas.getNextStockNode();
}
}
return xmas; // update the return value
}
/**
* This method is to find an ingredient from StockVar (given the ingredient name).
*
* * GIVEN METHOD *
* * DO NOT EDIT *
*
* @param ingredientName - the name of the ingredient
* @return the specific ingredient StockNode, null otherwise
*/
public StockNode findStockNode (String ingredientName) {
StockNode stockNode = null;
for ( int index = 0; index < stockVar.length; index ++ ){
StockNode ptr = stockVar[index];
while ( ptr != null ){
if ( ptr.getIngredient().getName().equalsIgnoreCase(ingredientName) ){
return ptr;
} else {
ptr = ptr.getNextStockNode();
}
}
}
return stockNode;
}
/**
* This method updates the stock amount of an ingredient.
*
* 1. you will be given the ingredientName OR the ingredientID:
* a) the ingredientName is NOT null: find the ingredient and add the given stock amount to the
* current stock amount
* b) the ingredientID is NOT -1: find the ingredient and add the given stock amount to the
* current stock amount
*
* (FOR FUTURE USE) SOMETIMES THE STOCK AMOUNT TO ADD MAY BE NEGATIVE (TO REMOVE STOCK)
*
* @param ingredientName - the name of the ingredient
* @param ingredientID - the id of the ingredient
* @param stockAmountToAdd - the amount to add to the current stock amount
*/
public void updateStock (String ingredientName, int ingredientID, int stockAmountToAdd) {
if(ingredientID != -1){
// System.out.println("inside else if statement => ");
//finding the stocknode
// System.out.println();
StockNode ptr = findStockNode(ingredientID);
int stock = ptr.getIngredient().getStockLevel();
// System.out.println("stock id => "+ ptr.getIngredient().getID());
// System.out.println("initial stock amount => "+stock);
// System.out.print(" for "+ptr.getIngredient().getName() +" ");
// System.out.println("stockAmount to add => "+stockAmountToAdd);
stock = stock + stockAmountToAdd;
ptr.getIngredient().setStockLevel(stock);
// System.out.println("updated stockLevel => "+stock);
// System.out.println("--------------------------");
return;
}
//checking if ingredient name is not null => execute
else if(ingredientName != null ){
// System.out.println("inside if statement => ");
//finding the stocknode containing the ingredient name
//System.out.println();
StockNode ptr = findStockNode(ingredientName);
int stock = ptr.getIngredient().getStockLevel();
// System.out.println("initial stock amount => "+stock);
// System.out.print(" for "+ptr.getIngredient().getName() +" ");
// System.out.println("stockAmount to add => "+stockAmountToAdd);
stock = stock + stockAmountToAdd;
ptr.getIngredient().setStockLevel(stock);
// System.out.println("updated stockLevel => "+stock);
// System.out.println("----------------------------");
return;
}
// if name is null use this
}
/**
* PICK UP LINE OF THE METHOD:
* are you a single ‘for’ loop? cuz i only have i’s for you
*
* ***
* This method goes over menuVar to update the price and profit of each dish,
* using the stockVar hashtable to lookup for ingredient's costs.
*
* 1. For each dish in menuVar, add up the cost for each ingredient (found in stockVar),
* and multiply the total by 1.2 to get the final price.
* a) update the price of each dish
* HINT! --> you can use the methods you've previously made!
*
* 2. Calculate the profit of each dish by getting the totalPrice of ingredients and subtracting from
* the price of the dish itself.
*
* @return void
*/
public void updatePriceAndProfit() {
// WRITE YOUR CODE HERE
int siize = menuVar.length;
for(int i = 0; i<siize; i++){
MenuNode ptr = menuVar[i];
while( ptr != null){
double dishCost = 0;
// System.out.println("initial dishcost => "+dishCost);
int [] arr = ptr.getDish().getStockID();
// System.out.println("arr size => "+ arr.length);
for(int j = 0; j<arr.length; j++){
StockNode stock = findStockNode(ptr.getDish().getStockID()[j]);
dishCost = dishCost + stock.getIngredient().getCost();
}
// System.out.println("calculated dishcost => "+dishCost );
double dishPrice = (1.2)*dishCost;
// System.out.println("dishprice => "+dishPrice);
double profit = dishPrice - dishCost;
// System.out.println("profit => "+profit);
ptr.getDish().setPriceOfDish(dishPrice);
ptr.getDish().setProfit(profit);
ptr = ptr.getNextMenuNode();
}
}
}
/**
* PICK UP LINE OF THE METHOD:
* *are you a decimal? cuz the thought of you
* always floats in my head and the two of use would make double*
*
* **
* This method initializes and populates stockVar which is a hashtable where each index contains a
* linked list with StockNodes.
*
* 1. set and read the inputFile (stock.in):
* a) first integer (on line 1) is the size of StockVar * update stockVarSize AND create the stockVar array *
* b) first integer of next line represents the ingredientID
* i) example: 101 on line 2
* c) use StdIn.readChar() to get rid of the space between the id and the name
* d) the string that follows is the ingredient name (NOTE! --> there are spaces between certain strings)
* i) example: Lettuce
* e) the double on the next line corresponds to the ingredient's cost
* i) example: 3.12 on line 3
* f) the next integer is the stock amount for that ingredient
* i) example: 30 on line 3
*
* 2. create a Ingredient object followed by a StockNode then add to stockVar
* HINT! --> you may use previous methods written to help you!
*
* @param inputFile - the input file with the ingredients and all their information (stock.in)
*/
public void createStockHashTable (String inputFile){
StdIn.setFile(inputFile); // opens inputFile to be read by StdIn
// WRITE YOUR CODE HERE
int size = StdIn.readInt();
stockVar = new StockNode[size];
stockVarSize = size;
while(!StdIn.isEmpty()){
int id = StdIn.readInt();
char space = StdIn.readChar();
String name = StdIn.readLine();
double cost = StdIn.readDouble();
char spaces = StdIn.readChar();
int stock = StdIn.readInt();
Ingredient ingredient = new Ingredient(id, name, stock, cost);
StockNode stockNode = new StockNode(ingredient, null);
addStockNode(stockNode);
}
}
/*
* Transaction methods
*/
/**
* This method adds a TransactionNode to the END of the transactions linked list.
* The front of the list is transactionVar.
*
* 1. create a new TransactionNode with the TransactionData paramenter.
* 2. add the TransactionNode at the end of the linked list transactionVar.
*
* @param data - TransactionData node to be added to transactionVar
*/
public void addTransactionNode ( TransactionData data ) { // method adds new transactionNode to the end of LL
// WRITE YOUR CODE HERE
TransactionNode node = new TransactionNode(data, null);
TransactionNode ptr = transactionVar;
if(ptr == null){
transactionVar = node;
}
else{
while(ptr.getNext() != null){
ptr = ptr.getNext();
}
ptr.setNext(node);
}
}
/**
* PICK UP LINE OF THE METHOD:
* are you the break command? cuz everything else stops when I see you
*
* ***
* This method checks if there's enough in stock to prepare a dish.
*
* 1. use findDish() method to find the menuNode node for dishName
*
* 2. retrieve the Dish, then traverse ingredient array within the Dish
*
* 3. return boolean based on whether you can sell the dish or not
* HINT! --> once you determine you can't sell the dish, break and return
*
* @param dishName - String of dish that's being requested
* @param numberOfDishes - int of how many of that dish is being ordered
* @return boolean
*/
public boolean checkDishAvailability (String dishName, int numberOfDishes){
// WRITE YOUR CODE HERE
MenuNode containsDish = findDish(dishName);
int [] arr = containsDish.getDish().getStockID();
for (int i = 0; i<arr.length; i++){
//System.out.println("arr["+i+"] => "+arr[i] );
StockNode stock = findStockNode(containsDish.getDish().getStockID()[i]);
int stocklevel = stock.getIngredient().getStockLevel();
if((stocklevel < numberOfDishes)){
return false;
// break;
}
}
return true; // update the return value
}
/**
* PICK UP LINE OF THE METHOD:
* *if you were a while loop and I were a boolean, we could run
* together forever because I’ll always stay true to you*
*
* ***
* This method simulates a customer ordering a dish. Use the checkDishAvailability() method to check whether the dish can be ordered.
* If the dish cannot be prepared
* - create a TransactionData object of type "order" where the item is the dishName, the amount is the quantity being ordered, and profit is 0 (zero).
* - then add the transaction as an UNsuccessful transaction and,
* - simulate the customer trying to order other dishes in the same category linked list:
* - if the dish that comes right after the dishName can be prepared, great. If not, try the next one and so on.
* - you might have to traverse through the entire category searching for a dish that can be prepared. If you reach the end of the list, start from the beginning until you have visited EVERY dish in the category.
* - It is possible that no dish in the entire category can be prepared.
* - Note: the next dish the customer chooses is always the one that comes right after the one that could not be prepared.
*
* @param dishName - String of dish that's been ordered
* @param quantity - int of how many of that dish has been ordered
*/
public void order (String dishName, int quantity){
// WRITE YOUR CODE HERE
//System.out.println("------------------------------");
// checks if the item can be prepared
boolean canPrepare = checkDishAvailability(dishName, quantity);
if(canPrepare == true){
//System.out.print("order was successful");
double profit = findDish(dishName).getDish().getProfit() * quantity;
TransactionData yourOrder = new TransactionData("order", dishName, quantity,profit, true);
addTransactionNode(yourOrder);
int [] arr = findDish(dishName).getDish().getStockID();
for(int i =0 ; i<arr.length; i++){
updateStock(dishName, findDish(dishName).getDish().getStockID()[i], -quantity );
}
return;
}
else{
// if not prepared then inserts the node as unsuccessfull transaction
TransactionData yourOrder = new TransactionData("order", dishName, quantity,0, false);
addTransactionNode(yourOrder);
//gives other options to the customer
// find the category of the dish and its index in the array
String category = findDish(dishName).getDish().getCategory();
int index = -1;
for(int i = 0; i<categoryVar.length; i++){
if(categoryVar[i].equalsIgnoreCase(category)){
index = i;
}
}
//keeping tracker nodes in the linked list to find the next item that can be prepared
MenuNode front = menuVar[index]; // first node
MenuNode ptr = findDish(dishName); //pointer at the node containing the dish in parameter(initial order)
MenuNode temp = ptr.getNextMenuNode(); //starts at the next node after the ptr
boolean available = false;
//traverse till the last to find a dish that can be prepared
while(temp != null ){
//if not prepared add as a failed transaction
//System.out.println("current dish name after ptr => "+temp.getDish().getName()+ " its availability => "+checkDishAvailability(temp.getDish().getName(), quantity));
//System.out.println("------------------------------");
if(checkDishAvailability(temp.getDish().getName(), quantity) == false){
TransactionData yourFailedOrder = new TransactionData("order", temp.getDish().getName(), quantity,0, false);
addTransactionNode(yourFailedOrder);
//System.out.println("order failed for => "+temp.getDish().getName());
temp = temp.getNextMenuNode();
}
else{
//if prepared then add as successful transaction
// System.out.println("order successful for => "+temp.getDish().getName());
available = true;
double profit = temp.getDish().getProfit() * quantity;
TransactionData yournewOrder = new TransactionData("order",temp.getDish().getName(), quantity,profit, true);
addTransactionNode(yournewOrder);
int [] arr = temp.getDish().getStockID();
for(int i = 0 ; i<arr.length; i++){
updateStock(temp.getDish().getName(), temp.getDish().getStockID()[i],-quantity );
}
return;
}
}
//if not found
if(available == false){
temp = front; // starts from the first node till it reaches ptr
while(temp != ptr ){
// System.out.println("current dish name before ptr => "+temp.getDish().getName() + " its availability => "+checkDishAvailability(temp.getDish().getName(), quantity) );
if(checkDishAvailability(temp.getDish().getName(), quantity) == false){
TransactionData yourFailedOrder = new TransactionData("order", temp.getDish().getName(), quantity,0, false);
addTransactionNode(yourFailedOrder);
// System.out.println("order failed for => "+temp.getDish().getName());
temp = temp.getNextMenuNode();
}
else{
// System.out.println("order successful for => "+temp.getDish().getName());
available = true;
double profit = temp.getDish().getProfit() * quantity;
TransactionData yournewOrder = new TransactionData("order",temp.getDish().getName(), quantity,profit, true);
addTransactionNode(yournewOrder);
int [] arr = temp.getDish().getStockID();
for(int i =0 ; i<arr.length; i++){
updateStock(temp.getDish().getName(), temp.getDish().getStockID()[i],-quantity );
}
break;
}
}
}
}
}
/**
* This method returns the total profit for the day
*
* The profit is computed by traversing the transaction linked list (transactionVar)
* adding up all the profits for the day
*
* @return profit - double value of the total profit for the day
*/
public double profit () {
// WRITE YOUR CODE HERE
TransactionNode ptr = transactionVar;
double prof = 0;
while(ptr != null){
prof = prof + ptr.getData().getProfit();
ptr = ptr.getNext();
}
return prof; // update the return value
}
/**
* This method simulates donation requests, successful or not.
*
* 1. check whether the profit is > 50 and whether there's enough ingredients in stock.
*
* 2. add transaction to transactionVar
*
* @param ingredientName - String of ingredient that's been requested
* @param quantity - int of how many of that ingredient has been ordered
* @return void
*/
public void donation (String ingredientName, int quantity){
// WRITE YOUR CODE HERE
double profit = profit();
int stock = findStockNode(ingredientName).getIngredient().getStockLevel();
//System.out.println("stock => "+stock + " quantity => "+ quantity);
if((profit > 50) && (stock >= quantity)) {
TransactionData data = new TransactionData("donation", ingredientName, quantity, 0, true);
addTransactionNode(data);
StockNode ingredient = findStockNode(ingredientName);
updateStock(ingredientName, ingredient.getIngredient().getID(), -quantity);
}
else{
TransactionData data = new TransactionData("donation", ingredientName, quantity, 0, false);
addTransactionNode(data);
}
}
/**
* This method simulates restock orders
*
* 1. check whether the profit is sufficient to pay for the total cost of ingredient
* a) (how much each ingredient costs) * (quantity)
* b) if there is enough profit, adjust stock and profit accordingly
*
* 2. add transaction to transactionVar
*
* @param ingredientName - ingredient that's been requested
* @param quantity - how many of that ingredient needs to be ordered
*/
public void restock (String ingredientName, int quantity){
// WRITE YOUR CODE HERE
double profit = profit();
double cost = findStockNode(ingredientName).getIngredient().getCost();
double total = quantity * cost;
if(profit >= total){
TransactionData data = new TransactionData("restock", ingredientName, quantity, -total, true);
addTransactionNode(data);
updateStock(ingredientName, findStockNode(ingredientName).getIngredient().getID(), quantity);
}
else {
TransactionData data = new TransactionData("restock", ingredientName, quantity, 0, false);
addTransactionNode(data);
}
}
/*
* Seat guests/customers methods
*/
/**
* Method to populate tables (which is a 1D integer array) based upon input file
*
* The input file is formatted as follows:
* - an integer t contains the number of tables
* - t lines containing number of rows * seats per row for each table
*
* @param inputFile - tables1.in (contains all the tables in the RUHungry restaurant)
* @return void (aka nothing)
*/
public void createTables ( String inputFile ) {
StdIn.setFile(inputFile);
int numberOfTables = StdIn.readInt();
tableSeats = new int[numberOfTables];
tables = new Party[numberOfTables];
for ( int t = 0; t < numberOfTables; t++ ) {
tableSeats[t] = StdIn.readInt() * StdIn.readInt();
}
}
/**
* PICK UP LINE OF THE METHOD:
* *are you a linked list? cuz nothing could stock up to you and
* you’re pretty queue(te)*
*
* ***
* This method simulates seating guests at tables. You are guaranteed to be able to sit everyone from the waitingQueue eventually.
*
* 1. initialize a tables array for party that are currently sitting
*
* 2. initialize leftQueueVar a Party queue that represents the people that have left the restaurant
*
* 3. while there are parties waiting to be sat:
* - Starting from index 0 (zero), seat the next party in the first available table that fits their party.
* - If there is no available table for the next party, kick a party out from the tables array:
* 1. starting at index 0 (zero), find the first table big enough to hold the next party in line.
* 2. remove the current party, add them to the leftQueueVar.
* 3. seat the next party in line.
*
* Parallel arrays: tableSeats[i] refers to tables[i]. If tableSeats[i] is 3 then a party with 3 or less people can sit at tables[i].
* tableSeats contains the number of seats per table.
* tables contains the Party object currently at the table.
*
* Note: After everyone has been seated (waitingQueue is empty), remove all the parties from tables and add then to the leftQueueVar.
*
* @param waitingQueue - queue containing parties waiting to be seated (each element in queue is a Party <-- you are given this class!)
*/
public void seatAllGuests ( Queue<Party> waitingQueue ) {
// WRITE YOUR CODE HERE
}
/**
* Prints all states of the restaurant.
*
* Edit this method if you wish.
*/
public void printRestaurant() {
// 1. Print out menu
StdOut.println("Menu:");
if (categoryVar != null) {
for (int i=0; i < categoryVar.length; i++) {
StdOut.print(categoryVar[i] + ":");
StdOut.println();
MenuNode ptr = menuVar[i];
while (ptr != null) {
StdOut.print(ptr.getDish().getName() + " Price: $" +
((Math.round(ptr.getDish().getPriceOfDish() * 100.0)) / 100.0) + " Profit: $" + ((Math.round(ptr.getDish().getProfit() * 100.0)) / 100.0));
StdOut.println();
ptr = ptr.getNextMenuNode();
}
StdOut.println();
}
}
else {
StdOut.println("Empty - categoryVar is null.");
}
// 2. Print out stock
StdOut.println("Stock:");
if (stockVar != null) {
for (int i=0; i < 10; i++) {
StdOut.println("Index " + i);
StockNode ptr = stockVar[i];
while (ptr != null) {
StdOut.print(ptr.getIngredient().getName() + " ID: " + ptr.getIngredient().getID() + " Price: " +
((Math.round(ptr.getIngredient().getCost() *100.0)) / 100.0) + " Stock Level: " + ptr.getIngredient().getStockLevel());
StdOut.println();
ptr = ptr.getNextStockNode();
}
StdOut.println();
}
}
else {
StdOut.println("Empty - stockVar is null.");
}
// 3. Print out transactions
StdOut.println("Transactions:");
if (transactionVar != null) {
TransactionNode ptr = transactionVar;
int successes = 0;
int failures = 0;
while (ptr != null) {
String type = ptr.getData().getType();
String item = ptr.getData().getItem();
int amount = ptr.getData().getAmount();
double profit = ptr.getData().getProfit();
boolean success = ptr.getData().getSuccess();
if (success == true){
successes += 1;
}
else if (success == false){
failures += 1;
}
StdOut.println("Type: " + type + ", Name: " + item + ", Amount: " + amount + ", Profit: $" + ((Math.round(profit * 100.0)) / 100.0) + ", Was it a Success? " + success);
ptr = ptr.getNext();
}
StdOut.println("Total number of successful transactions: " + successes);
StdOut.println("Total number of unsuccessful transactions: " + failures);
StdOut.println("Total profit remaining: $" + ((Math.round(profit() * 100.0)) / 100.0));
}
else {
StdOut.println("Empty - transactionVar is null.");
}
// 4. Print out tables
StdOut.println("Tables and Parties:");
restaurant.Queue<Party> leftQueue = leftQueueVar;
if (leftQueueVar != null) {
StdOut.println(("Parties in order of leaving:"));
int counter = 0;
while (!leftQueue.isEmpty()) {
Party removed = leftQueue.dequeue();
counter += 1;
StdOut.println(counter + ": " + removed.getName());
}
}
else {
StdOut.println("Empty -- leftQueueVar is empty");
}
}
}