-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
- Loading branch information
1 parent
89f430e
commit ce24a18
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
chapter-04/src/main/java/expert/os/books/ddd/chapter04/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
chapter-04/src/main/java/expert/os/books/ddd/chapter04/CustomerCategory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
chapter-04/src/main/java/expert/os/books/ddd/chapter04/LoyaltyCard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
chapter-04/src/main/java/expert/os/books/ddd/chapter04/LoyaltyPoints.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |