Skip to content

Commit 4bd05bf

Browse files
committed
refactor
1 parent 6358566 commit 4bd05bf

File tree

6 files changed

+25
-11
lines changed

6 files changed

+25
-11
lines changed

domain/src/main/java/com/github/j5ik2o/ddd_example/domain/BankAccount.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import com.github.j5ik2o.ddd_eaxmple.utils.IdGenerator;
44
import com.google.common.collect.Lists;
5+
import lombok.AccessLevel;
6+
import lombok.AllArgsConstructor;
57
import lombok.NonNull;
68
import lombok.Value;
79

810
import java.math.BigDecimal;
911
import java.util.List;
1012

1113
@Value
14+
@AllArgsConstructor(access = AccessLevel.PRIVATE)
1215
public final class BankAccount {
1316

1417
@NonNull

domain/src/main/java/com/github/j5ik2o/ddd_example/domain/BankAccountEvent.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.github.j5ik2o.ddd_example.domain;
22

3+
import lombok.AccessLevel;
4+
import lombok.AllArgsConstructor;
35
import lombok.NonNull;
46
import lombok.Value;
57

68
import java.time.ZonedDateTime;
79

810
@Value
11+
@AllArgsConstructor(access = AccessLevel.PRIVATE)
912
public final class BankAccountEvent {
1013
@NonNull
1114
private Long id;
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.j5ik2o.ddd_example.domain;
22

3+
import lombok.NonNull;
4+
import lombok.RequiredArgsConstructor;
35
import lombok.Value;
46

57
public final class BankAccountService {
@@ -8,23 +10,26 @@ public final class BankAccountService {
810
* 口座間送金後の状態を表す値オブジェクト。
911
*/
1012
@Value
13+
@RequiredArgsConstructor(staticName = "of")
1114
public static class TransferResult {
15+
@NonNull
1216
private BankAccount to;
17+
@NonNull
1318
private BankAccount from;
1419
}
1520

1621
/**
1722
* 二つの口座間でお金を移動する。
1823
*
19-
* @param to 移動先の口座
20-
* @param from 移動元の口座
24+
* @param to 移動先の口座
25+
* @param from 移動元の口座
2126
* @param money お金
2227
* @return 移動後の結果
2328
*/
2429
public static TransferResult transfer(BankAccount to, BankAccount from, Money money) {
2530
BankAccount updatedFrom = from.withdrawTo(to, money);
2631
BankAccount updatedTo = to.depositFrom(from, money);
27-
return new TransferResult(updatedTo, updatedFrom);
32+
return TransferResult.of(updatedTo, updatedFrom);
2833
}
2934

3035
}

domain/src/main/java/com/github/j5ik2o/ddd_example/stepbase/BankAccountService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.github.j5ik2o.ddd_eaxmple.utils.IdGenerator;
44
import com.google.common.collect.Lists;
5+
import lombok.AccessLevel;
6+
import lombok.AllArgsConstructor;
57
import lombok.Data;
68

79
import java.math.BigDecimal;

domain/src/test/java/com/github/j5ik2o/ddd_example/domain/BankAccountServiceTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.github.j5ik2o.ddd_eaxmple.utils.IdGenerator;
44
import org.junit.Test;
55

6+
import static org.junit.Assert.fail;
7+
68
public class BankAccountServiceTest {
79

810
@Test
@@ -33,13 +35,12 @@ public void transfer2() throws Exception {
3335
System.out.println("bankAccount1 = " + bankAccount1.getBalance());
3436
System.out.println("bankAccount2 = " + bankAccount2.getBalance());
3537

36-
BankAccountService.TransferResult transferResult = BankAccountService.transfer(
37-
bankAccount1,
38-
bankAccount2,
39-
Money.of(20000)
40-
);
38+
try {
39+
BankAccountService.transfer(bankAccount1, bankAccount2, Money.of(20000));
40+
fail("No error occurred!!!");
41+
} catch (IllegalArgumentException ignored) {
42+
;
43+
}
4144

42-
System.out.println("to = " + transferResult.getTo().getBalance());
43-
System.out.println("from = " + transferResult.getFrom().getBalance());
4445
}
4546
}

domain/src/test/java/com/github/j5ik2o/ddd_example/domain/BankAccountTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class BankAccountTest {
77

88
@Test
99
public void test() {
10-
BankAccount bankAccount = new BankAccount(1L, ImmutableList.of()).depositCash(Money.of(10000));
10+
BankAccount bankAccount = BankAccount.of(1L).depositCash(Money.of(10000));
1111
System.out.println(bankAccount.getBalance());
1212
}
1313

0 commit comments

Comments
 (0)