Exercise: Retail Discount Calculator
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.TryParseordouble.TryParsepattern to ensure the application does not crash on invalid input.Use an
if-elsestatement or aswitchexpression 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
TryParsereturns abool; use this in anifstatement to check if the conversion worked.To apply a 15% discount, you can multiply the price by
0.85or subtract(price * 0.15)from the original.Since prices can have decimals, using
doubleordecimalis more appropriate thanint.
Exercise: Retail Discount Calculator
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.TryParseordouble.TryParsepattern to ensure the application does not crash on invalid input.Use an
if-elsestatement or aswitchexpression 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
TryParsereturns abool; use this in anifstatement to check if the conversion worked.To apply a 15% discount, you can multiply the price by
0.85or subtract(price * 0.15)from the original.Since prices can have decimals, using
doubleordecimalis more appropriate thanint.
// 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