forked from ZCW-Java8-0/BlueJ.NaiveTicket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicketMachine.java
More file actions
120 lines (109 loc) · 3.83 KB
/
TicketMachine.java
File metadata and controls
120 lines (109 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* TicketMachine models a naive ticket machine that issues
* flat-fare tickets.
* The price of a ticket is specified via the constructor.
* It is a naive machine in the sense that it trusts its users
* to insert enough money before trying to print a ticket.
* It also assumes that users enter sensible amounts.
*
* @author David J. Barnes and Michael Kolling
* @version 2008.03.30
*/
//Ex 2.2 Balance is zero after running get balance after printing a ticket.
//Ex 2.3 Machine does not give refund if inserting too much money.
//Ex 2.3 Machine does not give an error if inserting too little money.
//Ex 2.5 Second machine has a different name.
//Ex 2.6 public class Student {} and public class LabClass {}
//Ex 2.7 Changing order of class declaration throws multiple errors and does not compile. Err messages are clear, compiler expects class id after "class".
//Ex 2.8 Removing public still compiles and all test cases run.
//Ex 2.9 TicketMachine-fields:price,balance,total,ticketNumber constructor-TicketMachine methods-getPrice,getTicketNumber,getBalance,insertMoney,calculateTotal,getTotal,incrementTicketNumber,printTicket
//Ex 2.10 The constructor only assigns values to the fields in the Superclass while creating a Subclass.
//Ex 2.11 1-int, 2-string, 3-string
//Ex 2.12 1-alive, 2-tutor, 3-game
//Ex 2.13 After re-ordering, the class diagram box has red lines, and the code wont compile, no matter the combination(aside from the correct one).
//Ex 2.14 Semicolon is necessary for declaration.
//Ex 2.15 Answer below
//Ex 2.16 The constructor belongs to the Student class
//Ex 2.17 This constructor has two parameters, a String and a Double.
//Ex 2.18 Book class's fields may include Strings, Integers, Doubles, and Floats. Can assume they would be titles, page numbers, catalog numbers, and prices.
public class TicketMachine
{
// The price of a ticket from this machine.
private Integer price;
// The amount of money entered by a customer so far.
private Integer balance;
// The total amount of money collected by this machine.
private Integer total;
// The number of tickets printed.
private Integer ticketNumber;
private Integer status;
//Ex 2.15
/**
* Create a machine that issues tickets of the given price.
* Note that the price must be greater than zero, and there
* are no checks to ensure this.
*/
public TicketMachine(Integer ticketCost)
{
price = ticketCost;
balance = 0;
total = 0;
ticketNumber = 0;
}
/**
* Return the price of a ticket.
*/
public Integer getPrice()
{
return price;
}
/**
* Return ticketNumber.
* (Increments on each print.)
*/
public Integer getTicketNumber()
{
return ticketNumber;
}
/**
* Return the amount of money already inserted for the
* next ticket.
*/
public Integer getBalance()
{
return balance;
}
/**
* Receive an amount of money in cents from a customer.
*/
public void insertMoney(Integer amount)
{
balance = balance + amount;
}
public Integer calculateTotal(){
total = balance + total;
return total;
}
public Integer incrementTicketNumber(){
ticketNumber++;
return ticketNumber;
}
public Integer getTotal() {
return total;
}
/**
* Print a ticket.
* Update the total collected and
* reduce the balance to zero.
*/
public String printTicket()
{
//Increment the number of tickets printed
incrementTicketNumber();
// Update the total collected with the balance.
total = total + balance;
// Clear the balance.
balance = 0;
return "Ticket price: " + price + " cents. " + "Your total is " + total + ".";
}
}