|
@@ -28,8 +28,15 @@ public class TicketMachine
|
28
|
28
|
price = ticketCost;
|
29
|
29
|
balance = 0;
|
30
|
30
|
total = 0;
|
|
31
|
+ prompt();
|
|
32
|
+ }
|
|
33
|
+ public TicketMachine()
|
|
34
|
+ {
|
|
35
|
+ price = 1000;
|
|
36
|
+ balance = 0;
|
|
37
|
+ total = 0;
|
|
38
|
+ prompt();
|
31
|
39
|
}
|
32
|
|
-
|
33
|
40
|
/**
|
34
|
41
|
* Return the price of a ticket.
|
35
|
42
|
*/
|
|
@@ -52,6 +59,20 @@ public class TicketMachine
|
52
|
59
|
*/
|
53
|
60
|
public void insertMoney(int amount)
|
54
|
61
|
{
|
|
62
|
+ if (amount > 0)
|
|
63
|
+ {
|
|
64
|
+ balance = balance + amount;
|
|
65
|
+ }
|
|
66
|
+ else
|
|
67
|
+ System.out.println("Use a positive amount: " +amount);
|
|
68
|
+
|
|
69
|
+ if (balance >= price)
|
|
70
|
+ {
|
|
71
|
+ total = total + price;
|
|
72
|
+ balance = balance - price;
|
|
73
|
+ }
|
|
74
|
+ else
|
|
75
|
+ System.out.println("You must insert at least " + (price-balance)+ " more cents.");
|
55
|
76
|
balance = balance + amount;
|
56
|
77
|
}
|
57
|
78
|
|
|
@@ -62,6 +83,10 @@ public class TicketMachine
|
62
|
83
|
*/
|
63
|
84
|
public void printTicket()
|
64
|
85
|
{
|
|
86
|
+ int amountLeftToPay = price - balance;
|
|
87
|
+ if (amountLeftToPay <=0)
|
|
88
|
+ {
|
|
89
|
+
|
65
|
90
|
// Simulate the printing of a ticket.
|
66
|
91
|
System.out.println("##################");
|
67
|
92
|
System.out.println("# The BlueJ Line");
|
|
@@ -69,10 +94,40 @@ public class TicketMachine
|
69
|
94
|
System.out.println("# " + price + " cents.");
|
70
|
95
|
System.out.println("##################");
|
71
|
96
|
System.out.println();
|
|
97
|
+ }
|
|
98
|
+ else
|
|
99
|
+ System.out.println("An additional " +amountLeftToPay+ " cents is required.");
|
72
|
100
|
|
73
|
101
|
// Update the total collected with the balance.
|
74
|
102
|
total = total + balance;
|
75
|
103
|
// Clear the balance.
|
76
|
|
- balance = 0;
|
|
104
|
+ balance = 0;
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ public int getTotal()
|
|
108
|
+ {
|
|
109
|
+ return total;
|
|
110
|
+ }
|
|
111
|
+ public void prompt()
|
|
112
|
+ {
|
|
113
|
+ System.out.println("Please insert the correct amount of money.");
|
|
114
|
+ }
|
|
115
|
+ public void showPrice()
|
|
116
|
+ {
|
|
117
|
+ System.out.println("The price of a ticket is " + price + "cents.");
|
|
118
|
+ }
|
|
119
|
+ public void empty()
|
|
120
|
+ {
|
|
121
|
+ total = 0;
|
|
122
|
+ }
|
|
123
|
+ public void setPrice(int newPrice)
|
|
124
|
+ {
|
|
125
|
+ price = newPrice;
|
|
126
|
+ }
|
|
127
|
+ public int emptyMachine()
|
|
128
|
+ {
|
|
129
|
+ System.out.println("The total is " + total);
|
|
130
|
+ total = 0;
|
|
131
|
+ return total;
|
77
|
132
|
}
|
78
|
133
|
}
|