-
Notifications
You must be signed in to change notification settings - Fork 13
/
Order.java
51 lines (46 loc) · 1.36 KB
/
Order.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.sunnypatel.daysofrefactoringjava.day13.extractmethodobjects.problem;
import java.util.ArrayList;
import java.util.List;
/*
* When trying to
apply an Extract Method refactoring, and multiple methods are needing to be introduced, it sometimes
gets ugly because of multiple local variables that are being used within a method.
Because of this reason, it
is better to introduce an Extract Method Object refactoring and to segregate the logic required to perform
the task.
*/
public class Order {
private List<OrderLineItem> orderLineItems = new ArrayList<>();
private List<Double> discounts = new ArrayList<>();
private double tax = 0;
private double netTax = 0;
private double subtotal = 0;
public double calculate(){
orderLineItems.forEach(orderLineItem -> {
subtotal += orderLineItem.getPrice();
});
discounts.forEach(discount -> {
subtotal -= discount;
});
netTax = subtotal * tax;
return subtotal+netTax;
}
public List<OrderLineItem> getOrderLineItems() {
return orderLineItems;
}
public void setOrderLineItems(List<OrderLineItem> orderLineItems) {
this.orderLineItems = orderLineItems;
}
public List<Double> getDiscounts() {
return discounts;
}
public void setDiscounts(List<Double> discounts) {
this.discounts = discounts;
}
public double getTax() {
return tax;
}
public void setTax(double tax) {
this.tax = tax;
}
}