Procházet zdrojové kódy

chapter 2 exercies changes

Aliza Lang před 6 roky
rodič
revize
3596851977
3 změnil soubory, kde provedl 77 přidání a 38 odebrání
  1. 53
    14
      TicketMachine.java
  2. 12
    12
      bluej.pkg
  3. 12
    12
      package.bluej

+ 53
- 14
TicketMachine.java Zobrazit soubor

@@ -23,9 +23,9 @@ public class TicketMachine
23 23
      * Note that the price must be greater than zero, and there
24 24
      * are no checks to ensure this.
25 25
      */
26
-    public TicketMachine(int ticketCost)
26
+    public TicketMachine()
27 27
     {
28
-        price = ticketCost;
28
+        price = 1000;
29 29
         balance = 0;
30 30
         total = 0;
31 31
     }
@@ -47,12 +47,23 @@ public class TicketMachine
47 47
         return balance;
48 48
     }
49 49
 
50
+    public void setPrice(int newPrice) 
51
+    {
52
+        price = newPrice;
53
+    }
54
+
50 55
     /**
51 56
      * Receive an amount of money in cents from a customer.
52 57
      */
53 58
     public void insertMoney(int amount)
54
-    {
55
-        balance = balance + amount;
59
+
60
+    {if(amount >= 0) {
61
+            balance = balance + amount;
62
+        }
63
+        else {
64
+            System.out.println("Use a positive amount: " +
65
+                amount);
66
+        }
56 67
     }
57 68
 
58 69
     /**
@@ -62,17 +73,45 @@ public class TicketMachine
62 73
      */
63 74
     public void printTicket()
64 75
     {
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();
76
+        int amountLeftToPay = price - balance;
77
+        if (amountLeftToPay >= 0){
78
+            // Simulate the printing of a ticket.
79
+            System.out.println("##################");
80
+            System.out.println("# The BlueJ Line");
81
+            System.out.println("# Ticket");
82
+            System.out.println("# " + price + " cents.");
83
+            System.out.println("##################");
84
+            System.out.println();
72 85
 
73
-        // Update the total collected with the balance.
74
-        total = total + balance;
75
-        // Clear the balance.
86
+            // Update the total collected with the balance.
87
+            total = total + price;
88
+            // Clear the balance.
89
+
90
+        } else {
91
+            System.out.println("You must insert at least " + amountLeftToPay + " cents.");
92
+        }
93
+    }
94
+
95
+    public int refundBalance()
96
+    {
97
+        int amountToRefund = balance;
76 98
         balance = 0;
99
+        return amountToRefund;
100
+    }
101
+
102
+    public void prompt() 
103
+    {
104
+        System.out.println("Please insert the correct amount of money.");
105
+    }
106
+
107
+    public void showPrice() 
108
+    {
109
+        System.out.println("The price of a ticket is " + price + " cents.");
77 110
     }
111
+
112
+    public void emptyMachine() 
113
+    {
114
+        total = 0;
115
+    }
116
+
78 117
 }

+ 12
- 12
bluej.pkg Zobrazit soubor

@@ -1,18 +1,18 @@
1 1
 #BlueJ package file
2 2
 editor.fx.0.height=722
3
-editor.fx.0.width=800
4
-editor.fx.0.x=709
5
-editor.fx.0.y=113
3
+editor.fx.0.width=676
4
+editor.fx.0.x=755
5
+editor.fx.0.y=47
6 6
 objectbench.height=101
7
-objectbench.width=461
8
-package.divider.horizontal=0.6
9
-package.divider.vertical=0.8007380073800738
10
-package.editor.height=427
11
-package.editor.width=674
12
-package.editor.x=1067
13
-package.editor.y=119
14
-package.frame.height=600
15
-package.frame.width=800
7
+objectbench.width=349
8
+package.divider.horizontal=0.6425992779783394
9
+package.divider.vertical=0.8078291814946619
10
+package.editor.height=447
11
+package.editor.width=448
12
+package.editor.x=859
13
+package.editor.y=23
14
+package.frame.height=620
15
+package.frame.width=574
16 16
 package.numDependencies=0
17 17
 package.numTargets=1
18 18
 package.showExtends=true

+ 12
- 12
package.bluej Zobrazit soubor

@@ -1,18 +1,18 @@
1 1
 #BlueJ package file
2 2
 editor.fx.0.height=722
3
-editor.fx.0.width=800
4
-editor.fx.0.x=709
5
-editor.fx.0.y=113
3
+editor.fx.0.width=676
4
+editor.fx.0.x=755
5
+editor.fx.0.y=47
6 6
 objectbench.height=101
7
-objectbench.width=461
8
-package.divider.horizontal=0.6
9
-package.divider.vertical=0.8007380073800738
10
-package.editor.height=427
11
-package.editor.width=674
12
-package.editor.x=1067
13
-package.editor.y=119
14
-package.frame.height=600
15
-package.frame.width=800
7
+objectbench.width=349
8
+package.divider.horizontal=0.6425992779783394
9
+package.divider.vertical=0.8078291814946619
10
+package.editor.height=447
11
+package.editor.width=448
12
+package.editor.x=859
13
+package.editor.y=23
14
+package.frame.height=620
15
+package.frame.width=574
16 16
 package.numDependencies=0
17 17
 package.numTargets=1
18 18
 package.showExtends=true