-
Notifications
You must be signed in to change notification settings - Fork 0
/
VendingMachineModel.java
171 lines (131 loc) · 4.42 KB
/
VendingMachineModel.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
import java.util.Arrays;
public class VendingMachineModel {
private class Product{
public String name;
public int price;
public int stockLeft;
public Product(String name, int price, int stockLeft){
this.name=name;this.price=price;this.stockLeft=stockLeft;
}
}
public VendingMachineModel(){
currentCredit = 0;
}
private Product[] products;
private int[] productNumbers = {11, 12, 13, 14,
21, 22, 23, 24,
31, 32, 33, 34,
41, 42, 43, 44};
private int[] changeRemaining;
public int[] change = {1, 2, 5, 10, 20, 50, 100, 200};
public int getPositionOfCoin(int coin){
//to find out how many 2p pieces we have left
//we need to find which index of the array "changeRemaining"
//refers to 2p pieces.
//The bijection is set up using the array change.
//This function returns the mapping from coin to array index.
return Arrays.binarySearch(change, coin);
}
public int getPositionOfProduct(int product){
return Arrays.binarySearch(productNumbers, product);
}
public Product getProductWithChoice(int choice){
int index = getPositionOfProduct(choice);
return products[index];
}
public String getNameOfProductWithChoice(int choice){
return getProductWithChoice(choice).name;
}
//---------------------------------------------------------------------------------------------
// Setting up THE MACHINE.
//---------------------------------------------------------------------------------------------
public void initialMachineStock(int[] changeInserted, int[] productsInserted){
changeRemaining = changeInserted;
products = new Product[productsInserted.length];
for(int i=0; i<productsInserted.length; i++){
//initial prices are 0, prices set separately afterwards.
products[i] = new Product(Integer.toString(productNumbers[i]), 0, productsInserted[i]);
}
}
public void setMachinePrices(int[] prices){
for(int i=0; i<productNumbers.length; i++){
Product item = products[i];
products[i] = new Product(item.name, prices[i], item.stockLeft);
}
}
public void reloadMachineChange(int[] changeInserted){
for(int i=0; i<change.length; i++){
changeRemaining[i]+=changeInserted[i];
}
}
public void reloadMachineProducts(int[] productsInserted){
for(int i=0; i<productNumbers.length; i++){
Product item = products[i];
products[i] = new Product(item.name, item.price, item.stockLeft + productsInserted[i]);
}
}
//---------------------------------------------------------------------------------------------
// Calculating credit
//---------------------------------------------------------------------------------------------
//Private so that it can't be increased without adding
//the coins to the change pile also.
private int currentCredit;
public int currentCredit(){
return currentCredit;
}
public void addCredit(int coin){
currentCredit += coin;
int index = getPositionOfCoin(coin);
changeRemaining[index] += 1;
}
public int[] returnCredit(){
int[] toReturn = new int[change.length];
for(int i = change.length-1; currentCredit>0 && i>=0;i--){
while(changeRemaining[i]>0 && change[i]<=currentCredit){
toReturn[i]++;
changeRemaining[i]--;
currentCredit-=change[i];
}
}
return toReturn;
}
//---------------------------------------------------------------------------------------------
// Purchasing an item
//---------------------------------------------------------------------------------------------
public int userBuys(int choice){
//returns: 2 = notEnough change
//returns: 1 = succesful
//returns: 0 = out of stock
//returns: <0 = insufficient funds
Product toBuy = getProductWithChoice(choice);
if(currentCredit>= toBuy.price && toBuy.stockLeft>0){
if(couldGiveChangeAfterBuying(choice)){
currentCredit-=toBuy.price;
toBuy.stockLeft--;
return 1;
}
else{
return 2;
}
}
else{
if(toBuy.stockLeft==0) return 0;
else{//in stock but insufficient credit.
return -1 * toBuy.price;
}
}
}
public Boolean couldGiveChangeAfterBuying(int choice){
Product toBuy = getProductWithChoice(choice);
int newCredit = currentCredit - toBuy.price;
int[] tempChangeRemaining = new int[change.length];
System.arraycopy(changeRemaining, 0, tempChangeRemaining, 0, changeRemaining.length);
for(int i = change.length-1; newCredit>0 && i>=0; i--){
while(tempChangeRemaining[i]>0 && change[i]<=newCredit){
tempChangeRemaining[i]--;
newCredit-=change[i];
}
}
return newCredit == 0;
}
}