-
Notifications
You must be signed in to change notification settings - Fork 1
/
VendingMachine.java
104 lines (88 loc) · 3.26 KB
/
VendingMachine.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
public class VendingMachine {
// This is just a list of all the possible things we could stock in our
// VendingMachine - quantity is 0 because these aren't actually stocked;
// it's just a list of possibilities
private static final Snack[] POSSIBLE_SNACKS = {
new Snack("Cheetos", 0.85, 0),
new Snack("Cheez-Its", 0.85, 0),
new Snack("Funions", 0.85, 0),
new Snack("Combos", 0.75, 0),
new Snack("Teddy Grahams", 0.75, 0),
new Snack("Potato Chips", 0.75, 0),
new Snack("Fritos", 0.75, 0),
new Snack("Nacho Doritos", 0.85, 0),
new Snack("Cool Ranch Doritos", 0.85, 0),
new Snack("Sun Chips", 0.85, 0),
new Snack("Snickers", 1.0, 0),
new Snack("Kit Kats", 1.0, 0),
new Snack("Butterfinger", 1.0, 0),
new Snack("Twix", 1.0, 0),
new Snack("Reese's Peanut Butter Cups", 1.25, 0)
};
private static double allCollectedMoneyEver = 0.0;
private final int NUM_ROWS;
private final int NUM_COLS;
private Snack[][] availableItems;
private double depositedMoney;
private double collectedMoney;
public VendingMachine(int rows, int cols, int stockingQuantity) {
depositedMoney = 0.0;
collectedMoney = 0.0;
NUM_ROWS = rows;
NUM_COLS = cols;
availableItems = new Snack[NUM_ROWS][NUM_COLS];
restockSnacks(stockingQuantity);
}
public static double getAllCollectedMoneyEver() {
return allCollectedMoneyEver;
}
public void depositMoney(double amount) {
depositedMoney += amount;
}
public double getDepositedMoney() {
return depositedMoney;
}
public double getCollectedMoney() {
return collectedMoney;
}
// return value (null) indicates error
public String vend(int row, int col) {
Snack item = availableItems[row][col];
if (depositedMoney >= item.getCost()
&& item.takeSnack()) {
depositedMoney -= item.getCost();
collectedMoney += item.getCost();
allCollectedMoneyEver += item.getCost();
return item.getName();
} else {
return null;
}
}
public String toString() {
String output = "";
for (int row = 0; row < NUM_ROWS; ++row) {
for (int col = 0; col < NUM_COLS; ++col) {
output += "Slot " + row + "-" + col + " contains: ";
output += availableItems[row][col].toString() + "\n";
}
}
return output;
}
public double getChange() {
double change = depositedMoney;
depositedMoney = 0.0;
return change;
}
private void restockSnacks(int stockingQuantity) {
for (int row = 0; row < NUM_ROWS; ++row) {
for (int col = 0; col < NUM_COLS; ++col) {
int snackIndex = row * NUM_COLS + col;
Snack snackToStock = POSSIBLE_SNACKS[snackIndex
% POSSIBLE_SNACKS.length];
availableItems[row][col] = new Snack(snackToStock.getName(),
snackToStock.getCost(),
stockingQuantity);
}
}
}
}