5
5
6
6
public class VendingMachine {
7
7
8
- Double currentAmount = 0.0 ;
9
- Double coinReturnAmount = 0.0 ;
10
- ArrayList <String > returnSlotCoins = new ArrayList <String >();
11
- String display = "" ;
12
- ArrayList <String > itemBinList = new ArrayList <String >();
13
- ArrayList <String > coinList = new ArrayList <String >();
8
+ private Double currentAmount = 0.0 ;
9
+ private Double coinReturnAmount = 0.0 ;
10
+ private ArrayList <String > returnSlotCoins = new ArrayList <String >();
11
+ private String display = "" ;
12
+ private ArrayList <String > itemBinList = new ArrayList <String >();
13
+ private ArrayList <String > coinList = new ArrayList <String >();
14
14
private VendingCalc coinCalc ;
15
15
private VendItem soda ;
16
16
private VendItem chips ;
@@ -23,12 +23,18 @@ public VendingMachine() {
23
23
candy = new Candy ();
24
24
}
25
25
26
+ public void update () {
27
+ setCurrentAmount (coinCalc .calcTotalAmount ());
28
+ updateDisplay ();
29
+ }
30
+
26
31
public ArrayList <String > returnCoins () {
27
- setCoinReturnAmount (getCurrentAmount ());
32
+ returnSlotCoins .addAll (coinList );
33
+ coinList .removeAll (coinList );
28
34
setCurrentAmount (0.00 );
35
+ updateDisplay ();
29
36
30
- returnSlotCoins .addAll (coinList );
31
- return coinList ;
37
+ return returnSlotCoins ;
32
38
}
33
39
34
40
public void insertCoin (String coin , double coinAmount ) {
@@ -39,91 +45,77 @@ public void insertCoin(String coin, double coinAmount) {
39
45
}
40
46
}
41
47
42
- private void coinAdded (String coin , double coinAmount ) {
43
- coinList .add (coin );
44
- coinCalc .insertCoin (coinAmount );
45
- }
46
-
47
- private void pennyFallsToReturnSlot (String coin , double coinAmount ) {
48
- returnSlotCoins .add (coin );
49
- setCoinReturnAmount (coinAmount );
50
- }
51
-
52
- private boolean coinIsPenny (String coin ) {
53
- return coin .equals ("Penny" );
54
- }
55
-
56
- public void update () {
57
- setCurrentAmount (coinCalc .calcTotalAmount ());
58
- updateDisplay ();
59
- }
60
-
61
48
public void sodaButton () {
62
- itemBinList = soda .vend (itemBinList , getCurrentAmount () );
49
+ itemBinList = soda .vend (itemBinList , currentAmount );
63
50
updateChangeAmount ();
64
51
}
65
52
66
53
public void chipsButton () {
67
- itemBinList = chips .vend (itemBinList , getCurrentAmount () );
54
+ itemBinList = chips .vend (itemBinList , currentAmount );
68
55
updateChangeAmount ();
69
56
}
70
57
71
58
public void candyButton () {
72
- itemBinList = candy .vend (itemBinList , getCurrentAmount () );
59
+ itemBinList = candy .vend (itemBinList , currentAmount );
73
60
updateChangeAmount ();
74
61
}
75
62
76
- private void updateDisplay () {
77
- NumberFormat nf = NumberFormat .getInstance ();
78
- nf .setMinimumFractionDigits (2 );
79
- String stringConversion = nf .format (getCurrentAmount ());
80
- setDisplay ("$" + stringConversion );
81
- }
82
-
83
- private void updateChangeAmount () {
84
- if (getCurrentAmount () >= 1.25 ) {
85
- setCurrentAmount (getCurrentAmount () - 1.25 );
86
- }
87
- }
88
-
89
63
public Double getCoinReturnAmount () {
90
64
return coinReturnAmount ;
91
65
}
92
66
93
- public void setCoinReturnAmount (Double coinReturnAmount ) {
94
- this .coinReturnAmount = coinReturnAmount ;
95
- }
96
-
97
67
public ArrayList <String > getReturnSlotCoins () {
98
68
return returnSlotCoins ;
99
69
}
100
70
101
- public void setReturnSlotCoins (ArrayList <String > returnSlotCoins ) {
102
- this .returnSlotCoins = returnSlotCoins ;
103
- }
104
-
105
71
public String display () {
106
72
return display ;
107
73
}
108
74
109
- public void setDisplay (String display ) {
110
- this .display = display ;
111
- }
112
-
113
75
public ArrayList <String > getItemBinList () {
114
76
return itemBinList ;
115
77
}
116
78
117
- public void setItemBinList ( ArrayList < String > itemBinList ) {
118
- this .itemBinList = itemBinList ;
79
+ private void setDisplay ( String display ) {
80
+ this .display = display ;
119
81
}
120
-
82
+
121
83
private void setCurrentAmount (Double currentAmount ) {
122
84
this .currentAmount = currentAmount ;
123
85
}
124
86
125
- private Double getCurrentAmount () {
126
- return currentAmount ;
87
+ private void updateDisplay () {
88
+ NumberFormat nf = NumberFormat .getInstance ();
89
+ nf .setMinimumFractionDigits (2 );
90
+ String stringConversion = nf .format (currentAmount );
91
+ setDisplay ("$" + stringConversion );
92
+ }
93
+
94
+ private void updateChangeAmount () {
95
+ if (currentAmount >= 1.25 ) {
96
+ setCurrentAmount (currentAmount - 1.25 );
97
+ coinList .remove (0 );
98
+ coinList .remove (1 );
99
+ coinList .remove (2 );
100
+ }
101
+ }
102
+
103
+ private void setCoinReturnAmount (Double coinReturnAmount ) {
104
+ this .coinReturnAmount = coinReturnAmount ;
127
105
}
128
106
107
+ private void coinAdded (String coin , double coinAmount ) {
108
+ coinList .add (coin );
109
+ coinCalc .insertCoin (coinAmount );
110
+ }
111
+
112
+ private void pennyFallsToReturnSlot (String coin , double coinAmount ) {
113
+ returnSlotCoins .add (coin );
114
+ setCoinReturnAmount (coinAmount );
115
+ }
116
+
117
+ private boolean coinIsPenny (String coin ) {
118
+ return coin .equals ("Penny" );
119
+ }
120
+
129
121
}
0 commit comments