-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCustomerDataObject.java
More file actions
55 lines (46 loc) · 1.48 KB
/
Copy pathCustomerDataObject.java
File metadata and controls
55 lines (46 loc) · 1.48 KB
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
package adapter;
import java.util.Date;
import java.util.List;
public class CustomerDataObject implements DataObject {
private Customer customer;
private static long millisecondsInMonth = 30 * 24 * 3600 * 1000L;
private static long millisecondsInYear = 365 * 24 * 3600 * 1000L;
public CustomerDataObject(Customer customer) {
this.customer = customer;
}
@Override
public double getValue(String fieldName) {
switch (fieldName) {
case "callsPerMonth":
return calculateCallsPerLastMonth();
case "callsPerYear":
return calculateCallsPerLastYear();
case "minutesPerMonth":
return calculateMinutesPerLastMonth();
default:
throw new IllegalArgumentException(
"Field " + fieldName + " doesn't exist!"
);
}
}
private double calculateCallsPerLastMonth() {
Date now = new Date();
Date monthAgo = new Date(now.getTime() - millisecondsInMonth);
return customer.getCallsCount(now, monthAgo);
}
private double calculateCallsPerLastYear() {
Date now = new Date();
Date yearAgo = new Date(now.getTime() - millisecondsInYear);
return customer.getCallsCount(now, yearAgo);
}
private double calculateMinutesPerLastMonth() {
Date now = new Date();
Date monthAgo = new Date(now.getTime() - millisecondsInMonth);
List<Call> calls = customer.getCalls(now, monthAgo);
int minutes = 0;
for (Call call : calls) {
minutes += call.getDuration();
}
return minutes;
}
}