|
@@ -23,9 +23,9 @@ public class TicketMachine
|
23
|
23
|
* Note that the price must be greater than zero, and there
|
24
|
24
|
* are no checks to ensure this.
|
25
|
25
|
*/
|
26
|
|
- public TicketMachine(int ticketCost)
|
|
26
|
+ public TicketMachine()
|
27
|
27
|
{
|
28
|
|
- price = ticketCost;
|
|
28
|
+ price = 1000;
|
29
|
29
|
balance = 0;
|
30
|
30
|
total = 0;
|
31
|
31
|
}
|
|
@@ -47,12 +47,23 @@ public class TicketMachine
|
47
|
47
|
return balance;
|
48
|
48
|
}
|
49
|
49
|
|
|
50
|
+ public void setPrice(int newPrice)
|
|
51
|
+ {
|
|
52
|
+ price = newPrice;
|
|
53
|
+ }
|
|
54
|
+
|
50
|
55
|
/**
|
51
|
56
|
* Receive an amount of money in cents from a customer.
|
52
|
57
|
*/
|
53
|
58
|
public void insertMoney(int amount)
|
54
|
|
- {
|
55
|
|
- balance = balance + amount;
|
|
59
|
+
|
|
60
|
+ {if(amount >= 0) {
|
|
61
|
+ balance = balance + amount;
|
|
62
|
+ }
|
|
63
|
+ else {
|
|
64
|
+ System.out.println("Use a positive amount: " +
|
|
65
|
+ amount);
|
|
66
|
+ }
|
56
|
67
|
}
|
57
|
68
|
|
58
|
69
|
/**
|
|
@@ -62,17 +73,45 @@ public class TicketMachine
|
62
|
73
|
*/
|
63
|
74
|
public void printTicket()
|
64
|
75
|
{
|
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();
|
|
76
|
+ int amountLeftToPay = price - balance;
|
|
77
|
+ if (amountLeftToPay >= 0){
|
|
78
|
+ // Simulate the printing of a ticket.
|
|
79
|
+ System.out.println("##################");
|
|
80
|
+ System.out.println("# The BlueJ Line");
|
|
81
|
+ System.out.println("# Ticket");
|
|
82
|
+ System.out.println("# " + price + " cents.");
|
|
83
|
+ System.out.println("##################");
|
|
84
|
+ System.out.println();
|
72
|
85
|
|
73
|
|
- // Update the total collected with the balance.
|
74
|
|
- total = total + balance;
|
75
|
|
- // Clear the balance.
|
|
86
|
+ // Update the total collected with the balance.
|
|
87
|
+ total = total + price;
|
|
88
|
+ // Clear the balance.
|
|
89
|
+
|
|
90
|
+ } else {
|
|
91
|
+ System.out.println("You must insert at least " + amountLeftToPay + " cents.");
|
|
92
|
+ }
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ public int refundBalance()
|
|
96
|
+ {
|
|
97
|
+ int amountToRefund = balance;
|
76
|
98
|
balance = 0;
|
|
99
|
+ return amountToRefund;
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ public void prompt()
|
|
103
|
+ {
|
|
104
|
+ System.out.println("Please insert the correct amount of money.");
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ public void showPrice()
|
|
108
|
+ {
|
|
109
|
+ System.out.println("The price of a ticket is " + price + " cents.");
|
77
|
110
|
}
|
|
111
|
+
|
|
112
|
+ public void emptyMachine()
|
|
113
|
+ {
|
|
114
|
+ total = 0;
|
|
115
|
+ }
|
|
116
|
+
|
78
|
117
|
}
|