jpsp91 6 years ago
parent
commit
03597814f4
4 changed files with 60 additions and 31 deletions
  1. BIN
      .DS_Store
  2. 42
    13
      TicketMachine.java
  3. 9
    9
      bluej.pkg
  4. 9
    9
      package.bluej

BIN
.DS_Store View File


+ 42
- 13
TicketMachine.java View File

@@ -12,7 +12,7 @@
12 12
 public class TicketMachine
13 13
 {
14 14
     // The price of a ticket from this machine.
15
-    private int price;
15
+    private int price = 500;
16 16
     // The amount of money entered by a customer so far.
17 17
     private int balance;
18 18
     // The total amount of money collected by this machine.
@@ -25,7 +25,7 @@ public class TicketMachine
25 25
      */
26 26
     public TicketMachine(int ticketCost)
27 27
     {
28
-        price = ticketCost;
28
+        int price = ticketCost;
29 29
         balance = 0;
30 30
         total = 0;
31 31
     }
@@ -52,7 +52,13 @@ public class TicketMachine
52 52
      */
53 53
     public void insertMoney(int amount)
54 54
     {
55
-        balance = balance + amount;
55
+        if(amount > 0) {
56
+            balance = balance + amount;
57
+        }
58
+        else {
59
+            System.out.println("Use a positive amount: " +
60
+                amount);
61
+        }
56 62
     }
57 63
 
58 64
     /**
@@ -62,17 +68,40 @@ public class TicketMachine
62 68
      */
63 69
     public void printTicket()
64 70
     {
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();
71
+        if(balance >= price) {
72
+            // Simulate the printing of a ticket.
73
+            System.out.println("##################");
74
+            System.out.println("# The BlueJ Line");
75
+            System.out.println("# Ticket");
76
+            System.out.println("# " + price + " cents.");
77
+            System.out.println("##################");
78
+            System.out.println();
79
+            // Update the total collected with the price.
80
+            total = total + price;
81
+            // Reduce the balance by the price.
82
+            balance = balance - price;
83
+        }
84
+        else {
85
+            System.out.println("You must insert at least: " +
86
+                (price - balance) + " more cents.");
87
+        }
88
+    }
89
+    
90
+    public void showPrice() 
91
+    {
92
+        System.out.println("The price of a ticket is " + price + " cents.");
93
+    }
72 94
 
73
-        // Update the total collected with the balance.
74
-        total = total + balance;
75
-        // Clear the balance.
95
+    /**
96
+     * Return the money in the balance.
97
+     * The balance is cleared.
98
+     */
99
+    public int refundBalance()
100
+    {
101
+        int amountToRefund;
102
+        amountToRefund = balance;
76 103
         balance = 0;
104
+        return amountToRefund;
77 105
     }
106
+
78 107
 }

+ 9
- 9
bluej.pkg View File

@@ -1,16 +1,16 @@
1 1
 #BlueJ package file
2
-editor.fx.0.height=722
3
-editor.fx.0.width=800
4
-editor.fx.0.x=709
5
-editor.fx.0.y=113
6
-objectbench.height=101
2
+editor.fx.0.height=721
3
+editor.fx.0.width=1280
4
+editor.fx.0.x=0
5
+editor.fx.0.y=23
6
+objectbench.height=164
7 7
 objectbench.width=461
8 8
 package.divider.horizontal=0.6
9
-package.divider.vertical=0.8007380073800738
10
-package.editor.height=427
9
+package.divider.vertical=0.6845018450184502
10
+package.editor.height=364
11 11
 package.editor.width=674
12
-package.editor.x=1067
13
-package.editor.y=119
12
+package.editor.x=249
13
+package.editor.y=108
14 14
 package.frame.height=600
15 15
 package.frame.width=800
16 16
 package.numDependencies=0

+ 9
- 9
package.bluej View File

@@ -1,16 +1,16 @@
1 1
 #BlueJ package file
2
-editor.fx.0.height=722
3
-editor.fx.0.width=800
4
-editor.fx.0.x=709
5
-editor.fx.0.y=113
6
-objectbench.height=101
2
+editor.fx.0.height=721
3
+editor.fx.0.width=1280
4
+editor.fx.0.x=0
5
+editor.fx.0.y=23
6
+objectbench.height=164
7 7
 objectbench.width=461
8 8
 package.divider.horizontal=0.6
9
-package.divider.vertical=0.8007380073800738
10
-package.editor.height=427
9
+package.divider.vertical=0.6845018450184502
10
+package.editor.height=364
11 11
 package.editor.width=674
12
-package.editor.x=1067
13
-package.editor.y=119
12
+package.editor.x=249
13
+package.editor.y=108
14 14
 package.frame.height=600
15 15
 package.frame.width=800
16 16
 package.numDependencies=0