|
@@ -8,27 +8,41 @@
|
8
|
8
|
*
|
9
|
9
|
* @author David J. Barnes and Michael Kolling
|
10
|
10
|
* @version 2008.03.30
|
11
|
|
- */
|
12
|
|
-public class TicketMachine
|
|
11
|
+ */ public class TicketMachine
|
13
|
12
|
{
|
14
|
13
|
// The price of a ticket from this machine.
|
15
|
|
- private int price;
|
|
14
|
+ private int price ;
|
16
|
15
|
// The amount of money entered by a customer so far.
|
17
|
16
|
private int balance;
|
18
|
17
|
// The total amount of money collected by this machine.
|
19
|
18
|
private int total;
|
|
19
|
+ /**
|
|
20
|
+ * Exercise 2.15 Write in full the
|
|
21
|
+ * declaration for a field of type
|
|
22
|
+ * int whose name is status.
|
|
23
|
+ */
|
|
24
|
+ private int status;
|
20
|
25
|
|
21
|
26
|
/**
|
22
|
27
|
* Create a machine that issues tickets of the given price.
|
23
|
28
|
* Note that the price must be greater than zero, and there
|
24
|
29
|
* are no checks to ensure this.
|
|
30
|
+ * Exercise 2.39
|
25
|
31
|
*/
|
26
|
|
- public TicketMachine(int ticketCost)
|
|
32
|
+ public TicketMachine()
|
27
|
33
|
{
|
28
|
|
- price = ticketCost;
|
|
34
|
+ price = 1000;
|
29
|
35
|
balance = 0;
|
30
|
36
|
total = 0;
|
31
|
37
|
}
|
|
38
|
+
|
|
39
|
+ /**Exercise 2.42
|
|
40
|
+ *
|
|
41
|
+ */
|
|
42
|
+ public void TicketSet(int ticketCost)
|
|
43
|
+ {
|
|
44
|
+ price = ticketCost;
|
|
45
|
+ }
|
32
|
46
|
|
33
|
47
|
/**
|
34
|
48
|
* Return the price of a ticket.
|
|
@@ -38,6 +52,21 @@ public class TicketMachine
|
38
|
52
|
return price;
|
39
|
53
|
}
|
40
|
54
|
|
|
55
|
+ /**Exercise 2.33
|
|
56
|
+ * Request the correct amount of money
|
|
57
|
+ */
|
|
58
|
+ public void prompt()
|
|
59
|
+ {
|
|
60
|
+ System.out.println("Please insert the correct amount of money.");
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ /**Exercise 2.34
|
|
64
|
+ * Show price
|
|
65
|
+ */
|
|
66
|
+ public void showPrice()
|
|
67
|
+ {
|
|
68
|
+ System.out.println("The price of the ticket is " + price);
|
|
69
|
+ }
|
41
|
70
|
/**
|
42
|
71
|
* Return the amount of money already inserted for the
|
43
|
72
|
* next ticket.
|
|
@@ -55,6 +84,14 @@ public class TicketMachine
|
55
|
84
|
balance = balance + amount;
|
56
|
85
|
}
|
57
|
86
|
|
|
87
|
+ /**Exercise 2.24 Define an accessor method, getTotal,
|
|
88
|
+ * that returns the value of the total field.
|
|
89
|
+ */
|
|
90
|
+ public int getTotal()
|
|
91
|
+ {
|
|
92
|
+ return total;
|
|
93
|
+ }
|
|
94
|
+
|
58
|
95
|
/**
|
59
|
96
|
* Print a ticket.
|
60
|
97
|
* Update the total collected and
|
|
@@ -75,4 +112,31 @@ public class TicketMachine
|
75
|
112
|
// Clear the balance.
|
76
|
113
|
balance = 0;
|
77
|
114
|
}
|
78
|
|
-}
|
|
115
|
+
|
|
116
|
+ /**
|
|
117
|
+ * Exercise 2.30 & 2.41
|
|
118
|
+ * Set Price
|
|
119
|
+ */
|
|
120
|
+ public void setPrice(int newValue)
|
|
121
|
+ {
|
|
122
|
+ price = newValue;
|
|
123
|
+ //return price;
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ /**
|
|
127
|
+ * Exercise 2.32
|
|
128
|
+ * Reduce price by the given amount.
|
|
129
|
+ */
|
|
130
|
+ public void discount(int amount)
|
|
131
|
+ {
|
|
132
|
+ price = price - amount;
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ /**Exercise 2.40
|
|
136
|
+ * Empty machine
|
|
137
|
+ */
|
|
138
|
+ public void empty()
|
|
139
|
+ {
|
|
140
|
+ total = 0;
|
|
141
|
+ }
|
|
142
|
+ }
|