Procházet zdrojové kódy

test3CheckbookValue

Nick Satinover před 6 roky
rodič
revize
c3685472d3

+ 6
- 4
Checkbook/src/main/java/Checkbook.java Zobrazit soubor

@@ -6,16 +6,16 @@ import java.util.function.Predicate;
6 6
 
7 7
 public class Checkbook {
8 8
     private ArrayList<Transaction> transactionList;
9
-    private double cashAmount;
9
+    private GenericAccount account;
10 10
     private Payee owner;
11 11
 
12 12
     Checkbook(double cashAmount){
13
-        this.cashAmount = cashAmount;
14 13
         this.transactionList = new ArrayList<>();
14
+        this.account = new GenericAccount();
15 15
     }
16 16
     Checkbook(Payee owner, double cashAmount){
17 17
         this.owner = owner;
18
-        this.cashAmount = cashAmount;
18
+        this.account = new GenericAccount(cashAmount);
19 19
         this.transactionList = new ArrayList<>();
20 20
     }
21 21
 
@@ -34,6 +34,7 @@ public class Checkbook {
34 34
     public boolean add(Transaction element){
35 35
         if (element instanceof Transaction){
36 36
             transactionList.add(element);
37
+            account.setBalance(element.getAmount());
37 38
             return true;
38 39
         }
39 40
         return false;
@@ -41,6 +42,7 @@ public class Checkbook {
41 42
 
42 43
     public boolean remove(Transaction element){
43 44
         if (element instanceof Transaction && transactionList.contains(element)){
45
+            account.setBalance(element.getAmount() * -1);
44 46
             transactionList.remove(element);
45 47
             return true;
46 48
         }
@@ -60,7 +62,7 @@ public class Checkbook {
60 62
     }
61 63
 
62 64
     public double checkbookValue(){ // sum of all Transactions (both Debits and Credits)
63
-        return cashAmount;
65
+        return account.getBalance();
64 66
     }
65 67
 
66 68
     public Transaction[] getTransactionsForDate(LocalDate aDate){

+ 31
- 5
Checkbook/src/test/java/CheckbookTest.java Zobrazit soubor

@@ -9,6 +9,7 @@ import static org.junit.Assert.*;
9 9
 
10 10
 public class CheckbookTest {
11 11
     Checkbook checkbook;
12
+    Checkbook checkbookNoOwner;
12 13
     Transaction debit;
13 14
     Transaction credit;
14 15
     Payee payeeDebit;
@@ -26,6 +27,8 @@ public class CheckbookTest {
26 27
         this.debit = new Transaction("This is a Debit", payeeDebit, TransactionType.DEBIT, 100.50);
27 28
         this.credit = new Transaction("This is a Credit", payeeCredit, TransactionType.CREDIT, 100.50);
28 29
         this.checkbook = new Checkbook(owner, 1000.00);
30
+
31
+        this.checkbookNoOwner = new Checkbook(100.50);
29 32
     }
30 33
 
31 34
     @Test
@@ -48,12 +51,14 @@ public class CheckbookTest {
48 51
     @Test
49 52
     public void test3Size(){
50 53
         checkbook.add(debit);
54
+        checkbook.add(debit);
55
+        checkbook.add(debit);
51 56
         checkbook.remove(debit);
52
-        int expected = 0;
57
+        int expected = 2;
53 58
         int actual = checkbook.size();
54 59
         Assert.assertEquals(expected, actual);
55 60
     }
56
-//
61
+
57 62
     @Test
58 63
     public void test1IsEmpty(){
59 64
         checkbook.add(debit);
@@ -199,14 +204,35 @@ public class CheckbookTest {
199 204
 
200 205
     @Test
201 206
     public void test3ToArray(){
207
+        Transaction[] expected = new Transaction[1];
208
+        expected[0] = debit;
209
+        Transaction[] actual = checkbook.toArray(expected);
210
+        Assert.assertEquals(expected[0], actual[0]);
211
+    }
212
+
213
+    @Test
214
+    public void test1CheckbookValue(){
202 215
         checkbook.add(debit);
216
+        double expected = 100.50;
217
+        double actual = checkbook.checkbookValue();
218
+        Assert.assertEquals(expected, actual);
219
+    }
220
+
221
+    @Test
222
+    public void test2CheckbookValue(){
203 223
         checkbook.add(credit);
204
-        String expected = payeeCredit.getGivenName();
205
-        Transaction[] ret = checkbook.toArray();
206
-        String actual = ret[1].getPayee().getGivenName();
224
+        double expected = -100.50;
225
+        double actual = checkbook.checkbookValue();
207 226
         Assert.assertEquals(expected, actual);
208 227
     }
209 228
 
229
+    @Test
230
+    public void test3CheckbookValue(){
231
+        checkbook.add(credit);
232
+        double expected = -100.50;
233
+        double actual = checkbook.checkbookValue();
234
+        Assert.assertEquals(expected, actual);
235
+    }
210 236
 
211 237
 
212 238