|
@@ -39,6 +39,14 @@ public class TicketMachine
|
39
|
39
|
}
|
40
|
40
|
|
41
|
41
|
/**
|
|
42
|
+ * set the price
|
|
43
|
+ */
|
|
44
|
+ public void setPrice(int ticketCost){
|
|
45
|
+
|
|
46
|
+ price = ticketCost;
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ /**
|
42
|
50
|
* Return the amount of money already inserted for the
|
43
|
51
|
* next ticket.
|
44
|
52
|
*/
|
|
@@ -48,11 +56,54 @@ public class TicketMachine
|
48
|
56
|
}
|
49
|
57
|
|
50
|
58
|
/**
|
|
59
|
+ * return the total
|
|
60
|
+ */
|
|
61
|
+ public int getTotal(){
|
|
62
|
+
|
|
63
|
+ return total;
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ /**
|
|
67
|
+ * reduce the price by amount
|
|
68
|
+ */
|
|
69
|
+ public void discount(int amount){
|
|
70
|
+ price -= amount;
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ /**
|
51
|
74
|
* Receive an amount of money in cents from a customer.
|
52
|
75
|
*/
|
53
|
76
|
public void insertMoney(int amount)
|
54
|
77
|
{
|
55
|
|
- balance = balance + amount;
|
|
78
|
+ if (amount > 0){
|
|
79
|
+ balance = balance + amount;
|
|
80
|
+ }
|
|
81
|
+ else{
|
|
82
|
+ System.out.println("Enter a positive amount: " + amount);
|
|
83
|
+ }
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ /**
|
|
87
|
+ * refund the balance
|
|
88
|
+ */
|
|
89
|
+ public int refundBalance(){
|
|
90
|
+ int amountToReturn = balance;
|
|
91
|
+ balance = 0;
|
|
92
|
+ return amountToReturn;
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ /**
|
|
96
|
+ * print a prompt
|
|
97
|
+ */
|
|
98
|
+ public void prompt(){
|
|
99
|
+ System.out.println("Please insert the correct amount of money");
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ /**
|
|
103
|
+ * show the price of the ticket
|
|
104
|
+ */
|
|
105
|
+ public void showPrice(){
|
|
106
|
+ System.out.println("The price of the ticket " + price + " cents");
|
56
|
107
|
}
|
57
|
108
|
|
58
|
109
|
/**
|
|
@@ -62,17 +113,32 @@ public class TicketMachine
|
62
|
113
|
*/
|
63
|
114
|
public void printTicket()
|
64
|
115
|
{
|
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();
|
|
116
|
+ int amountLeftToPay = price - balance;
|
|
117
|
+ if (balance >= price){
|
|
118
|
+ // Simulate the printing of a ticket.
|
|
119
|
+ System.out.println("##################");
|
|
120
|
+ System.out.println("# The BlueJ Line");
|
|
121
|
+ System.out.println("# Ticket");
|
|
122
|
+ System.out.println("# " + price + " cents.");
|
|
123
|
+ System.out.println("##################");
|
|
124
|
+ System.out.println();
|
72
|
125
|
|
73
|
|
- // Update the total collected with the balance.
|
74
|
|
- total = total + balance;
|
75
|
|
- // Clear the balance.
|
76
|
|
- balance = 0;
|
|
126
|
+ // Update the total collected with the balance.
|
|
127
|
+ total = total + price;
|
|
128
|
+ // Clear the balance.
|
|
129
|
+ balance = balance - price;
|
|
130
|
+ }
|
|
131
|
+ else{
|
|
132
|
+ System.out.println("You must enter atleast: " + (amountLeftToPay) + " cents");
|
|
133
|
+ }
|
|
134
|
+ }
|
|
135
|
+
|
|
136
|
+ /**
|
|
137
|
+ * emptying machine
|
|
138
|
+ */
|
|
139
|
+ public int emptyMachine(){
|
|
140
|
+ int moneyInMachine = total;
|
|
141
|
+ total = 0;
|
|
142
|
+ return moneyInMachine;
|
77
|
143
|
}
|
78
|
144
|
}
|