#499 Applying a Customer Discount to a Cart 2
You are a backend developer at an online store. Business is growing, and the store has decided to improve customer loyalty by providing personalized discounts.
A product owner asks you to create a simple loyalty system that applies a percentage discount to a cart.
The discount amount depends on the customer. Analysts have already determined which discounts should be assigned to customers.
Definitions:
A cart is the customer's list of purchases.
A purchase:
-
Product
id; - Price;
- Final price after the discount.
Exercise
Implement the part of the new loyalty system that applies a customer's discount to a cart:
-
Accepts the customer
idand cart; - Calculates and applies discounts;
- Returns a cart with the discounts applied;
- All calculations are in rubles with kopeck precision. Use standard mathematical rounding.
There are two discount types, and they cannot be provided simultaneously:
- A percentage of the cart total;
- A fixed discount.
The discount amount is distributed among the products proportionally to each product's price.
Examples:
A 50% discount with products priced at 50 RUB and 100 RUB: discounts of 25 RUB and 50 RUB;
A 10 RUB discount with products priced at 20 RUB and 30 RUB: discounts of 4 RUB and 6 RUB;
A 10 RUB discount with products priced at 1 RUB and 99 RUB: discounts of 10 kopecks and 9 RUB 90 kopecks.
Question review
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor