Add eligibility check
Problem Context
Users are eligible for premium features if they have an active subscription OR they are in a trial period.
Intermediate
100 points
File Changes (1)
src/main/java/com/example/service/EligibilityService.java
ADDED
@@ -0 +1 @@
| 1 | +package com.example.service; |
|
| 2 | + |
|
| 3 | +@Service |
|
| 4 | +public class EligibilityService { |
|
| 5 | + |
|
| 6 | + public boolean isEligibleForPremium(User user) { |
|
| 7 | + return Optional.ofNullable(user) |
|
| 8 | + .map(u -> Optional.ofNullable(u.getSubscription()) |
|
| 9 | + .map(sub -> sub.getStatus() != null |
|
| 10 | + ? sub.getStatus().equals("ACTIVE") |
|
| 11 | + ? Boolean.TRUE |
|
| 12 | + : sub.getStatus().equals("TRIAL") |
|
| 13 | + ? Optional.ofNullable(sub.getTrialEndDate()) |
|
| 14 | + .map(date -> date.isAfter(LocalDate.now())) |
|
| 15 | + .orElse(Boolean.FALSE) |
|
| 16 | + : Boolean.FALSE |
|
| 17 | + : Boolean.FALSE) |
|
| 18 | + .orElseGet(() -> Optional.ofNullable(u.getTrialStartDate()) |
|
| 19 | + .map(start -> start.plusDays(14).isAfter(LocalDate.now())) |
|
| 20 | + .orElse(Boolean.FALSE))) |
|
| 21 | + .orElse(Boolean.FALSE); |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + // Could have been simply: |
|
| 25 | + // public boolean isEligibleForPremium(User user) { |
|
| 26 | + // if (user == null) return false; |
|
| 27 | + // return hasActiveSubscription(user) || isInTrialPeriod(user); |
|
| 28 | + // } |
|
| 29 | +} |
Login Required: You must be registered to submit reviews and receive AI feedback.
Register or
login to start reviewing!
Your Review
Review Tips
- Look for security vulnerabilities (SQL injection, XSS, etc.)
- Check for null pointer exceptions and error handling
- Consider performance implications
- Evaluate code maintainability and readability
- Check for proper resource management
- Look for logic errors or edge cases