Skip to content

Commit

Permalink
feat: create loyal points
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Oct 1, 2024
1 parent 89f430e commit ce24a18
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package expert.os.books.ddd.chapter04;

public record Customer(String name, String email) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package expert.os.books.ddd.chapter04;

public enum CustomerCategory {

BRONZE(0, 0),
SILVER(1000, 5),
GOLD(5000, 10),
PLATINUM(10000, 15);

private final int pointsThreshold;
private final int discountPercentage;

CustomerCategory(int pointsThreshold, int discountPercentage) {
this.pointsThreshold = pointsThreshold;
this.discountPercentage = discountPercentage;
}

public int pointsThreshold() {
return pointsThreshold;
}

public int discountPercentage() {
return discountPercentage;
}

public static CustomerCategory getCategoryByPoints(int points) {
if (points >= PLATINUM.pointsThreshold) {
return PLATINUM;
} else if (points >= GOLD.pointsThreshold) {
return GOLD;
} else if (points >= SILVER.pointsThreshold) {
return SILVER;
} else {
return BRONZE;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package expert.os.books.ddd.chapter04;

public class LoyaltyCard {

private final String id;

private final Customer customer;
private LoyaltyPoints loyaltyPoints;
private boolean isPremium;
private CustomerCategory category;

public LoyaltyCard(String id, Customer customer) {
this.id = id;
this.customer = customer;
this.loyaltyPoints = new LoyaltyPoints(0);
this.isPremium = false;
this.category = CustomerCategory.BRONZE;
}

public String getId() {
return id;
}

public Customer getCustomer() {
return customer;
}

public int getPoints() {
return loyaltyPoints.getPoints();
}

public boolean isPremium() {
return isPremium;
}

public CustomerCategory getCategory() {
return category;
}

public void addPoints(int points) {
loyaltyPoints.addPoints(points);
updateCategory();
if (loyaltyPoints.canUpgradeToPremium()) {
this.isPremium = true;
}
}

private void updateCategory() {
this.category = CustomerCategory.getCategoryByPoints(loyaltyPoints.getPoints());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package expert.os.books.ddd.chapter04;

public class LoyaltyPoints {

private int points;

public LoyaltyPoints(int initialPoints) {
this.points = initialPoints;
}

public int getPoints() {
return points;
}

public void addPoints(int additionalPoints) {
this.points += additionalPoints;
}

public boolean canUpgradeToPremium() {
return this.points >= 1000;
}
}

0 comments on commit ce24a18

Please sign in to comment.