-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicketMachine.java
More file actions
108 lines (91 loc) · 2.26 KB
/
TicketMachine.java
File metadata and controls
108 lines (91 loc) · 2.26 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
public class TicketMachine
{
private int price;
// The amount of money entered by a customer so far.
private int balance;
// The total amount of money collected by this machine.
private int total;
//disount offer
private int discount;
/**
* 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(int cost)
{
price = cost;
balance = 0;
total = 0;
}
/**
* Return the price of a ticket.
*/
public int getPrice()
{
return price;
}
public void setPrice(int price)
{
this.price=price;
}
/**
* Return the amount of money already inserted for the
* next ticket.
*/
public int getBalance()
{
return balance;
}
public void prompt(){
System.out.println("Please insert the correct amount of money.");
}
/**
* Receive an amount of money from a customer.
*/
public void insertMoney(int amount)
{
balance = balance + amount;
if (amount<=0){
prompt();}
}
public void setDiscount(int amount)
{
discount = amount;
price=price-discount;
}
public int getDiscount()
{
return price;
}
/**
* Print a ticket.
* Update the total collected and
* reduce the balance to zero.
*/
public void printTicket()
{ int amountLeftToPay= price-balance;
// Simulate the printing of a ticket.
if (amountLeftToPay<=0){
System.out.println("##################");
System.out.println("# The BlueJ Line");
System.out.println("# Ticket");
System.out.println("# " + price + " cents.");
System.out.println("##################");
System.out.println();
// Update the total collected with the balance.
total = total + balance;
balance = 0;
}
else {
System.out.println("Amount left to pay");
}
}
public void getTotal(){
System.out.println("#Total balance " + total + " cents.");
}
public void getEmptyMachine(){
getTotal();
total=0;
}
}