|
@@ -17,6 +17,8 @@ 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
|
+
|
|
21
|
+ private int score;
|
20
|
22
|
|
21
|
23
|
/**
|
22
|
24
|
* Create a machine that issues tickets of the given price.
|
|
@@ -29,6 +31,36 @@ public class TicketMachine
|
29
|
31
|
balance = 0;
|
30
|
32
|
total = 0;
|
31
|
33
|
}
|
|
34
|
+
|
|
35
|
+ public TicketMachine(){
|
|
36
|
+ price = 0;
|
|
37
|
+ balance = 0;
|
|
38
|
+ total = 0;
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ public void setPrice(int ticketCost) {
|
|
42
|
+ price = ticketCost;
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ public void empty(){
|
|
46
|
+ total = 0;
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ public void prompt() {
|
|
50
|
+ System.out.println("Please inser the correct amount of money");
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ public void showPrice(){
|
|
54
|
+ System.out.println("The price of a ticket is " + price);
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ public void discount(int amount){
|
|
58
|
+ price = price - amount;
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ public void increase(int points){
|
|
62
|
+ score += points;
|
|
63
|
+ }
|
32
|
64
|
|
33
|
65
|
/**
|
34
|
66
|
* Return the price of a ticket.
|
|
@@ -47,12 +79,18 @@ public class TicketMachine
|
47
|
79
|
return balance;
|
48
|
80
|
}
|
49
|
81
|
|
|
82
|
+
|
50
|
83
|
/**
|
51
|
84
|
* Receive an amount of money in cents from a customer.
|
52
|
85
|
*/
|
53
|
|
- public void insertMoney(int amount)
|
54
|
|
- {
|
55
|
|
- balance = balance + amount;
|
|
86
|
+ public void insertMoney(int amount) {
|
|
87
|
+
|
|
88
|
+ if(amount > 0) {
|
|
89
|
+ balance = balance + amount;
|
|
90
|
+ } else {
|
|
91
|
+ System.out.println("Use a positive amount: " + amount);
|
|
92
|
+ }
|
|
93
|
+
|
56
|
94
|
}
|
57
|
95
|
|
58
|
96
|
/**
|
|
@@ -60,19 +98,31 @@ public class TicketMachine
|
60
|
98
|
* Update the total collected and
|
61
|
99
|
* reduce the balance to zero.
|
62
|
100
|
*/
|
63
|
|
- public void printTicket()
|
64
|
|
- {
|
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
|
|
-
|
73
|
|
- // Update the total collected with the balance.
|
74
|
|
- total = total + balance;
|
75
|
|
- // Clear the balance.
|
|
101
|
+ public void printTicket() {
|
|
102
|
+
|
|
103
|
+ if(balance >= price) {
|
|
104
|
+ // Simulate the printing of a ticket.
|
|
105
|
+ System.out.println("##################");
|
|
106
|
+ System.out.println("# The BlueJ Line");
|
|
107
|
+ System.out.println("# Ticket");
|
|
108
|
+ System.out.println("# " + price + " cents.");
|
|
109
|
+ System.out.println("##################");
|
|
110
|
+ System.out.println();
|
|
111
|
+
|
|
112
|
+ // Update the total collected with the balance.
|
|
113
|
+ total = total + balance;
|
|
114
|
+ // Clear the balance.
|
|
115
|
+ balance = 0;
|
|
116
|
+ } else {
|
|
117
|
+ System.out.println("You must insert at least: " +
|
|
118
|
+ (price - balance) + " cents.");
|
|
119
|
+
|
|
120
|
+ }
|
|
121
|
+ }
|
|
122
|
+ public int refundBalance() {
|
|
123
|
+ int amountToRefund;
|
|
124
|
+ amountToRefund = balance;
|
76
|
125
|
balance = 0;
|
|
126
|
+ return amountToRefund;
|
77
|
127
|
}
|
78
|
128
|
}
|