|
@@ -13,12 +13,16 @@ public class TicketMachine
|
13
|
13
|
{
|
14
|
14
|
// The price of a ticket from this machine.
|
15
|
15
|
private int price;
|
|
16
|
+ // The amount that you get off with coupon
|
|
17
|
+ private int coupon;
|
16
|
18
|
// The amount of money entered by a customer so far.
|
17
|
19
|
private int balance;
|
18
|
20
|
// The total amount of money collected by this machine.
|
19
|
21
|
private int total;
|
|
22
|
+ // The total amount of tickets sold
|
|
23
|
+ private int ticketsSold = 0;
|
20
|
24
|
|
21
|
|
- private int score;
|
|
25
|
+ //private int score;
|
22
|
26
|
|
23
|
27
|
/**
|
24
|
28
|
* Create a machine that issues tickets of the given price.
|
|
@@ -47,7 +51,7 @@ public class TicketMachine
|
47
|
51
|
}
|
48
|
52
|
|
49
|
53
|
public void prompt() {
|
50
|
|
- System.out.println("Please inser the correct amount of money");
|
|
54
|
+ System.out.println("Please insert the correct amount of money");
|
51
|
55
|
}
|
52
|
56
|
|
53
|
57
|
public void showPrice(){
|
|
@@ -57,17 +61,13 @@ public class TicketMachine
|
57
|
61
|
public void discount(int amount){
|
58
|
62
|
price = price - amount;
|
59
|
63
|
}
|
60
|
|
-
|
61
|
|
- public void increase(int points){
|
62
|
|
- score += points;
|
63
|
|
- }
|
64
|
64
|
|
65
|
65
|
/**
|
66
|
66
|
* Return the price of a ticket.
|
67
|
67
|
*/
|
68
|
68
|
public int getPrice()
|
69
|
69
|
{
|
70
|
|
- return price;
|
|
70
|
+ return coupon != 0 ? price - coupon : price;
|
71
|
71
|
}
|
72
|
72
|
|
73
|
73
|
/**
|
|
@@ -78,19 +78,17 @@ public class TicketMachine
|
78
|
78
|
{
|
79
|
79
|
return balance;
|
80
|
80
|
}
|
|
81
|
+
|
|
82
|
+ public void insertCoupon(int couponAmount){
|
|
83
|
+ coupon = couponAmount;
|
|
84
|
+ }
|
81
|
85
|
|
82
|
86
|
|
83
|
87
|
/**
|
84
|
88
|
* Receive an amount of money in cents from a customer.
|
85
|
89
|
*/
|
86
|
90
|
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
|
|
-
|
|
91
|
+ balance += amount;
|
94
|
92
|
}
|
95
|
93
|
|
96
|
94
|
/**
|
|
@@ -100,29 +98,39 @@ public class TicketMachine
|
100
|
98
|
*/
|
101
|
99
|
public void printTicket() {
|
102
|
100
|
|
103
|
|
- if(balance >= price) {
|
|
101
|
+ int salePrice = coupon != 0 ? price - coupon : price;
|
|
102
|
+
|
|
103
|
+ int change = balance - salePrice;
|
|
104
|
+
|
|
105
|
+ if(change >= 0) {
|
104
|
106
|
// Simulate the printing of a ticket.
|
105
|
107
|
System.out.println("##################");
|
106
|
108
|
System.out.println("# The BlueJ Line");
|
107
|
109
|
System.out.println("# Ticket");
|
108
|
|
- System.out.println("# " + price + " cents.");
|
|
110
|
+ System.out.println("# " + salePrice + " cents.");
|
|
111
|
+ if(coupon != 0){
|
|
112
|
+ System.out.println("# You have been discounted " + coupon + " cents.");
|
|
113
|
+ }
|
109
|
114
|
System.out.println("##################");
|
|
115
|
+ if(change > 0){
|
|
116
|
+ System.out.println("You have been refunded " + change);
|
|
117
|
+ }
|
110
|
118
|
System.out.println();
|
111
|
|
-
|
112
|
119
|
// Update the total collected with the balance.
|
113
|
|
- total = total + balance;
|
|
120
|
+ total = price;
|
114
|
121
|
// Clear the balance.
|
115
|
|
- balance = 0;
|
|
122
|
+ balance = 0;
|
|
123
|
+ coupon = 0;
|
|
124
|
+ ticketsSold++;
|
|
125
|
+
|
116
|
126
|
} else {
|
117
|
127
|
System.out.println("You must insert at least: " +
|
118
|
|
- (price - balance) + " cents.");
|
119
|
|
-
|
|
128
|
+ (salePrice - balance) + " cents to purchase a ticket.");
|
120
|
129
|
}
|
121
|
130
|
}
|
122
|
|
- public int refundBalance() {
|
123
|
|
- int amountToRefund;
|
124
|
|
- amountToRefund = balance;
|
|
131
|
+ public void refundBalance() {
|
|
132
|
+ System.out.println(balance + " cents Has just been refunded to you");
|
125
|
133
|
balance = 0;
|
126
|
|
- return amountToRefund;
|
|
134
|
+
|
127
|
135
|
}
|
128
|
136
|
}
|