diff --git a/chapter-04/src/main/java/expert/os/books/ddd/chapter04/Customer.java b/chapter-04/src/main/java/expert/os/books/ddd/chapter04/Customer.java new file mode 100644 index 0000000..71df626 --- /dev/null +++ b/chapter-04/src/main/java/expert/os/books/ddd/chapter04/Customer.java @@ -0,0 +1,5 @@ +package expert.os.books.ddd.chapter04; + +public record Customer(String name, String email) { + +} diff --git a/chapter-04/src/main/java/expert/os/books/ddd/chapter04/CustomerCategory.java b/chapter-04/src/main/java/expert/os/books/ddd/chapter04/CustomerCategory.java new file mode 100644 index 0000000..587cd3a --- /dev/null +++ b/chapter-04/src/main/java/expert/os/books/ddd/chapter04/CustomerCategory.java @@ -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; + } + } + +} diff --git a/chapter-04/src/main/java/expert/os/books/ddd/chapter04/LoyaltyCard.java b/chapter-04/src/main/java/expert/os/books/ddd/chapter04/LoyaltyCard.java new file mode 100644 index 0000000..3f6e6b2 --- /dev/null +++ b/chapter-04/src/main/java/expert/os/books/ddd/chapter04/LoyaltyCard.java @@ -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()); + } +} diff --git a/chapter-04/src/main/java/expert/os/books/ddd/chapter04/LoyaltyPoints.java b/chapter-04/src/main/java/expert/os/books/ddd/chapter04/LoyaltyPoints.java new file mode 100644 index 0000000..ae37251 --- /dev/null +++ b/chapter-04/src/main/java/expert/os/books/ddd/chapter04/LoyaltyPoints.java @@ -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; + } +}