Skip to content

Commit 81ed349

Browse files
author
mpetrov
committed
#8 handle null child
1 parent 993c097 commit 81ed349

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

jcash-core/src/main/java/org/sct/jcash/domain/Account.java

+8
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ public boolean isClosed() {
7979
}
8080

8181
public boolean addChild(Account childAccount) {
82+
if(childAccount == null) {
83+
throw new IllegalArgumentException("Trying to add null child account");
84+
}
85+
86+
if(!children.isEmpty() && children.contains(childAccount)) {
87+
return false;
88+
}
89+
8290
children.add(childAccount);
8391
return true;
8492
}

jcash-core/src/test/java/org/sct/jcash/domain/AccountTest.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void construction() {
3636
}
3737

3838
@Test
39-
public void parentChildRelation() {
39+
public void addChild() {
4040
Account parentAccount = Account.of("");
4141
Account childAccount = Account.of("");
4242

@@ -45,8 +45,17 @@ public void parentChildRelation() {
4545
Assertions.assertTrue(added);
4646
Assertions.assertEquals(1, parentAccount.getChildren().size());
4747
Assertions.assertEquals(childAccount, parentAccount.getChildren().get(0));
48+
49+
// cannot add the same account twice
50+
Assertions.assertFalse(parentAccount.addChild(childAccount));
51+
Assertions.assertEquals(1, parentAccount.getChildren().size());
52+
53+
// cannot add null child
54+
Assertions.assertThrows(IllegalArgumentException.class, () -> parentAccount.addChild(null));
4855
}
4956

57+
58+
5059
@Test
5160
public void equality() {
5261

@@ -77,6 +86,7 @@ public void equality() {
7786

7887
@Test
7988
public void balance() {
89+
// todo move balance api into a separate class BalanceCalculator (maybe related to an account)
8090
Account acc = Account.of("acc");
8191

8292
LocalDate date = LocalDate.now();
@@ -91,7 +101,12 @@ public void balance() {
91101
}
92102

93103
@Test
94-
public void addOperation() {
104+
public void addOperationTest() {
105+
106+
}
107+
108+
@Test
109+
public void getOperationsTest() {
95110

96111
}
97112

0 commit comments

Comments
 (0)