Add discount calculation
Problem Context
We need to apply different discount rates based on customer type: regular, premium, and VIP customers.
Intermediate
100 points
File Changes (1)
src/main/java/com/example/pricing/DiscountCalculator.java
ADDED
@@ -0 +1 @@
| 1 | +package com.example.pricing; |
|
| 2 | + |
|
| 3 | +@Service |
|
| 4 | +public class DiscountCalculator { |
|
| 5 | + |
|
| 6 | + public BigDecimal calculateDiscount(Customer customer, BigDecimal orderTotal) { |
|
| 7 | + BigDecimal discountRate; |
|
| 8 | + |
|
| 9 | + switch (customer.getType()) { |
|
| 10 | + case REGULAR: |
|
| 11 | + discountRate = new BigDecimal("0.00"); |
|
| 12 | + break; |
|
| 13 | + case PREMIUM: |
|
| 14 | + discountRate = new BigDecimal("0.10"); |
|
| 15 | + if (orderTotal.compareTo(new BigDecimal("100")) > 0) { |
|
| 16 | + discountRate = new BigDecimal("0.15"); |
|
| 17 | + } |
|
| 18 | + break; |
|
| 19 | + case VIP: |
|
| 20 | + discountRate = new BigDecimal("0.20"); |
|
| 21 | + if (customer.getYearsAsMember() > 5) { |
|
| 22 | + discountRate = new BigDecimal("0.25"); |
|
| 23 | + } |
|
| 24 | + if (orderTotal.compareTo(new BigDecimal("500")) > 0) { |
|
| 25 | + discountRate = discountRate.add(new BigDecimal("0.05")); |
|
| 26 | + } |
|
| 27 | + break; |
|
| 28 | + default: |
|
| 29 | + discountRate = BigDecimal.ZERO; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + return orderTotal.multiply(discountRate); |
|
| 33 | + } |
|
| 34 | +} |
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