Skip to content

Commit 42cbf08

Browse files
committed
Refactoring Money
1 parent c2060a6 commit 42cbf08

File tree

3 files changed

+13
-24
lines changed

3 files changed

+13
-24
lines changed

src/com/lld/amazon/model/PricingGroup.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lld.amazon.model;
22

3+
import com.lld.amazon.constant.CurrencySymbol;
34
import com.lld.amazon.constant.PricingGroupType;
45
import com.lld.amazon.exception.PricingException;
56

@@ -58,12 +59,12 @@ public void addPricingLineItem(PricingLineItem lineItem, PricingGroup baseGroup)
5859

5960
private Double calculateTotal(PricingGroup baseGroup) throws PricingException {
6061
Double amount = 0D;
61-
String symbol = null;
62+
CurrencySymbol symbol = null;
6263

6364
for (PricingLineItem lineItem: pricingLineItems) {
6465
if (symbol == null) {
65-
symbol = lineItem.getCurrencySymbol();
66-
} else if (!symbol.equals(lineItem.getCurrencySymbol())) {
66+
symbol = lineItem.getAmount().getCurrencySymbol();
67+
} else if (!symbol.equals(lineItem.getAmount().getCurrencySymbol())) {
6768
throw new PricingException("Inconsistent symbols in pricing line items");
6869
}
6970

@@ -81,8 +82,8 @@ private Double calculateTotal(PricingGroup baseGroup) throws PricingException {
8182
}
8283
}
8384

84-
if (lineItem.getAmount() != null) {
85-
amount += lineItem.getAmount();
85+
if (lineItem.getAmount() != null && lineItem.getAmount().getAmount() != null) {
86+
amount += lineItem.getAmount().getAmount();
8687
} else if (lineItem.getPercentageOfBaseAmount() != null) {
8788
amount = amount + baseGroup.totalAmount * lineItem.getPercentageOfBaseAmount() * 0.01;
8889
}
Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.lld.amazon.model;
22

3-
import com.lld.amazon.constant.PricingLineItemType;
4-
53
import java.util.Objects;
64

75
/*
@@ -10,36 +8,28 @@
108
*/
119
public class PricingLineItem {
1210
private String name;
13-
// If `amount` is specified, it will be directly used
14-
// for computation
15-
private Double amount;
1611
// If `amount` is unspecified, `percentageOfBaseAmount`
12+
private Money amount;
1713
// will be used
1814
private Double percentageOfBaseAmount;
19-
private String currencySymbol;
2015

21-
public PricingLineItem(String name, Double amount, String currencySymbol, Double percentageOfBaseAmount) {
16+
public PricingLineItem(String name, Money amount, Double percentageOfBaseAmount) {
2217
this.name = name;
2318
this.amount = amount;
24-
this.currencySymbol = currencySymbol;
2519
this.percentageOfBaseAmount = percentageOfBaseAmount;
2620
}
2721

28-
public Double getAmount() {
22+
public Money getAmount() {
2923
return amount;
3024
}
3125

3226
public Double getPercentageOfBaseAmount() {
3327
return percentageOfBaseAmount;
3428
}
3529

36-
public String getCurrencySymbol() {
37-
return currencySymbol;
38-
}
39-
4030
@Override
4131
public int hashCode() {
42-
return Objects.hash(name, currencySymbol);
32+
return Objects.hash(name, this.amount.getCurrencySymbol().name());
4333
}
4434

4535
@Override
@@ -54,6 +44,6 @@ public boolean equals(Object other) {
5444

5545
PricingLineItem o = (PricingLineItem) other;
5646
return this.name.equals(o.name)
57-
&& this.currencySymbol.equals(o.currencySymbol);
47+
&& this.amount.getCurrencySymbol().name().equals(o.amount.getCurrencySymbol().name());
5848
}
5949
}

src/com/lld/amazon/service/impl/PricingServiceImpl.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public PricingGroup registerPricingLineItemWithAmount(PricingGroup pricingGroup,
4545
throw new PricingException("Amount can not be null");
4646
}
4747

48-
PricingLineItem pricingLineItem = new PricingLineItem(lineItemName, money.getAmount(),
49-
money.getCurrencySymbol().name(), null);
48+
PricingLineItem pricingLineItem = new PricingLineItem(lineItemName, money, null);
5049
pricingGroup.addPricingLineItem(pricingLineItem, baseGroup);
5150

5251
return pricingGroup;
@@ -76,8 +75,7 @@ public PricingGroup registerPricingLineItemWithPercentage(PricingGroup pricingGr
7675
throw new PricingException("Percentage of base amount can not be null");
7776
}
7877

79-
PricingLineItem pricingLineItem = new PricingLineItem(lineItemName, null, money.getCurrencySymbol().name(),
80-
percentageOfBaseAmount);
78+
PricingLineItem pricingLineItem = new PricingLineItem(lineItemName, money, percentageOfBaseAmount);
8179
pricingGroup.addPricingLineItem(pricingLineItem, baseGroup);
8280

8381
return pricingGroup;

0 commit comments

Comments
 (0)