Browse Source

Exercises completed

ThuyKhong 6 years ago
parent
commit
a34a5a6591
1 changed files with 57 additions and 2 deletions
  1. 57
    2
      TicketMachine.java

+ 57
- 2
TicketMachine.java View File

28
         price = ticketCost;
28
         price = ticketCost;
29
         balance = 0;
29
         balance = 0;
30
         total = 0;
30
         total = 0;
31
+        prompt();
32
+    }
33
+    public TicketMachine()
34
+    {
35
+        price = 1000;
36
+        balance = 0;
37
+        total = 0;
38
+        prompt();
31
     }
39
     }
32
-
33
     /**
40
     /**
34
      * Return the price of a ticket.
41
      * Return the price of a ticket.
35
      */
42
      */
52
      */
59
      */
53
     public void insertMoney(int amount)
60
     public void insertMoney(int amount)
54
     {
61
     {
62
+        if (amount > 0) 
63
+        {
64
+         balance = balance + amount;
65
+        }
66
+        else 
67
+        System.out.println("Use a positive amount: " +amount);
68
+        
69
+        if (balance >= price) 
70
+        {
71
+        total = total + price;
72
+        balance = balance - price;
73
+        }
74
+        else
75
+        System.out.println("You must insert at least " + (price-balance)+ " more cents.");
55
         balance = balance + amount;
76
         balance = balance + amount;
56
     }
77
     }
57
 
78
 
62
      */
83
      */
63
     public void printTicket()
84
     public void printTicket()
64
     {
85
     {
86
+        int amountLeftToPay = price - balance;
87
+        if (amountLeftToPay <=0) 
88
+        {
89
+        
65
         // Simulate the printing of a ticket.
90
         // Simulate the printing of a ticket.
66
         System.out.println("##################");
91
         System.out.println("##################");
67
         System.out.println("# The BlueJ Line");
92
         System.out.println("# The BlueJ Line");
69
         System.out.println("# " + price + " cents.");
94
         System.out.println("# " + price + " cents.");
70
         System.out.println("##################");
95
         System.out.println("##################");
71
         System.out.println();
96
         System.out.println();
97
+        }
98
+        else
99
+        System.out.println("An additional " +amountLeftToPay+ " cents is required.");
72
 
100
 
73
         // Update the total collected with the balance.
101
         // Update the total collected with the balance.
74
         total = total + balance;
102
         total = total + balance;
75
         // Clear the balance.
103
         // Clear the balance.
76
-        balance = 0;
104
+       balance = 0;
105
+    }
106
+
107
+    public int getTotal()
108
+    {
109
+        return total;
110
+    }
111
+    public void prompt()
112
+    {
113
+        System.out.println("Please insert the correct amount of money.");
114
+    }
115
+    public void showPrice()
116
+    {
117
+     System.out.println("The price of a ticket is " + price + "cents.");   
118
+    }
119
+    public void empty()
120
+    {
121
+    total = 0;    
122
+    }
123
+    public void setPrice(int newPrice)
124
+    {
125
+    price = newPrice;   
126
+    }
127
+    public int emptyMachine()
128
+    {
129
+    System.out.println("The total is " + total);
130
+    total = 0;
131
+    return total;
77
     }
132
     }
78
 }
133
 }