-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCustomer.java
101 lines (75 loc) · 2.3 KB
/
Customer.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Customer {
private String name;
private List<Rental> rentals = new ArrayList<Rental>();
public Customer(String name) {
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Rental> getRentals() { return rentals;
}
public void setRentals(List<Rental> rentals) {
this.rentals = rentals;
}
public void addRental(Rental rental) {
rentals.add(rental);
}
// SRP violation - Long Method
// Feature Envy
public String getReport() {
String result = "Customer Report for " + getName() + "\n";
List<Rental> rentals = getRentals();
double totalCharge = 0;
int totalPoint = 0;
for (Rental each : rentals) {
double eachCharge = 0;
int eachPoint = 0 ;
int daysRented = 0;
// duplication
if (each.getStatus() == 1) { // returned Video
long diff = each.getReturnDate().getTime() - each.getRentDate().getTime();
daysRented = (int) (diff / (1000 * 60 * 60 * 24)) + 1;
} else { // not yet returned
long diff = new Date().getTime() - each.getRentDate().getTime();
daysRented = (int) (diff / (1000 * 60 * 60 * 24)) + 1;
}
// Strategy 또는 enum으로 처리
// magic no
switch (each.getVideo().getPriceCode()) {
case Video.REGULAR:
eachCharge += 2; // magic no
if (daysRented > 2)
eachCharge += (daysRented - 2) * 1.5;
break;
case Video.NEW_RELEASE:
eachCharge = daysRented * 3;
break;
}
eachPoint++;
if ((each.getVideo().getPriceCode() == Video.NEW_RELEASE) )
eachPoint++;
if ( daysRented > each.getDaysRentedLimit() )
eachPoint -= Math.min(eachPoint, each.getVideo().getLateReturnPointPenalty()) ;
// string generating 하는 부분으로 빼낸다
result += "\t" + each.getVideo().getTitle() + "\tDays rented: " + daysRented + "\tCharge: " + eachCharge
+ "\tPoint: " + eachPoint + "\n";
totalCharge += eachCharge;
totalPoint += eachPoint ;
}
result += "Total charge: " + totalCharge + "\tTotal Point:" + totalPoint + "\n";
if ( totalPoint >= 10 ) {
System.out.println("Congrat! You earned one free coupon");
}
if ( totalPoint >= 30 ) {
System.out.println("Congrat! You earned two free coupon");
}
return result ;
}
}