-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVendingMachineController.java
More file actions
146 lines (99 loc) · 3.71 KB
/
VendingMachineController.java
File metadata and controls
146 lines (99 loc) · 3.71 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
import java.text.DecimalFormat;
public class VendingMachineController {
private VendingMachineViewInterface view;
private VendingMachineModel model;
public VendingMachineController(VendingMachineViewInterface view){
this.view = view;
model = new VendingMachineModel();
}
//---------------------------------------------------------------------------------------------
// Loading Machine Stock
//---------------------------------------------------------------------------------------------
public void populateMachine(){
//Initially 10pounds worth of each coin
int[] coins = {1000, 500, 200, 100, 50, 20, 10, 5};
// int[] coins = {1, 1, 0, 0, 0, 0, 0, 0}; less coins useful for testing of change giving
//Initially 10 of each item. 20 of the cheap itmes, 1 of each expensive one.
int[] products = {20, 20, 20, 20,
10, 10, 10, 10,
10, 10, 10, 10,
1, 1, 1, 1};
model.initialMachineStock(coins, products);
//Pricing is done by row in the machine
int[] productPrices = {50, 50, 50, 50,
75, 75, 75, 75,
100, 100, 100, 100,
150, 150, 150, 150};
model.setMachinePrices(productPrices);
}
public void reloadMachine(){
int[] coins = {1000, 500, 200, 100, 50, 20, 10, 5};
// int[] coins = {1, 1, 0, 0, 0, 0, 0, 0}; less coins useful for testing of change giving
//Initially 10 of each item. 20 of the cheap itmes, 1 of each expensive one.
int[] products = {20, 20, 20, 20,
10, 10, 10, 10,
10, 10, 10, 10,
1, 1, 1, 1};
model.reloadMachineChange(coins);
model.reloadMachineProducts(products);
}
//---------------------------------------------------------------------------------------------
// Choosing an item
//---------------------------------------------------------------------------------------------
// public Boolean coinInserted(Coin coin);
public Boolean coinInserted(int coinVal){
model.addCredit(coinVal);
updateScreenWithCurrentCredit();
return true;
}
//returns the amount of change that should be returned.
public void madeChoiceOfItem(int choice){
int credit = model.currentCredit();
double value = credit / 100.0;
DecimalFormat df = new DecimalFormat("#0.00");
String creditString = "\u00A3"+df.format(value);
if(model.getPositionOfProduct(choice) <0){
view.UpdateScreen("Invalid product selection. Credit" + creditString);
return;
}
int result = model.userBuys(choice);
if(result == 2){
view.UpdateScreen("Please insert exact change!");
returnCoins();
return;
}
if (result==1){
view.DispenseProduct(choice);
returnCoins();
view.UpdateScreen("Vending machine is ready.");
}
else if(result == 0) view.UpdateScreen("Out of Stock. Credit:"+creditString);
else if(result <= -100 ){
double cost = (-1 * result) / 100.0;
view.UpdateScreen("insufficient money. Cost:" + "\u00A3" + df.format(cost)
+ ".Credit:" + creditString);
}
//else = if(-100< result < 0)
else{ view.UpdateScreen("insufficient money. Cost:" + Integer.toString(-1 * result) + "p"
+ ".Credit:" + creditString);
}
}
//---------------------------------------------------------------------------------------------
// Returning Change
//---------------------------------------------------------------------------------------------
public void returnCoins(){
view.GiveChange(model.returnCredit());
}
public String nameOfProductWithChoice(int choice){
return model.getNameOfProductWithChoice(choice);
}
public int[] change(){
return model.change;
}
private void updateScreenWithCurrentCredit(){
int credit = model.currentCredit();
double value = credit / 100.0;
DecimalFormat df = new DecimalFormat("#0.00");
view.UpdateScreen("\u00A3"+df.format(value));
}
}