|
@@ -9,20 +9,25 @@
|
9
|
9
|
* @author David J. Barnes and Michael Kolling
|
10
|
10
|
* @version 2008.03.30
|
11
|
11
|
*/
|
|
12
|
+import java.util.Scanner;
|
|
13
|
+
|
12
|
14
|
public class TicketMachine
|
13
|
15
|
{
|
14
|
16
|
// The price of a ticket from this machine.
|
15
|
17
|
private int price;
|
16
|
|
- // The amount that you get off with coupon
|
17
|
|
- private int coupon;
|
18
|
|
- // The amount of money entered by a customer so far.
|
|
18
|
+
|
19
|
19
|
private int balance;
|
20
|
20
|
// The total amount of money collected by this machine.
|
21
|
21
|
private int total;
|
|
22
|
+
|
|
23
|
+ // The amount that you get off with coupon
|
|
24
|
+ private int discount;
|
22
|
25
|
// The total amount of tickets sold
|
23
|
26
|
private int ticketsSold = 0;
|
24
|
|
-
|
25
|
27
|
//private int score;
|
|
28
|
+ private int score;
|
|
29
|
+
|
|
30
|
+ private int attempts;
|
26
|
31
|
|
27
|
32
|
/**
|
28
|
33
|
* Create a machine that issues tickets of the given price.
|
|
@@ -36,18 +41,51 @@ public class TicketMachine
|
36
|
41
|
total = 0;
|
37
|
42
|
}
|
38
|
43
|
|
39
|
|
- public TicketMachine(){
|
40
|
|
- price = 0;
|
41
|
|
- balance = 0;
|
42
|
|
- total = 0;
|
43
|
|
- }
|
44
|
|
-
|
|
44
|
+ /*
|
|
45
|
+ **public TicketMachine(){
|
|
46
|
+ ** price = 0;
|
|
47
|
+ ** balance = 0;
|
|
48
|
+ ** total = 0;
|
|
49
|
+ **}
|
|
50
|
+ */
|
|
51
|
+
|
45
|
52
|
public void setPrice(int ticketCost) {
|
46
|
53
|
price = ticketCost;
|
47
|
54
|
}
|
48
|
55
|
|
|
56
|
+ /**
|
|
57
|
+ * Return the price of a ticket.
|
|
58
|
+ */
|
|
59
|
+ public int getPrice(){
|
|
60
|
+ return price;
|
|
61
|
+ }
|
|
62
|
+
|
49
|
63
|
public void empty(){
|
50
|
|
- total = 0;
|
|
64
|
+
|
|
65
|
+ while(attempts < 5){
|
|
66
|
+ int pin = inputHelper("Please enter the Security Pin: ");
|
|
67
|
+ if(pin == 4321){
|
|
68
|
+ if(total > 0) {
|
|
69
|
+ System.out.println("*You have just emptied out the machine & received " + total + " Cents");
|
|
70
|
+ total = 0;
|
|
71
|
+ } else {
|
|
72
|
+ System.out.println("*There is no money in the machine...");
|
|
73
|
+ }
|
|
74
|
+ attempts = 0;
|
|
75
|
+ break;
|
|
76
|
+ } else {
|
|
77
|
+ attempts++;
|
|
78
|
+ System.out.println("You have Entered the incorrect Pin. " + (5 - attempts) + " Attempts remaining");
|
|
79
|
+ }
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ if(attempts >= 5){
|
|
83
|
+ System.out.print("This Machine is currently locked out. Please contact your administator.\n");
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
51
|
89
|
}
|
52
|
90
|
|
53
|
91
|
public void prompt() {
|
|
@@ -62,12 +100,8 @@ public class TicketMachine
|
62
|
100
|
price = price - amount;
|
63
|
101
|
}
|
64
|
102
|
|
65
|
|
- /**
|
66
|
|
- * Return the price of a ticket.
|
67
|
|
- */
|
68
|
|
- public int getPrice()
|
69
|
|
- {
|
70
|
|
- return coupon != 0 ? price - coupon : price;
|
|
103
|
+ public int getCustomerPrice(){
|
|
104
|
+ return discount != 0 ? price - discount : price;
|
71
|
105
|
}
|
72
|
106
|
|
73
|
107
|
/**
|
|
@@ -80,7 +114,7 @@ public class TicketMachine
|
80
|
114
|
}
|
81
|
115
|
|
82
|
116
|
public void insertCoupon(int couponAmount){
|
83
|
|
- coupon = couponAmount;
|
|
117
|
+ discount = couponAmount;
|
84
|
118
|
}
|
85
|
119
|
|
86
|
120
|
|
|
@@ -88,7 +122,11 @@ public class TicketMachine
|
88
|
122
|
* Receive an amount of money in cents from a customer.
|
89
|
123
|
*/
|
90
|
124
|
public void insertMoney(int amount) {
|
91
|
|
- balance += amount;
|
|
125
|
+ if(amount > 0){
|
|
126
|
+ balance += amount;
|
|
127
|
+ } else {
|
|
128
|
+ System.out.println("Use a positive amount: " + amount);
|
|
129
|
+ }
|
92
|
130
|
}
|
93
|
131
|
|
94
|
132
|
/**
|
|
@@ -98,7 +136,7 @@ public class TicketMachine
|
98
|
136
|
*/
|
99
|
137
|
public void printTicket() {
|
100
|
138
|
|
101
|
|
- int salePrice = coupon != 0 ? price - coupon : price;
|
|
139
|
+ int salePrice = discount != 0 ? price - discount : price;
|
102
|
140
|
|
103
|
141
|
int change = balance - salePrice;
|
104
|
142
|
|
|
@@ -108,19 +146,18 @@ public class TicketMachine
|
108
|
146
|
System.out.println("# The BlueJ Line");
|
109
|
147
|
System.out.println("# Ticket");
|
110
|
148
|
System.out.println("# " + salePrice + " cents.");
|
111
|
|
- if(coupon != 0){
|
112
|
|
- System.out.println("# You have been discounted " + coupon + " cents.");
|
|
149
|
+ if(discount != 0){
|
|
150
|
+ System.out.println("# You have been discounted " + discount + " cents.");
|
113
|
151
|
}
|
114
|
152
|
System.out.println("##################");
|
115
|
153
|
if(change > 0){
|
116
|
|
- System.out.println("You have been refunded " + change);
|
|
154
|
+ System.out.println("*You have been refunded " + change);
|
117
|
155
|
}
|
118
|
|
- System.out.println();
|
119
|
156
|
// Update the total collected with the balance.
|
120
|
|
- total = price;
|
|
157
|
+ total += salePrice;
|
121
|
158
|
// Clear the balance.
|
122
|
159
|
balance = 0;
|
123
|
|
- coupon = 0;
|
|
160
|
+ discount = 0;
|
124
|
161
|
ticketsSold++;
|
125
|
162
|
|
126
|
163
|
} else {
|
|
@@ -128,9 +165,23 @@ public class TicketMachine
|
128
|
165
|
(salePrice - balance) + " cents to purchase a ticket.");
|
129
|
166
|
}
|
130
|
167
|
}
|
131
|
|
- public void refundBalance() {
|
132
|
|
- System.out.println(balance + " cents Has just been refunded to you");
|
|
168
|
+ public int refundBalance() {
|
|
169
|
+ int amountToRefund;
|
|
170
|
+ System.out.println("*" + balance + " cents Has just been refunded to you");
|
|
171
|
+ if(discount > 0){
|
|
172
|
+ System.out.println("*Your coupon of " + discount + " cents has been now been returned.");
|
|
173
|
+ discount = 0;
|
|
174
|
+ }
|
|
175
|
+
|
|
176
|
+ amountToRefund = balance;
|
133
|
177
|
balance = 0;
|
|
178
|
+ return amountToRefund;
|
134
|
179
|
|
135
|
180
|
}
|
|
181
|
+ public int inputHelper(String message) {
|
|
182
|
+ Scanner input = new Scanner(System.in);
|
|
183
|
+ System.out.print(message);
|
|
184
|
+ return input.nextInt();
|
|
185
|
+ }
|
|
186
|
+
|
136
|
187
|
}
|