Permalink
Cannot retrieve contributors at this time
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?
Cashier/Program.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
70 lines (57 sloc)
2.49 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); |