Skip to content

Commit e95c460

Browse files
committed
Multi Currency Money [springframeworkguru#26. Times method refactoring]
1 parent db54775 commit e95c460

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/test/java/guru/springframework/Dollar.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,4 @@ public Dollar(int amount, String currency) {
55
super(amount, currency);
66
}
77

8-
public Money times(int multiplier) {
9-
return Money.dollar(this.amount * multiplier);
10-
}
118
}

src/test/java/guru/springframework/Franc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public Franc(int amount, String currency) {
66
}
77

88
public Money times(int multiplier) {
9-
return Money.franc(this.amount * multiplier);
9+
return new Money(this.amount * multiplier, this.currency);
1010
}
1111

1212
}

src/test/java/guru/springframework/Money.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package guru.springframework;
22

3-
public abstract class Money {
3+
public class Money {
44
protected int amount;
55
protected String currency;
66

@@ -14,7 +14,9 @@ protected String currency() {
1414
return currency;
1515
}
1616

17-
public abstract Money times(int multiplier);
17+
public Money times(int multiplier) {
18+
return new Money(this.amount * multiplier, this.currency);
19+
}
1820

1921
public static Money dollar(int amount) {
2022
return new Dollar(amount, "USD");
@@ -27,8 +29,15 @@ public static Money franc(int amount) {
2729
public boolean equals(Object o) {
2830
Money money = (Money) o;
2931
return amount == money.amount
30-
&& this.getClass() == money.getClass();
32+
&& this.currency == money.currency;
3133
}
3234

35+
@Override
36+
public String toString() {
37+
return "Money{" +
38+
"amount=" + amount +
39+
", currency='" + currency + '\'' +
40+
'}';
41+
}
3342

3443
}

0 commit comments

Comments
 (0)