forked from ZipCodeCore/BlueJ.NaiveTicket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicketMachine.java
More file actions
149 lines (136 loc) · 3.81 KB
/
TicketMachine.java
File metadata and controls
149 lines (136 loc) · 3.81 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* 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
*/
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;
// Entering a new price for the ticket.
private Integer newPrice;
/**
* 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 total amount of money inserted into the machine
*/
public Integer getTotal()
{
return total;
}
/**
* Return the amount of money already inserted for the
* next ticket.
*
public Integer getBalance()
{
return balance;
}
public String prompt(){
return "Please insert the correct amount of money.";
}
/**
* Display the price of the ticket on the terminal
* We return a String rather than System.out.println
* because it prints easier when the method is called
*/
public String showPrice(){
return "The price of a ticket is " + price + " cents.";
}
/**
* Receive an amount of money in cents from a customer.
*/
public Integer insertMoney(Integer amount)
{
if(amount <= 0){
System.out.println("Invalid amount.");
return balance;
} else {
balance = balance + amount;
total = total + amount;
return balance;
}
}
public Integer incrementTicketNumber(){
ticketNumber++;
return ticketNumber;
}
/**
* Resets the value of the total to zero
*/
public String emptyTotalBalance(){
total = 0;
balance = 0;
return "Your total is now 0.\nYour balance is now 0.";
}
/**
* Sets a new price to the ticket
*/
public Integer setPrice(Integer newPrice){
price = newPrice;
return price;
}
/**
* Print a ticket.
* Update the total collected and
* reduce the balance to zero.
*/
public String printTicket()
{
//Conditional that checks if there is enough money
if(balance < price){
return "Please insert the proper amount of money.";
} else {
//Increment the number of tickets printed
incrementTicketNumber();
// Clear the balance.
balance = balance - price;
if(ticketNumber == 1){
return "Ticket price: " + price + " cents." + "\n" +
"Your balance is " + balance +
" cents.\nYou have purchased 1 ticket.";
} else {
return "Ticket price: " + price + " cents.\n" +
"Your balance is " + balance + " cents.\nYou have purchased "
+ ticketNumber + " tickets.";
}
}
}
}