|
@@ -12,7 +12,7 @@
|
12
|
12
|
public class TicketMachine
|
13
|
13
|
{
|
14
|
14
|
// The price of a ticket from this machine.
|
15
|
|
- private int price;
|
|
15
|
+ private int price = 500;
|
16
|
16
|
// The amount of money entered by a customer so far.
|
17
|
17
|
private int balance;
|
18
|
18
|
// The total amount of money collected by this machine.
|
|
@@ -25,7 +25,7 @@ public class TicketMachine
|
25
|
25
|
*/
|
26
|
26
|
public TicketMachine(int ticketCost)
|
27
|
27
|
{
|
28
|
|
- price = ticketCost;
|
|
28
|
+ int price = ticketCost;
|
29
|
29
|
balance = 0;
|
30
|
30
|
total = 0;
|
31
|
31
|
}
|
|
@@ -52,7 +52,13 @@ public class TicketMachine
|
52
|
52
|
*/
|
53
|
53
|
public void insertMoney(int amount)
|
54
|
54
|
{
|
55
|
|
- balance = balance + amount;
|
|
55
|
+ if(amount > 0) {
|
|
56
|
+ balance = balance + amount;
|
|
57
|
+ }
|
|
58
|
+ else {
|
|
59
|
+ System.out.println("Use a positive amount: " +
|
|
60
|
+ amount);
|
|
61
|
+ }
|
56
|
62
|
}
|
57
|
63
|
|
58
|
64
|
/**
|
|
@@ -62,17 +68,40 @@ public class TicketMachine
|
62
|
68
|
*/
|
63
|
69
|
public void printTicket()
|
64
|
70
|
{
|
65
|
|
- // Simulate the printing of a ticket.
|
66
|
|
- System.out.println("##################");
|
67
|
|
- System.out.println("# The BlueJ Line");
|
68
|
|
- System.out.println("# Ticket");
|
69
|
|
- System.out.println("# " + price + " cents.");
|
70
|
|
- System.out.println("##################");
|
71
|
|
- System.out.println();
|
|
71
|
+ if(balance >= price) {
|
|
72
|
+ // Simulate the printing of a ticket.
|
|
73
|
+ System.out.println("##################");
|
|
74
|
+ System.out.println("# The BlueJ Line");
|
|
75
|
+ System.out.println("# Ticket");
|
|
76
|
+ System.out.println("# " + price + " cents.");
|
|
77
|
+ System.out.println("##################");
|
|
78
|
+ System.out.println();
|
|
79
|
+ // Update the total collected with the price.
|
|
80
|
+ total = total + price;
|
|
81
|
+ // Reduce the balance by the price.
|
|
82
|
+ balance = balance - price;
|
|
83
|
+ }
|
|
84
|
+ else {
|
|
85
|
+ System.out.println("You must insert at least: " +
|
|
86
|
+ (price - balance) + " more cents.");
|
|
87
|
+ }
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ public void showPrice()
|
|
91
|
+ {
|
|
92
|
+ System.out.println("The price of a ticket is " + price + " cents.");
|
|
93
|
+ }
|
72
|
94
|
|
73
|
|
- // Update the total collected with the balance.
|
74
|
|
- total = total + balance;
|
75
|
|
- // Clear the balance.
|
|
95
|
+ /**
|
|
96
|
+ * Return the money in the balance.
|
|
97
|
+ * The balance is cleared.
|
|
98
|
+ */
|
|
99
|
+ public int refundBalance()
|
|
100
|
+ {
|
|
101
|
+ int amountToRefund;
|
|
102
|
+ amountToRefund = balance;
|
76
|
103
|
balance = 0;
|
|
104
|
+ return amountToRefund;
|
77
|
105
|
}
|
|
106
|
+
|
78
|
107
|
}
|