#499 Applying a Customer Discount to a Cart 2

Companies:
Yandex
Themes:
JavaOOPPractice

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 id and 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

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in