Exercise: Retail Discount Calculator

Implement a point-of-sale logic to calculate final prices by safely parsing user input and applying conditional discounts.

Problem statement

A local retail store needs a simple console tool for its cashiers. The tool must accept the original price of an item and apply a 15% discount if the price is $100 or higher. If the price is below $100, no discount is applied. The system must be robust enough to handle accidental non-numeric text entry without crashing.

Task requirements

  • Prompt the user to enter the original price of an item.

  • Safely parse the input string into a numeric format.

  • Determine if the price qualifies for a 15% discount (price >= 100).

  • Calculate the final amount after the potential discount.

  • Print the final price to the console, formatted clearly for the cashier.

  • Display an error message if the user enters invalid text.

Constraints

  • Use Console.ReadLine() to capture user input.

  • Use the int.TryParse or double.TryParse pattern to ensure the application does not crash on invalid input.

  • Use an if-else statement or a switch expression to handle the discount logic.

  • Use string interpolation ($"") to display the final output.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Remember that TryParse returns a bool; use this in an if statement to check if the conversion worked.

  • To apply a 15% discount, you can multiply the price by 0.85 or subtract (price * 0.15) from the original.

  • Since prices can have decimals, using double or decimal is more appropriate than int.

Exercise: Retail Discount Calculator

Implement a point-of-sale logic to calculate final prices by safely parsing user input and applying conditional discounts.

Problem statement

A local retail store needs a simple console tool for its cashiers. The tool must accept the original price of an item and apply a 15% discount if the price is $100 or higher. If the price is below $100, no discount is applied. The system must be robust enough to handle accidental non-numeric text entry without crashing.

Task requirements

  • Prompt the user to enter the original price of an item.

  • Safely parse the input string into a numeric format.

  • Determine if the price qualifies for a 15% discount (price >= 100).

  • Calculate the final amount after the potential discount.

  • Print the final price to the console, formatted clearly for the cashier.

  • Display an error message if the user enters invalid text.

Constraints

  • Use Console.ReadLine() to capture user input.

  • Use the int.TryParse or double.TryParse pattern to ensure the application does not crash on invalid input.

  • Use an if-else statement or a switch expression to handle the discount logic.

  • Use string interpolation ($"") to display the final output.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Remember that TryParse returns a bool; use this in an if statement to check if the conversion worked.

  • To apply a 15% discount, you can multiply the price by 0.85 or subtract (price * 0.15) from the original.

  • Since prices can have decimals, using double or decimal is more appropriate than int.

// Retail Discount System
Console.WriteLine("=== Retail Discount Calculator ===");

// 1. Prompt the user for the item price
Console.Write("Enter the original price: ");
string input = Console.ReadLine();

// 2. Use TryParse to handle the input safely
// TODO: Implement the TryParse logic and conditional discount here

// 3. Print the final result or an error message
Starter code for the retail discount calculator