Build a Product class for a shop. A product has a name, a price and a discount.
The price is in cents, and it can never go below 0. The discount is a percent from 0 to 100. The setters keep both rules.
Implement the class Product:
Product(name, price)creates a product. Its discount starts at 0.Priceis a property. Reading it gives the price.- Setting
Priceto a negative number changes nothing. The old price stays. Discountis a property too. Reading it gives the discount.- Setting
Discountbelow 0 makes it 0. Setting it above 100 makes it 100. FinalPrice()returns the price with the discount taken off. It rounds down to a whole cent.
Keep the price and the discount in private fields. The properties Price and Discount read and set them. The name is a public field, name.
Each test case makes one product. Then it runs a list of operations on it, in order.
The output has one line per test case. It shows the name and a colon. Then it lists what each read gives, separated by spaces.
Example 1:
Input:
p = new Product("Pen", 250)
p.Price
p.Discount = 10
p.FinalPrice()
p.Price = -40
p.Price
Output: Pen: 250 225 250
Explanation: 10% of 250 cents is 25 cents, so the pen costs 225.
The price can't be -40, so it stays 250.
Example 2:
Input:
p = new Product("Lamp", 999)
p.Discount = 150
p.Discount
p.FinalPrice()
p.Discount = -20
p.Discount
Output: Lamp: 100 0 0
Explanation: 150 is above 100, so the discount becomes 100.
With 100% off, the lamp costs 0.
Then -20 is below 0, so the discount becomes 0.
Example 3:
Input:
p = new Product("Mug", 999)
p.Discount = 15
p.FinalPrice()
p.Price = 0
p.FinalPrice()
Output: Mug: 849 0
Explanation: 15% off 999 cents leaves 849.15 cents.
Rounded down, that is 849.
A price of 0 is allowed. It stays 0 after any discount.
Constraints:
- The first price is a whole number from 0 to 10,000,000.
- A new price is from -10,000,000 to 10,000,000.
- A new discount is from -1,000 to 1,000.
- A name is one word of letters.
- 1 to 1,000 operations per test case.
- Each test case reads at least one value.
Free plan: 30 tutor messages. Premium is unlimited.
