Nick Satinover преди 6 години
родител
ревизия
c3685472d3
променени са 2 файла, в които са добавени 37 реда и са изтрити 9 реда
  1. 6
    4
      Checkbook/src/main/java/Checkbook.java
  2. 31
    5
      Checkbook/src/test/java/CheckbookTest.java

+ 6
- 4
Checkbook/src/main/java/Checkbook.java Целия файл

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

+ 31
- 5
Checkbook/src/test/java/CheckbookTest.java Целия файл

9
 
9
 
10
 public class CheckbookTest {
10
 public class CheckbookTest {
11
     Checkbook checkbook;
11
     Checkbook checkbook;
12
+    Checkbook checkbookNoOwner;
12
     Transaction debit;
13
     Transaction debit;
13
     Transaction credit;
14
     Transaction credit;
14
     Payee payeeDebit;
15
     Payee payeeDebit;
26
         this.debit = new Transaction("This is a Debit", payeeDebit, TransactionType.DEBIT, 100.50);
27
         this.debit = new Transaction("This is a Debit", payeeDebit, TransactionType.DEBIT, 100.50);
27
         this.credit = new Transaction("This is a Credit", payeeCredit, TransactionType.CREDIT, 100.50);
28
         this.credit = new Transaction("This is a Credit", payeeCredit, TransactionType.CREDIT, 100.50);
28
         this.checkbook = new Checkbook(owner, 1000.00);
29
         this.checkbook = new Checkbook(owner, 1000.00);
30
+
31
+        this.checkbookNoOwner = new Checkbook(100.50);
29
     }
32
     }
30
 
33
 
31
     @Test
34
     @Test
48
     @Test
51
     @Test
49
     public void test3Size(){
52
     public void test3Size(){
50
         checkbook.add(debit);
53
         checkbook.add(debit);
54
+        checkbook.add(debit);
55
+        checkbook.add(debit);
51
         checkbook.remove(debit);
56
         checkbook.remove(debit);
52
-        int expected = 0;
57
+        int expected = 2;
53
         int actual = checkbook.size();
58
         int actual = checkbook.size();
54
         Assert.assertEquals(expected, actual);
59
         Assert.assertEquals(expected, actual);
55
     }
60
     }
56
-//
61
+
57
     @Test
62
     @Test
58
     public void test1IsEmpty(){
63
     public void test1IsEmpty(){
59
         checkbook.add(debit);
64
         checkbook.add(debit);
199
 
204
 
200
     @Test
205
     @Test
201
     public void test3ToArray(){
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
         checkbook.add(debit);
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
         checkbook.add(credit);
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
         Assert.assertEquals(expected, actual);
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