Selaa lähdekoodia

finished commit

Jamez-s 6 vuotta sitten
vanhempi
commit
3c7c4f2329
2 muutettua tiedostoa jossa 99 lisäystä ja 13 poistoa
  1. 33
    13
      TicketMachine.java
  2. 66
    0
      TicketMachineTest.java

+ 33
- 13
TicketMachine.java Näytä tiedosto

@@ -6,11 +6,10 @@
6 6
  * to insert enough money before trying to print a ticket.
7 7
  * It also assumes that users enter sensible amounts.
8 8
  *
9
- * @author David J. Barnes and Michael Kolling
10
- * @version 2008.03.30
9
+ * @author James Seo
10
+ * @version 2018.05.24
11 11
  */
12
-public class TicketMachine
13
-{
12
+public class TicketMachine {
14 13
     // The price of a ticket from this machine.
15 14
     private int price;
16 15
     // The amount of money entered by a customer so far.
@@ -23,8 +22,7 @@ public class TicketMachine
23 22
      * Note that the price must be greater than zero, and there
24 23
      * are no checks to ensure this.
25 24
      */
26
-    public TicketMachine(int ticketCost)
27
-    {
25
+    public TicketMachine(int ticketCost) {
28 26
         price = ticketCost;
29 27
         balance = 0;
30 28
         total = 0;
@@ -33,8 +31,7 @@ public class TicketMachine
33 31
     /**
34 32
      * Return the price of a ticket.
35 33
      */
36
-    public int getPrice()
37
-    {
34
+    public int getPrice() {
38 35
         return price;
39 36
     }
40 37
 
@@ -42,19 +39,41 @@ public class TicketMachine
42 39
      * Return the amount of money already inserted for the
43 40
      * next ticket.
44 41
      */
45
-    public int getBalance()
46
-    {
47
-        return balance;
42
+    public int getBalance() {
43
+         return balance;
48 44
     }
49 45
 
50 46
     /**
51 47
      * Receive an amount of money in cents from a customer.
52 48
      */
53
-    public void insertMoney(int amount)
54
-    {
49
+    public void insertMoney(int amount) {
55 50
         balance = balance + amount;
56 51
     }
52
+    
53
+    public int getTotal() {
54
+         return total;
55
+    }
56
+    public void getPrompt() {
57
+    System.out.println("Please insert the correct amount of money.");
58
+    }
59
+    public void showPrice(){
60
+        System.out.println("# " + price + " cents.");
61
+          
62
+    }
63
+    
64
+    public void setEmpty(){
65
+     total = 0;
66
+  
67
+    }
68
+    public void setPrice(int newPrice){
69
+   
70
+    price = newPrice;
71
+    }   
72
+        
73
+
74
+
57 75
 
76
+ 
58 77
     /**
59 78
      * Print a ticket.
60 79
      * Update the total collected and
@@ -74,5 +93,6 @@ public class TicketMachine
74 93
         total = total + balance;
75 94
         // Clear the balance.
76 95
         balance = 0;
96
+    
77 97
     }
78 98
 }

+ 66
- 0
TicketMachineTest.java Näytä tiedosto

@@ -0,0 +1,66 @@
1
+
2
+
3
+import static org.junit.Assert.*;
4
+import org.junit.After;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+/**
9
+ * The test class TicketMachineTest.
10
+ *
11
+ * @author  (your name)
12
+ * @version (a version number or a date)
13
+ */
14
+public class TicketMachineTest
15
+{
16
+    private TicketMachine ticketMa1;
17
+    private TicketMachine ticketMa2;
18
+    private TicketMachine ticketMa3;
19
+    private TicketMachine ticketMa4;
20
+
21
+    /**
22
+     * Default constructor for test class TicketMachineTest
23
+     */
24
+    public TicketMachineTest()
25
+    {
26
+    }
27
+
28
+    /**
29
+     * Sets up the test fixture.
30
+     *
31
+     * Called before every test case method.
32
+     */
33
+    @Before
34
+    public void setUp()
35
+    {
36
+        ticketMa1 = new TicketMachine(500);
37
+        ticketMa2 = new TicketMachine(100);
38
+        ticketMa2.showPrice();
39
+        ticketMa1.showPrice();
40
+        ticketMa3 = new TicketMachine(500);
41
+        ticketMa4 = new TicketMachine(100);
42
+        ticketMa4.showPrice();
43
+        ticketMa3.showPrice();
44
+        ticketMa4.setPrice(50);
45
+        ticketMa3.setPrice(100);
46
+        ticketMa4.showPrice();
47
+        ticketMa3.setPrice(100);
48
+        ticketMa3.showPrice();
49
+        ticketMa4.showPrice();
50
+        ticketMa3.printTicket();
51
+        ticketMa4.getPrice();
52
+        ticketMa3.getPrice();
53
+        ticketMa4.getPrompt();
54
+        
55
+    }
56
+
57
+    /**
58
+     * Tears down the test fixture.
59
+     *
60
+     * Called after every test case method.
61
+     */
62
+    @After
63
+    public void tearDown()
64
+    {
65
+    }
66
+}