|
@@ -1,3 +1,5 @@
|
|
1
|
+package NaiveTicket;
|
|
2
|
+
|
1
|
3
|
/**
|
2
|
4
|
* TicketMachine models a naive ticket machine that issues
|
3
|
5
|
* flat-fare tickets.
|
|
@@ -9,7 +11,7 @@
|
9
|
11
|
* @author David J. Barnes and Michael Kolling
|
10
|
12
|
* @version 2008.03.30
|
11
|
13
|
*/
|
12
|
|
-public class TicketMachine
|
|
14
|
+class TicketMachine
|
13
|
15
|
{
|
14
|
16
|
// The price of a ticket from this machine.
|
15
|
17
|
private int price;
|
|
@@ -23,6 +25,7 @@ public class TicketMachine
|
23
|
25
|
* Note that the price must be greater than zero, and there
|
24
|
26
|
* are no checks to ensure this.
|
25
|
27
|
*/
|
|
28
|
+
|
26
|
29
|
public TicketMachine(int ticketCost)
|
27
|
30
|
{
|
28
|
31
|
price = ticketCost;
|
|
@@ -52,7 +55,11 @@ public class TicketMachine
|
52
|
55
|
*/
|
53
|
56
|
public void insertMoney(int amount)
|
54
|
57
|
{
|
55
|
|
- balance = balance + amount;
|
|
58
|
+ if(amount > 0) {
|
|
59
|
+ balance = balance + amount;
|
|
60
|
+ } else {
|
|
61
|
+ System.out.println("Use an amount greater than zero: " + amount);
|
|
62
|
+ }
|
56
|
63
|
}
|
57
|
64
|
|
58
|
65
|
/**
|
|
@@ -62,17 +69,52 @@ public class TicketMachine
|
62
|
69
|
*/
|
63
|
70
|
public void printTicket()
|
64
|
71
|
{
|
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();
|
|
72
|
+ int amountLeftToPay;
|
|
73
|
+
|
|
74
|
+ amountLeftToPay = price - balance;
|
|
75
|
+ if (amountLeftToPay <= 0) {
|
|
76
|
+ // Simulate the printing of a ticket.
|
|
77
|
+ System.out.println("##################");
|
|
78
|
+ System.out.println("# The BlueJ Line");
|
|
79
|
+ System.out.println("# Ticket");
|
|
80
|
+ System.out.println("# " + price + " cents.");
|
|
81
|
+ System.out.println("##################");
|
|
82
|
+ System.out.println();
|
|
83
|
+
|
|
84
|
+ // Update the total collected with the balance.
|
|
85
|
+ total = total + price;
|
|
86
|
+ // Reduce balance
|
|
87
|
+ balance = balance - price;
|
|
88
|
+ } else {
|
|
89
|
+ System.out.println("Please insert " + amountLeftToPay + " to receive ticket.");
|
|
90
|
+ }
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ public void prompt()
|
|
94
|
+ {
|
|
95
|
+ System.out.println("Please insert the correct amount of money.");
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ public void showPrice()
|
|
99
|
+ {
|
|
100
|
+ System.out.println("The price of a ticket is " + price + " cents.");
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ public void empty()
|
|
104
|
+ {
|
|
105
|
+ total = 0;
|
|
106
|
+ }
|
72
|
107
|
|
73
|
|
- // Update the total collected with the balance.
|
74
|
|
- total = total + balance;
|
75
|
|
- // Clear the balance.
|
|
108
|
+ public void setPrice(int newPrice)
|
|
109
|
+ {
|
|
110
|
+ price = newPrice;
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ public int refundBalance()
|
|
114
|
+ {
|
|
115
|
+ int amountToRefund;
|
|
116
|
+ amountToRefund = balance;
|
76
|
117
|
balance = 0;
|
|
118
|
+ return amountToRefund;
|
77
|
119
|
}
|
78
|
120
|
}
|