Solution: Retail Discount Calculator
Explore how to implement a retail discount calculator in C# by reading input, validating numeric values with TryParse, applying conditional discounts using the ternary operator, and displaying formatted results. This lesson guides you through error handling to prevent crashes and ensures your program processes input robustly.
We'll cover the following...
We'll cover the following...
Console.WriteLine("=== Retail Discount Calculator ===");
Console.Write("Enter the original price: ");
string input = Console.ReadLine();
// We use double.TryParse to allow for decimal prices
if (double.TryParse(input, out double price))
{
// Apply 15% discount if price is 100 or more
double finalPrice = (price >= 100) ? price * 0.85 : price;
Console.WriteLine($"Original Price: ${price}");
Console.WriteLine($"Final Price: ${finalPrice}");
}
else
{
Console.WriteLine("Error: Please enter a valid numeric price.");
}The complete solution for the retail discount calculator exercise
...