Skip to content
Permalink
1c585b75d7
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
70 lines (57 sloc) 2.49 KB
 decimal subtotal = 0;
decimal price;
string input;
Console.WriteLine("Welcome to the Cashier Program!");
// Input loop for item prices
do
{
Console.Write("Enter item price (ex. 1.99) or press enter key if done: ");
input = Console.ReadLine();
if (decimal.TryParse(input, out price))
{
subtotal += price;
}
else if (input != "")
{
Console.WriteLine("Invalid input. Please enter a valid price.");
}
} while (input != "");
// Calculate sales tax
decimal salesTax = subtotal * 0.0615m;
// Calculate total with tax
decimal total = subtotal + salesTax;
Console.WriteLine($"Subtotal is: {subtotal:C}");
Console.WriteLine($"Sales tax is: {salesTax:C}");
Console.WriteLine($"Total amount due: {total:C}");
// Input for cash given
decimal cash;
do
{
Console.Write("Enter the cash given by the customer: ");
input = Console.ReadLine();
if (!decimal.TryParse(input, out cash) || cash <= 0)
{
Console.WriteLine("Invalid input. Please enter a valid positive number.");
}
} while (!decimal.TryParse(input, out cash) || cash <= 0);
// Calculate change
decimal change = cash - total;
// Calculate denominations
int twenties = (int)(change / 20);
change %= 20;
int tens = (int)(change / 10);
change %= 10;
int fives = (int)(change / 5);
change %= 5;
int ones = (int)change;
change -= ones;
int quarters = (int)(change / 0.25m);
change %= 0.25m;
int dimes = (int)(change / 0.1m);
change %= 0.1m;
int nickels = (int)(change / 0.05m);
change %= 0.05m;
int pennies = (int)(change / 0.01m);
// Output change
Console.WriteLine($"Change: {twenties} $20s, {tens} $10s, {fives} $5s, {ones} $1s, {quarters} quarters, {dimes} dimes, {nickels} nickels, {pennies} pennies");
Console.WriteLine("Thank you for using the Cashier Program!");