|
@@ -17,12 +17,18 @@ public class TicketMachine
|
17
|
17
|
private int balance;
|
18
|
18
|
// The total amount of money collected by this machine.
|
19
|
19
|
private int total;
|
20
|
|
-
|
|
20
|
+ public void prompt()
|
|
21
|
+ {
|
|
22
|
+ System.out.println("Please insert the correct amount of money.");
|
|
23
|
+ }
|
|
24
|
+
|
|
25
|
+
|
21
|
26
|
/**
|
22
|
27
|
* Create a machine that issues tickets of the given price.
|
23
|
28
|
* Note that the price must be greater than zero, and there
|
24
|
29
|
* are no checks to ensure this.
|
25
|
30
|
*/
|
|
31
|
+
|
26
|
32
|
public TicketMachine(int ticketCost)
|
27
|
33
|
{
|
28
|
34
|
price = ticketCost;
|
|
@@ -52,7 +58,12 @@ public class TicketMachine
|
52
|
58
|
*/
|
53
|
59
|
public void insertMoney(int amount)
|
54
|
60
|
{
|
55
|
|
- balance = balance + amount;
|
|
61
|
+ if (amount >= 0) {
|
|
62
|
+ balance = balance + amount;
|
|
63
|
+ }
|
|
64
|
+ else {
|
|
65
|
+ System.out.println("Use a positive amount: " + amount);
|
|
66
|
+ }
|
56
|
67
|
}
|
57
|
68
|
|
58
|
69
|
/**
|
|
@@ -62,6 +73,7 @@ public class TicketMachine
|
62
|
73
|
*/
|
63
|
74
|
public void printTicket()
|
64
|
75
|
{
|
|
76
|
+ if (balance >= price) {
|
65
|
77
|
// Simulate the printing of a ticket.
|
66
|
78
|
System.out.println("##################");
|
67
|
79
|
System.out.println("# The BlueJ Line");
|
|
@@ -71,8 +83,21 @@ public class TicketMachine
|
71
|
83
|
System.out.println();
|
72
|
84
|
|
73
|
85
|
// Update the total collected with the balance.
|
74
|
|
- total = total + balance;
|
75
|
|
- // Clear the balance.
|
|
86
|
+ total = total + price;
|
|
87
|
+ // new balance
|
|
88
|
+ balance = balance - price;
|
|
89
|
+ }
|
|
90
|
+ else {
|
|
91
|
+ System.out.println("You must insert at least: " + (price - balance) + " cents.");
|
|
92
|
+ }
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ //Balance life//
|
|
96
|
+ public int refundBalance()
|
|
97
|
+ {
|
|
98
|
+ int amountToRefund;
|
|
99
|
+ amountToRefund = balance;
|
76
|
100
|
balance = 0;
|
|
101
|
+ return amountToRefund;
|
77
|
102
|
}
|
78
|
103
|
}
|