|
Example: Cashier Change |
|
|
This application shows how to assist the cashier of a
store (super market, clothing store, hardware store, etc) figure out how
much change to give to a customer after registering some items that have
just been bought. The calculation produces the counts of dollar bills and
coins in American currency.
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cashier_Change1
{
public class Program
{
static int Main(string[] args)
{
// This variable is the value that will be considered
string strAmount;
// This is the actual monetary value that will be used
double Amount = 0.00;
// This will be an accessory used during calculation
double NewAmount = 0.00;
int Decimals = 0;
// These are the values that will be displayed
int Fifties = 0;
int Twenties = 0;
int Tens = 0;
int Fives = 0;
int Ones = 0;
int Quarters = 0;
int Dimes = 0;
int Nickels = 0;
int Pennies = 0;
try
{
Console.Write("Enter the amount: ");
strAmount = Console.ReadLine();
int IndexOfPeriod = strAmount.IndexOf(".");
// This variable is used to locate the decimal side
string strDecimals = strAmount.Substring(IndexOfPeriod+1, 2);
Amount = double.Parse(strAmount);
Decimals = int.Parse(strDecimals);
if (Amount > 50)
{
Fifties = (int)(Amount / 50);
NewAmount = Amount - 50;
Twenties = (int)(NewAmount / 20);
NewAmount -= (Twenties * 20);
Tens = (int)(NewAmount / 10);
NewAmount -= (Tens * 10);
Fives = (int)(NewAmount / 5);
NewAmount -= (Fives * 5);
Ones = (int)(NewAmount);
}
else
{
NewAmount = Amount;
Twenties = (int)(NewAmount / 20);
NewAmount -= (Twenties * 20);
Tens = (int)(NewAmount / 10);
NewAmount -= (Tens * 10);
Fives = (int)(NewAmount / 5);
NewAmount -= (Fives * 5);
Ones = (int)(NewAmount);
}
Quarters = (int)(Decimals / 25);
Decimals -= Quarters * 25;
Dimes = (int)(Decimals / 10);
Decimals -= Dimes * 10;
Nickels = (int)(Decimals / 5);
Decimals -= Nickels * 5;
Pennies = Decimals;
Console.WriteLine("=======================");
Console.WriteLine("Amount Entered: {0}", Amount);
Console.WriteLine("-----------------------");
if( Fifties != 0 )
Console.WriteLine("Fifties: {0}", Fifties);
if( Twenties != 0 )
Console.WriteLine("Twenties: {0}", Twenties);
if( Tens != 0 )
Console.WriteLine("Tens: {0}", Tens);
if( Fives != 0 )
Console.WriteLine("Fives: {0}", Fives);
if( Ones != 0 )
Console.WriteLine("Ones: {0}", Ones);
Console.WriteLine("-----------------------");
if( Quarters != 0 )
Console.WriteLine("Quarters: {0}", Quarters);
if( Dimes != 0 )
Console.WriteLine("Dimes: {0}", Dimes);
if( Nickels != 0 )
Console.WriteLine("Nikels: {0}", Nickels);
if( Pennies != 0 )
Console.WriteLine("Pennies: {0}", Pennies);
Console.WriteLine("=======================");
}
catch (FormatException)
{
Console.WriteLine("Invlid Value!");
}
Console.WriteLine();
return 0;
}
}
}
Here is an example or running the application:
Enter the amount: 4.06
=======================
Amount Entered: 4.06
-----------------------
Ones: 4
-----------------------
Nikels: 1
Pennies: 1
=======================
Press any key to continue . . .
Here is another example of running the application:
Enter the amount: 42.68
=======================
Amount Entered: 42.68
-----------------------
Twenties: 2
Ones: 2
-----------------------
Quarters: 2
Dimes: 1
Nikels: 1
Pennies: 3
=======================
Press any key to continue . . .
Here is another example of running the application:
Enter the amount: 88.68
=======================
Amount Entered: 88.68
-----------------------
Fifties: 1
Twenties: 1
Tens: 1
Fives: 1
Ones: 3
-----------------------
Quarters: 2
Dimes: 1
Nikels: 1
Pennies: 3
=======================
Press any key to continue . . .
|