Last active
December 8, 2021 11:28
-
-
Save farhadmpr/24691abeee5c6541e8e14f1f8ece4169 to your computer and use it in GitHub Desktop.
RichModel1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Order { | |
private BigDecimal total; | |
private List<OrderItem> items = new ArrayList<OrderItem>(); | |
/** | |
* The total is defined as the sum of all {@link OrderItem#getTotal()}. | |
* | |
* @return the total of this {@link Order}. | |
*/ | |
public BigDecimal getTotal() { | |
if (total == null) { | |
/* | |
* we have to calculate the total and remember the result | |
*/ | |
BigDecimal orderItemTotal = BigDecimal.ZERO; | |
List<OrderItem> items = getItems(); | |
for (OrderItem orderItem : items) { | |
BigDecimal itemTotal = orderItem.getTotal(); | |
/* | |
* add the total of an OrderItem to our total. | |
*/ | |
orderItemTotal = orderItemTotal.add(itemTotal); | |
} | |
this.total = orderItemTotal; | |
} | |
return total; | |
} | |
/** | |
* Adds the {@link OrderItem} to this {@link Order}. | |
* | |
* @param orderItem | |
* the {@link OrderItem} to add. Must not be null. | |
*/ | |
public void addItem(OrderItem orderItem) { | |
if (orderItem == null) { | |
throw new IllegalArgumentException("orderItem must not be null"); | |
} | |
if (this.items.add(orderItem)) { | |
/* | |
* the list of order items changes so we reset the total field to | |
* let getTotal re-calculate the total. | |
*/ | |
this.total = null; | |
} | |
} | |
/** | |
* | |
* @return the {@link OrderItem} that belong to this {@link Order}. Clients | |
* may not modify the returned {@link List}. Use | |
* {@link #addItem(OrderItem)} instead. | |
*/ | |
public List<OrderItem> getItems() { | |
/* | |
* we wrap our items to prevent clients from manipulating our internal | |
* state. | |
*/ | |
return Collections.unmodifiableList(items); | |
} | |
} | |
import java.math.BigDecimal; | |
public class OrderItem { | |
private BigDecimal price; | |
private int quantity; | |
private String name = "no name"; | |
public OrderItem(BigDecimal price, int quantity, String name) { | |
if (price == null) { | |
throw new IllegalArgumentException("price must not be null"); | |
} | |
if (name == null) { | |
throw new IllegalArgumentException("name must not be null"); | |
} | |
if (price.compareTo(BigDecimal.ZERO) < 0) { | |
throw new IllegalArgumentException( | |
"price must be a positive big decimal"); | |
} | |
if (quantity < 1) { | |
throw new IllegalArgumentException("quantity must be 1 or greater"); | |
} | |
this.price = price; | |
this.quantity = quantity; | |
this.name = name; | |
} | |
public BigDecimal getPrice() { | |
return price; | |
} | |
public int getQuantity() { | |
return quantity; | |
} | |
public String getName() { | |
return name; | |
} | |
/** | |
* The total is defined as the {@link #getPrice()} multiplied with the | |
* {@link #getAmount()}. | |
* | |
* @return | |
*/ | |
public BigDecimal getTotal() { | |
int quantity = getQuantity(); | |
BigDecimal price = getPrice(); | |
BigDecimal total = price.multiply(new BigDecimal(quantity)); | |
return total; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment