Port Round 1 to Java (OrderDomain): 7 tests green, START state preserved
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package orderdomain;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Order {
|
||||
|
||||
private final List<OrderLine> lines = new ArrayList<>();
|
||||
|
||||
private boolean completed;
|
||||
|
||||
public List<OrderLine> getLines() {
|
||||
return lines;
|
||||
}
|
||||
|
||||
public BigDecimal getTotal() {
|
||||
BigDecimal total = BigDecimal.ZERO;
|
||||
for (OrderLine line : lines) {
|
||||
total = total.add(line.getValue());
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
public boolean isCompleted() {
|
||||
return completed;
|
||||
}
|
||||
|
||||
public void addLine(BigDecimal unitPrice, int quantity) {
|
||||
lines.add(new OrderLine(unitPrice, quantity));
|
||||
}
|
||||
|
||||
public void applyDiscount(int lineIndex) {
|
||||
lines.get(lineIndex).applyDiscount();
|
||||
}
|
||||
|
||||
public void complete() {
|
||||
completed = true;
|
||||
}
|
||||
|
||||
// C# decimal == is numeric (scale-insensitive); BigDecimal.equals is
|
||||
// scale-sensitive, so compare numerically with compareTo.
|
||||
public boolean isFullyPaid(BigDecimal expectedTotal) {
|
||||
return completed && getTotal().compareTo(expectedTotal) == 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package orderdomain;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class OrderLine {
|
||||
|
||||
private final BigDecimal unitPrice;
|
||||
private final int quantity;
|
||||
private boolean hasDiscount;
|
||||
|
||||
public OrderLine(BigDecimal unitPrice, int quantity) {
|
||||
this.unitPrice = unitPrice;
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public BigDecimal getUnitPrice() {
|
||||
return unitPrice;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public boolean hasDiscount() {
|
||||
return hasDiscount;
|
||||
}
|
||||
|
||||
public BigDecimal getValue() {
|
||||
BigDecimal value = unitPrice.multiply(BigDecimal.valueOf(quantity));
|
||||
return hasDiscount ? value.multiply(new BigDecimal("0.8")) : value;
|
||||
}
|
||||
|
||||
public void applyDiscount() {
|
||||
hasDiscount = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user