-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSilverState.java
More file actions
48 lines (35 loc) · 1.45 KB
/
Copy pathSilverState.java
File metadata and controls
48 lines (35 loc) · 1.45 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
import java.util.List;
public class SilverState implements CustomerState {
@Override
public void buyBooks(Customer customer, List<Book> books, boolean redeem) {
double totalCost = calculateTotalCost(books);
double costAfterRedemption = totalCost;
if (redeem) {
costAfterRedemption = applyRedemption(customer, totalCost);
}
int earnedPoints = (int)(costAfterRedemption * 10);
customer.setPoints(customer.getPoints() + earnedPoints);
updateStatus(customer);
}
@Override
public void updateStatus(Customer customer) {
if (customer.getPoints() >= 1000) {
customer.setState(new GoldState());
}
}
@Override
public String getStatusName() {
return "Silver";
}
private double calculateTotalCost(List<Book> books) {
return books.stream().mapToDouble(Book::getPrice).sum();
}
private double applyRedemption(Customer customer, double totalCost) {
int pointsToRedeem = customer.getPoints();
double discount = pointsToRedeem / 100.0;
double costAfterRedemption = Math.max(0, totalCost - discount);
int pointsUsed = (int)((totalCost - costAfterRedemption) * 100);
customer.setPoints(customer.getPoints() - pointsUsed);
return costAfterRedemption;
}
}