Skip to content

Commit 30948e5

Browse files
authored
Merge pull request #2 from WB3Tech-Java/add-money-comparators
Add money comparators
2 parents 7946506 + 304c53d commit 30948e5

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

src/main/java/com/wb3tech/kernel/valueobject/Money.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ public Money Minus(Money money) {
4444
return new Money(totalAmount, this.currency);
4545
}
4646

47+
public boolean LessThan(Money money) {
48+
this.validateDifferentCurrencyForAmountComparators(money);
49+
return this.getAmount() < money.getAmount();
50+
}
51+
52+
public boolean GreaterThan(Money money) {
53+
this.validateDifferentCurrencyForAmountComparators(money);
54+
return this.getAmount() > money.getAmount();
55+
}
56+
57+
public boolean Equals(Money money) {
58+
this.validateDifferentCurrencyForAmountComparators(money);
59+
return this.getAmount() == money.getAmount();
60+
}
61+
4762
public double getAmount() {
4863
return this.amount.doubleValue();
4964
}
@@ -80,4 +95,8 @@ private boolean isDifferentCurrency(Money money) {
8095
return !this.isSameCurrency(money);
8196
}
8297

98+
private void validateDifferentCurrencyForAmountComparators(Money money) {
99+
if(this.isDifferentCurrency(money)) { throw new ArithmeticException("Cannot compare of two different currencies."); }
100+
}
101+
83102
}

src/test/java/com/wb3tech/kernel/valueobject/MoneyTests.java

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,39 @@ public void SubtractTwoMonies() {
8787

8888
}
8989

90+
@Test @Tag("Small") @DisplayName("Money with amount of 13.00 USD should be less than Money with amount 15.00 USD")
91+
public void CompareAmountLessThan() {
92+
93+
var thirteen = Money.Of(13.00, "USD");
94+
var fifteen = Money.Of(15.00, "USD");
95+
var isLessThan = thirteen.LessThan(fifteen);
96+
97+
assertTrue(isLessThan);
98+
99+
}
100+
101+
@Test @Tag("Small") @DisplayName("Money with amount of 15.00 USD should be greater than Money with amount 13.00 USD")
102+
public void CompareAmountGreaterThan() {
103+
104+
var thirteen = Money.Of(13.00, "USD");
105+
var fifteen = Money.Of(15.00, "USD");
106+
var isGreaterThan = fifteen.GreaterThan(thirteen);
107+
108+
assertTrue(isGreaterThan);
109+
110+
}
111+
112+
@Test @Tag("Small") @DisplayName("Money with amount of 13.00 USD should equal money with amount 13.00 USD")
113+
public void CompareAmountEquals() {
114+
115+
var fifteenInstanceOne = Money.Of(15.00, "USD");
116+
var fifteenInstanceTwo = Money.Of(15.00, "USD");
117+
var isGreaterThan = fifteenInstanceOne.Equals(fifteenInstanceTwo);
118+
119+
assertTrue(isGreaterThan);
120+
121+
}
122+
90123
@Test @Tag("Small") @DisplayName("Should get a Money with amount of -5 when you subtract a money instance of 10 " +
91124
"and a second instance o 15")
92125
public void ShouldGetNegativeMoney() {
@@ -99,7 +132,7 @@ public void ShouldGetNegativeMoney() {
99132

100133
}
101134

102-
@Test @Tag("Small") @DisplayName("Should throw an ArithmeticExceptionn" +
135+
@Test @Tag("Small") @DisplayName("Should throw an ArithmeticException" +
103136
"with message 'Cannot subtract monies of two different currencies'")
104137
public void SubtractShouldThrowArithmeticExceptionForNotSameCurrency() {
105138

@@ -125,6 +158,45 @@ public void AddShouldThrowArithmeticExceptionForNotSameCurrency() {
125158

126159
}
127160

161+
@Test @Tag("Small") @DisplayName("Should throw an ArithmeticException " +
162+
"with message 'Cannot compare of two different currencies.' " +
163+
"when comparing amount-less-than for two different currencies.")
164+
public void AmountLessThanThrowArithmeticExceptionForNotSameCurrency() {
165+
166+
var thirteen = Money.Of(13.00, "USD");
167+
var fifteen = Money.Of(15.00, "GBP");
168+
169+
var exception = assertThrows(ArithmeticException.class, () -> thirteen.LessThan(fifteen));
170+
Assertions.assertEquals("Cannot compare of two different currencies.", exception.getMessage());
171+
172+
}
173+
174+
@Test @Tag("Small") @DisplayName("Should throw an ArithmeticException " +
175+
"with message 'Cannot compare of two different currencies.' " +
176+
"when comparing amount-greater-than for two different currencies.")
177+
public void AmountGreaterThanArithmeticExceptionForNotSameCurrency() {
178+
179+
var thirteen = Money.Of(13.00, "USD");
180+
var fifteen = Money.Of(15.00, "GBP");
181+
182+
var exception = assertThrows(ArithmeticException.class, () -> fifteen.GreaterThan(thirteen));
183+
Assertions.assertEquals("Cannot compare of two different currencies.", exception.getMessage());
184+
185+
}
186+
187+
@Test @Tag("Small") @DisplayName("Should throw an ArithmeticException " +
188+
"with message 'Cannot compare of two different currencies.' " +
189+
"when comparing amount-equals for two different currencies.")
190+
public void AmountEqualsArithmeticExceptionForNotSameCurrency() {
191+
192+
var fifteenInstanceOne = Money.Of(15.00, "USD");
193+
var fifteenInstanceTwo = Money.Of(15.00, "GBP");
194+
195+
var exception = assertThrows(ArithmeticException.class, () -> fifteenInstanceOne.Equals(fifteenInstanceTwo));
196+
Assertions.assertEquals("Cannot compare of two different currencies.", exception.getMessage());
197+
198+
}
199+
128200
@Test @Tag("Small") @DisplayName("A money instance of $1 should equal the second instance of $1")
129201
public void ShouldEqual() {
130202

0 commit comments

Comments
 (0)