Browse Source

Trying this out

NedRedmond 6 years ago
parent
commit
5cb5bdc78d
1 changed files with 54 additions and 12 deletions
  1. 54
    12
      TicketMachine.java

+ 54
- 12
TicketMachine.java View File

1
+package NaiveTicket;
2
+
1
 /**
3
 /**
2
  * TicketMachine models a naive ticket machine that issues
4
  * TicketMachine models a naive ticket machine that issues
3
  * flat-fare tickets.
5
  * flat-fare tickets.
9
  * @author David J. Barnes and Michael Kolling
11
  * @author David J. Barnes and Michael Kolling
10
  * @version 2008.03.30
12
  * @version 2008.03.30
11
  */
13
  */
12
-public class TicketMachine
14
+class TicketMachine
13
 {
15
 {
14
     // The price of a ticket from this machine.
16
     // The price of a ticket from this machine.
15
     private int price;
17
     private int price;
23
      * Note that the price must be greater than zero, and there
25
      * Note that the price must be greater than zero, and there
24
      * are no checks to ensure this.
26
      * are no checks to ensure this.
25
      */
27
      */
28
+
26
     public TicketMachine(int ticketCost)
29
     public TicketMachine(int ticketCost)
27
     {
30
     {
28
         price = ticketCost;
31
         price = ticketCost;
52
      */
55
      */
53
     public void insertMoney(int amount)
56
     public void insertMoney(int amount)
54
     {
57
     {
55
-        balance = balance + amount;
58
+        if(amount > 0) {
59
+            balance = balance + amount;
60
+        } else {
61
+            System.out.println("Use an amount greater than zero: " + amount);
62
+        }
56
     }
63
     }
57
 
64
 
58
     /**
65
     /**
62
      */
69
      */
63
     public void printTicket()
70
     public void printTicket()
64
     {
71
     {
65
-        // Simulate the printing of a ticket.
66
-        System.out.println("##################");
67
-        System.out.println("# The BlueJ Line");
68
-        System.out.println("# Ticket");
69
-        System.out.println("# " + price + " cents.");
70
-        System.out.println("##################");
71
-        System.out.println();
72
+        int amountLeftToPay;
73
+
74
+        amountLeftToPay = price - balance;
75
+        if (amountLeftToPay <= 0) {
76
+            // Simulate the printing of a ticket.
77
+            System.out.println("##################");
78
+            System.out.println("# The BlueJ Line");
79
+            System.out.println("# Ticket");
80
+            System.out.println("# " + price + " cents.");
81
+            System.out.println("##################");
82
+            System.out.println();
83
+            
84
+                    // Update the total collected with the balance.
85
+        total = total + price;
86
+        // Reduce balance
87
+        balance = balance - price;
88
+        } else {
89
+            System.out.println("Please insert " + amountLeftToPay + " to receive ticket.");
90
+        }
91
+    }
92
+
93
+    public void prompt()
94
+    {
95
+        System.out.println("Please insert the correct amount of money.");
96
+    }
97
+
98
+    public void showPrice()
99
+    {
100
+        System.out.println("The price of a ticket is " + price + " cents.");
101
+    }
102
+
103
+    public void empty()
104
+    {
105
+        total = 0;
106
+    }
72
 
107
 
73
-        // Update the total collected with the balance.
74
-        total = total + balance;
75
-        // Clear the balance.
108
+    public void setPrice(int newPrice)
109
+    {
110
+        price = newPrice;
111
+    }
112
+
113
+    public int refundBalance()
114
+    {
115
+        int amountToRefund;
116
+        amountToRefund = balance;
76
         balance = 0;
117
         balance = 0;
118
+        return amountToRefund;
77
     }
119
     }
78
 }
120
 }