Skip to content

Commit 6347134

Browse files
author
mpetrov
committed
#8 initial commit for ACCOUNT domain objects
1 parent 0d7b8d4 commit 6347134

File tree

3 files changed

+185
-1
lines changed

3 files changed

+185
-1
lines changed

jcash-core/pom.xml

+21-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,25 @@
77
<artifactId>jcash-core</artifactId>
88
<version>1.0.0-SNAPSHOT</version>
99

10-
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<version>3.8.1</version>
16+
<configuration>
17+
<source>14</source>
18+
<target>14</target>
19+
</configuration>
20+
</plugin>
21+
</plugins>
22+
</build>
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.junit.jupiter</groupId>
26+
<artifactId>junit-jupiter</artifactId>
27+
<version>RELEASE</version>
28+
<scope>test</scope>
29+
</dependency>
30+
</dependencies>
1131
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.sct.jcash.domain;
2+
3+
import java.time.LocalDate;
4+
import java.util.*;
5+
6+
public class Account {
7+
public static final String RUB_CCY = "RUB";
8+
9+
// todo: make a separate class/enum for ccy
10+
private final String ccy;
11+
12+
private final List<Account> children;
13+
private final NavigableMap<LocalDate, Double> balance;
14+
private boolean closed;
15+
private String name;
16+
17+
public Account(String name, String ccy) {
18+
this.name = name;
19+
this.ccy = ccy;
20+
this.closed = false;
21+
this.children = new ArrayList<>();
22+
this.balance = new TreeMap<>();
23+
}
24+
25+
public static Account of(String name, String ccy) {
26+
return new Account(name, ccy);
27+
}
28+
29+
public static Account of(String name) {
30+
return of(name, RUB_CCY);
31+
}
32+
33+
public String getCcy() {
34+
return ccy;
35+
}
36+
37+
public double getBalance(LocalDate date) {
38+
if (balance.containsKey(date)) {
39+
return balance.get(date);
40+
}
41+
42+
NavigableSet<LocalDate> dates = (NavigableSet<LocalDate>) balance.keySet();
43+
44+
LocalDate closestDate = dates.stream().filter(d -> d.isBefore(date)).findFirst().orElse(null);
45+
46+
if(closestDate != null) {
47+
return balance.get(closestDate);
48+
}
49+
50+
return 0;
51+
}
52+
53+
public List<Account> getChildren() {
54+
return Collections.unmodifiableList(children);
55+
}
56+
57+
public boolean isClosed() {
58+
return closed;
59+
}
60+
61+
public boolean addChild(Account childAccount) {
62+
children.add(childAccount);
63+
return true;
64+
}
65+
66+
67+
@Override
68+
public boolean equals(Object o) {
69+
if (this == o) return true;
70+
if (o == null || getClass() != o.getClass()) return false;
71+
Account account = (Account) o;
72+
return closed == account.closed &&
73+
Objects.equals(ccy, account.ccy) &&
74+
Objects.equals(children.size(), account.children.size()) &&
75+
Objects.equals(balance.size(), account.balance.size()) &&
76+
Objects.equals(name, account.name);
77+
}
78+
79+
@Override
80+
public int hashCode() {
81+
return Objects.hash(ccy, children.size(), balance.size(), closed, name);
82+
}
83+
84+
public String getName() {
85+
return name;
86+
}
87+
88+
public void setBalance(LocalDate date, double amount) {
89+
this.balance.put(date, amount);
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.sct.jcash.domain;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.time.LocalDate;
7+
import java.time.temporal.ChronoUnit;
8+
9+
import static org.sct.jcash.domain.Account.RUB_CCY;
10+
11+
public class AccountTest {
12+
13+
@Test
14+
public void construction() {
15+
Account account = Account.of("Account name", RUB_CCY);
16+
Assertions.assertNotNull(account);
17+
18+
Assertions.assertEquals("Account name", account.getName());
19+
Assertions.assertEquals(RUB_CCY, account.getCcy());
20+
Assertions.assertEquals(0.0, account.getBalance(LocalDate.now()));
21+
Assertions.assertEquals(0, account.getChildren().size());
22+
Assertions.assertFalse(account.isClosed());
23+
24+
account = Account.of("a name");
25+
Assertions.assertEquals("a name", account.getName());
26+
Assertions.assertEquals(RUB_CCY, account.getCcy());
27+
}
28+
29+
@Test
30+
public void parentChildRelation() {
31+
Account parentAccount = Account.of("");
32+
Account childAccount = Account.of("");
33+
34+
boolean added = parentAccount.addChild(childAccount);
35+
36+
Assertions.assertTrue(added);
37+
Assertions.assertEquals(1, parentAccount.getChildren().size());
38+
Assertions.assertEquals(childAccount, parentAccount.getChildren().get(0));
39+
}
40+
41+
@Test
42+
public void equality() {
43+
Account accLeft = Account.of("");
44+
Account accRight = Account.of("");
45+
46+
Assertions.assertEquals(accLeft, accRight);
47+
}
48+
49+
@Test
50+
public void balance() {
51+
Account acc = Account.of("acc");
52+
53+
LocalDate date = LocalDate.now();
54+
double amount = 100.15;
55+
acc.setBalance(date, amount);
56+
57+
double balance = acc.getBalance(date);
58+
Assertions.assertEquals(amount, balance);
59+
60+
Assertions.assertEquals(0.0, acc.getBalance(date.minus(1, ChronoUnit.DAYS)));
61+
Assertions.assertEquals(amount, acc.getBalance(date.plus(5, ChronoUnit.DAYS)));
62+
}
63+
64+
65+
// todo: account balance for particular date
66+
// todo: parent-child relationship:
67+
// RUR -> RUR (OK)
68+
// RUR -> USD (ERROR)
69+
// todo: USD currency account
70+
// todo: make Account.of()
71+
// todo: rename account
72+
// todo: close account
73+
}

0 commit comments

Comments
 (0)