|
@@ -17,19 +17,54 @@ 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
|
+ // Exercise 2.15
|
|
21
|
+ private int status;
|
20
|
22
|
|
21
|
23
|
/**
|
22
|
24
|
* Create a machine that issues tickets of the given price.
|
23
|
25
|
* Note that the price must be greater than zero, and there
|
24
|
26
|
* are no checks to ensure this.
|
25
|
27
|
*/
|
26
|
|
- public TicketMachine(int ticketCost)
|
|
28
|
+ public TicketMachine()
|
27
|
29
|
{
|
28
|
|
- price = ticketCost;
|
|
30
|
+ //2.39 set price to 1000 and remove input parameter
|
|
31
|
+ //2.42 constructor with parameter to specify price
|
|
32
|
+ //2.58 Better ticket machine
|
|
33
|
+ price = 0;
|
29
|
34
|
balance = 0;
|
30
|
35
|
total = 0;
|
31
|
36
|
}
|
32
|
37
|
|
|
38
|
+ //public TicketMachine()
|
|
39
|
+ //{
|
|
40
|
+ //2.42 constructor with default value of price
|
|
41
|
+ //price = 500;
|
|
42
|
+ //}
|
|
43
|
+
|
|
44
|
+ /*
|
|
45
|
+ * Exercise 2.30/2.41: Assign a value to the price field
|
|
46
|
+ */
|
|
47
|
+ public void setPrice(int ticketCost)
|
|
48
|
+ {
|
|
49
|
+ price = ticketCost;
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ /*
|
|
53
|
+ * Exercise 2.31: Add a value to the score field
|
|
54
|
+ */
|
|
55
|
+ public void increase(int points)
|
|
56
|
+ {
|
|
57
|
+ int score = points;
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ /*
|
|
61
|
+ * Exercise 2.32: Subtract a value from the price field
|
|
62
|
+ */
|
|
63
|
+ public void discount(int amount)
|
|
64
|
+ {
|
|
65
|
+ price = price - amount;
|
|
66
|
+ }
|
|
67
|
+
|
33
|
68
|
/**
|
34
|
69
|
* Return the price of a ticket.
|
35
|
70
|
*/
|
|
@@ -52,7 +87,55 @@ public class TicketMachine
|
52
|
87
|
*/
|
53
|
88
|
public void insertMoney(int amount)
|
54
|
89
|
{
|
55
|
|
- balance = balance + amount;
|
|
90
|
+ if(amount >= 0){
|
|
91
|
+ balance = balance + amount;
|
|
92
|
+ }
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ /**
|
|
96
|
+ * Return the total amount of money collected by this machine.
|
|
97
|
+ */
|
|
98
|
+ //Exercise 2.24
|
|
99
|
+ public int getTotal()
|
|
100
|
+ {
|
|
101
|
+ return total;
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ /**
|
|
105
|
+ * Print message when incorrect amount of money is inserted
|
|
106
|
+ */
|
|
107
|
+ //Exercise 2.33
|
|
108
|
+ public void prompt()
|
|
109
|
+ {
|
|
110
|
+ System.out.println("Please insert the correct amount of money.");
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ /**
|
|
114
|
+ * Print message showing price of a ticket
|
|
115
|
+ */
|
|
116
|
+ //Exercise 2.34
|
|
117
|
+ public void showPrice()
|
|
118
|
+ {
|
|
119
|
+ System.out.println("The price of a ticket is " + price + " cents.");
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ /**
|
|
123
|
+ * Print message showing price of a ticket
|
|
124
|
+ */
|
|
125
|
+ //Exercise 2.40
|
|
126
|
+ public void empty()
|
|
127
|
+ {
|
|
128
|
+ total = 0;
|
|
129
|
+ }
|
|
130
|
+
|
|
131
|
+ /**
|
|
132
|
+ * Print message showing price of a ticket
|
|
133
|
+ */
|
|
134
|
+ //Exercise 2.55
|
|
135
|
+ public int emptyMachine()
|
|
136
|
+ {
|
|
137
|
+ return total;
|
|
138
|
+ //total=0;
|
56
|
139
|
}
|
57
|
140
|
|
58
|
141
|
/**
|
|
@@ -62,6 +145,7 @@ public class TicketMachine
|
62
|
145
|
*/
|
63
|
146
|
public void printTicket()
|
64
|
147
|
{
|
|
148
|
+ /*
|
65
|
149
|
// Simulate the printing of a ticket.
|
66
|
150
|
System.out.println("##################");
|
67
|
151
|
System.out.println("# The BlueJ Line");
|
|
@@ -74,5 +158,107 @@ public class TicketMachine
|
74
|
158
|
total = total + balance;
|
75
|
159
|
// Clear the balance.
|
76
|
160
|
balance = 0;
|
|
161
|
+ */
|
|
162
|
+
|
|
163
|
+ /*
|
|
164
|
+ if(balance >= price) {
|
|
165
|
+ System.out.println("##################");
|
|
166
|
+ System.out.println("# The BlueJ Line");
|
|
167
|
+ System.out.println("# Ticket");
|
|
168
|
+ System.out.println("# " + price + " cents.");
|
|
169
|
+ System.out.println("##################");
|
|
170
|
+ System.out.println();
|
|
171
|
+ // Update the total collected with the price.
|
|
172
|
+ total = total + price;
|
|
173
|
+ // Reduce the balance by the price.
|
|
174
|
+ balance = balance - price;
|
|
175
|
+ }
|
|
176
|
+ else {
|
|
177
|
+ System.out.println("You must insert at least: " +
|
|
178
|
+ (price - balance) + " more cents.");
|
|
179
|
+ }
|
|
180
|
+ */
|
|
181
|
+
|
|
182
|
+ int amountLeftToPay = price - balance;
|
|
183
|
+
|
|
184
|
+ if(amountLeftToPay <=0){
|
|
185
|
+ System.out.println("##################");
|
|
186
|
+ System.out.println("# The BlueJ Line");
|
|
187
|
+ System.out.println("# Ticket");
|
|
188
|
+ System.out.println("# " + price + " cents.");
|
|
189
|
+ System.out.println("##################");
|
|
190
|
+ System.out.println();
|
|
191
|
+ // Update the total collected with the price.
|
|
192
|
+ total = total + price;
|
|
193
|
+ // Reduce the balance by the price.
|
|
194
|
+ balance = balance - price;
|
|
195
|
+ } else {
|
|
196
|
+ System.out.println("You must insert at least: " +
|
|
197
|
+ (price - balance) + " more cents.");
|
|
198
|
+ }
|
77
|
199
|
}
|
|
200
|
+
|
78
|
201
|
}
|
|
202
|
+
|
|
203
|
+/* EXERCISES
|
|
204
|
+ * 2.1: call void printTicket()
|
|
205
|
+ * 2.2: price that was entered
|
|
206
|
+ * 2.3: The ticket prints regardless
|
|
207
|
+ * 2.4: [practice ticket machine]
|
|
208
|
+ * 2.5: Yes, price is different.
|
|
209
|
+ * 2.6: public class Student; public class LabClass
|
|
210
|
+ * 2.7: Order of statement matters
|
|
211
|
+ * 2.8: It is possible to leave this out, the class will be assigned default access modifier
|
|
212
|
+ * 2.9: fields: price, balance, total;
|
|
213
|
+ * constructors: public TicketMachine(int ticketCost);
|
|
214
|
+ * methods: getPrice, getBalance, insertMoney, printTicket
|
|
215
|
+ * 2.10: no return type defined
|
|
216
|
+ * 2.11: int, Student, Server
|
|
217
|
+ * 2.12: alive, tutor, game
|
|
218
|
+ * 2.13: Order of statement matters
|
|
219
|
+ * 2.14: field declaration requires a semicolon
|
|
220
|
+ * 2.15: Shown in code
|
|
221
|
+ * 2.16: Student
|
|
222
|
+ * 2.17: title of type String, and price of type double
|
|
223
|
+ * 2.18: fields: title, price, author, publisher, publish date
|
|
224
|
+ * types: string, double, string, string, date
|
|
225
|
+ * 2.19: name = petsName;
|
|
226
|
+ * 2.20: The problem when adding int is the ticketCost was not recognized as price
|
|
227
|
+ * Assignments should not re-declare the field type. This needs to be done in fields section.
|
|
228
|
+ * 2.21: getPrice returns a passed value and getBalance returns a set value.
|
|
229
|
+ * 2.22: getBalance - "How much money do I owe or am I owed?"
|
|
230
|
+ * 2.23: No, the variable does not need to have the same name as the method
|
|
231
|
+ * 2.24: new getTotal method created above.
|
|
232
|
+ * 2.25: error "missing return statement"
|
|
233
|
+ * 2.26: getPrice returns an integer, printTicket does not return anything (void)
|
|
234
|
+ * 2.27: They are actions and don't return anything.
|
|
235
|
+ * 2.28: (Practicing methods)
|
|
236
|
+ * 2.29: There is a return type included
|
|
237
|
+ * 2.30-2.34: Shown in Code
|
|
238
|
+ * 2.35: Different outputs because the price is variable
|
|
239
|
+ * 2.36: "price" string would display instead of a variable
|
|
240
|
+ * 2.37: Same output as 2.36
|
|
241
|
+ * 2.38: No, they cannot show variable prices because they are outputing a string
|
|
242
|
+ * 2.39: Objects cannot pass a parameter to the ticketmachine
|
|
243
|
+ * 2.40: Mutator method
|
|
244
|
+ * 2.41: Setter method
|
|
245
|
+ * 2.42: Shown in code
|
|
246
|
+ * 2.43: (Practicing methods)
|
|
247
|
+ * 2.44: negative values do not affect balance
|
|
248
|
+ * 2.45: Booleans are well suited to be controlled by a type with only two different values
|
|
249
|
+ * 2.46: Shown in code
|
|
250
|
+ * 2.47: No because there is a check to make sure price is not greater than balance
|
|
251
|
+ * 2.48: +, -, /, *, %
|
|
252
|
+ * 2.49: saving = price * discount;
|
|
253
|
+ * 2.50: mean = total / count;
|
|
254
|
+ * 2.51: if(price > budget){System.out.print("Too expensive")}else {System.out.print("Just right")}
|
|
255
|
+ * 2.52: if(price > budget){System.out.print("Too expensive, your budget is only " + budget)}else {System.out.print("Just right")}
|
|
256
|
+ * 2.53: it is setting balance as 0 before returning it.
|
|
257
|
+ * 2.54: return statement ends the execution of a method
|
|
258
|
+ * 2.55: Shown in code
|
|
259
|
+ * 2.56: It is an accessor because method ends after return statement
|
|
260
|
+ * 2.57: Shown in code
|
|
261
|
+ * 2.58: setPrice method can be called to set price based on user selection
|
|
262
|
+ */
|
|
263
|
+
|
|
264
|
+
|