Skip to content

Instantly share code, notes, and snippets.

@rwk130
Created September 26, 2017 13:52
Show Gist options
  • Save rwk130/67971daab67945ef8bc056b2eec7cc68 to your computer and use it in GitHub Desktop.
Save rwk130/67971daab67945ef8bc056b2eec7cc68 to your computer and use it in GitHub Desktop.
Crazy Cash Register, written in .Net C#. Takes the amount owed and the amount paid and returns how to give back the change in the least amount of coins and bills; UNLESS the amount owed is divisible by 3, in which case the change is given in random bills and coins.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CashRegister.aspx.cs" Inherits="CashRegister" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Creative Cash Draw Solutions - Register</title>
<style>
h1 { text-align: center; }
h3 { font-size: small; text-align: justify; }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Cash Register FUN</h1>
<h3>This is a simple page that allows you to enter how much a customer owes, how much they paid, and it will tell you the simpliest amount of bills and coins to return. <b><i>UNLESS</i></b> the amount
owed is divisible by 3, in which case the bills and coins returned will be chosen at random (though still adding up to the correct amount).
</h3>
<table>
<tr>
<td>Owed:</td>
<td><asp:TextBox ID="txtTotal" runat="server" placeholder="Total amount due" /></td>
</tr>
<tr>
<td>Paid:</td>
<td><asp:TextBox ID="txtPaid" runat="server" placeholder="Total amount paid" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><asp:Button ID="btnGetChange" runat="server" Text="Get change" OnClick="btnGetChange_Click" /></td>
</tr>
</table>
<br /><br />
<asp:Label ID="lblChange" runat="server" />
</div>
</form>
</body>
</html>
using System;
public partial class CashRegister: System.Web.UI.Page
{
double gChange; //Change global variable
protected void Page_Load( object sender, EventArgs e )
{
}
private void MakeRandomChange( double mTotal, double mPaid )
{
int mDollars = 0;
int mQuarters = 0;
int mDimes = 0;
int mNickels = 0;
int mPennies = 0;
Random mRand = new Random();
int mMax=5;
while( gChange > 0 )
{
/* First determine what types of change are elligible
* Type 5 is dollars, 4 is quarters, 3 is dimes, 2 is nickels, and 1 is pennies
*/
if( gChange > 1 )
{
mMax = 5;
}
else if( gChange > .25 )
{
mMax = 4;
}
else if( gChange > .10 )
{
mMax = 3;
}
else if( gChange > .05 )
{
mMax = 2;
}
else
{
mMax = 1;
}
//Choose a change type at random
int mType = mRand.Next( 0, mMax ) + 1;
double mTemp = gChange;
//Add count of change type and adjust change amount
switch( mType )
{
case 2:
mNickels++;
gChange -= .05;
break;
case 3:
mDimes++;
gChange -= .10;
break;
case 4:
mQuarters++;
gChange -= .25;
break;
case 5:
mDollars++;
gChange--;
break;
default:
mPennies++;
gChange -= .01;
break;
}
gChange = Math.Round( gChange, 2, MidpointRounding.AwayFromZero );
}
//add new result to screen
FillLabel( mDollars, mQuarters, mDimes, mNickels, mPennies );
}
private void MakeChange( double aTotal, double aPaid )
{
int mDollars = GetChangeCount( 1.0 ); //Get dollars for change
int mQuarters = GetChangeCount( .25 ); //Get quarters for change
int mDimes = GetChangeCount( .10 );
int mNickels = GetChangeCount( .05 );
int mPennies = GetChangeCount( .01 );
FillLabel( mDollars, mQuarters, mDimes, mNickels, mPennies );
}
//Generate string to return to screen
private void FillLabel( int aDollars, int aQuarters, int aDimes, int aNickels, int aPennies )
{
string mResult = ListChange( aDollars, "dollar" );
mResult += ", " + ListChange( aQuarters, "quarter" );
mResult += ", " + ListChange( aDimes, "dime" );
mResult += ", " + ListChange( aNickels, "nickel" );
mResult += ", " + ListChange( aPennies, "penny" );
while( mResult.Contains( ", ," ) )
{
mResult = mResult.Replace( ", ,", "," );
}
while( mResult.StartsWith( ", " ) )
{
mResult = mResult.Substring( 2 );
}
lblChange.Text += mResult + "<br /><br />";
}
//Generate part of return string specific to current amount (dollar, quarter, dime, etc.)
private string ListChange( int aCount, string aName )
{
string mList = String.Empty;
if( aCount > 0 )
{
mList = aCount.ToString() + " " + aName;
}
if( aCount > 1 )
{
mList += "s";
mList = mList.Replace( "y", "ie" );
}
return mList;
}
//Get count of change type based on worth (dollar = 1, quarter = .25, etc.)
private int GetChangeCount( double aAmount )
{
int mCount = 0;
while( gChange >= aAmount )
{
mCount++;
gChange -= aAmount;
gChange = Math.Round( gChange, 2, MidpointRounding.AwayFromZero );
}
return mCount;
}
protected void btnGetChange_Click( object sender, EventArgs e ) {
try {
lblChange.Text = String.Empty;
double mTotal = 0;
double mPaid = 0;
//parse string into doubles. If they error, set amount as -1
if( !double.TryParse( txtTotal.Text.Replace("$",""), out mTotal ) ) {
mTotal = -1;
}
try {
if( !double.TryParse( txtPaid.Text.Replace( "$", "" ), out mPaid ) ) {
mPaid = -1;
}
}
catch {
mPaid = -1;
}
gChange = Math.Round( mPaid - mTotal, 2, MidpointRounding.AwayFromZero );
if( mTotal < 0 ) //Total not parsed to double
{
lblChange.Text += "Error parsing total owed (" + txtTotal.Text + ") to dollar amount<br /><br />";
}
else if( mPaid < 0 )//amount paid not parsed to double
{
lblChange.Text += "Error parsing amount paid (" + txtPaid.Text + ") to dollar amount<br /><br />";
}
else if( gChange < 0 ) //amount paid is less than amount owed
{
lblChange.Text += String.Format( "{0:C}", gChange * -1 ) + " is still owed<br /><br />";
}
else if( gChange == 0 ) //paid exact amount owed
{
lblChange.Text += "No change<br /><br />";
}
else if( ( mTotal * 100 ) % 3 == 0 ) //if total is divisible by 3, give random change
{
lblChange.Text += String.Format( "{0:C}", gChange ) + " is to be paid back in:<br />";
MakeRandomChange( mTotal, mPaid );
}
else {
lblChange.Text += String.Format( "{0:C}", gChange ) + " is to be paid back in:<br />";
MakeChange( mTotal, mPaid );
}
}
catch {
}
}
}
public partial class CashRegister {
/// <summary>
/// form1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlForm form1;
/// <summary>
/// txtTotal control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.TextBox txtTotal;
/// <summary>
/// txtPaid control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.TextBox txtPaid;
/// <summary>
/// btnGetChange control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btnGetChange;
/// <summary>
/// lblChange control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label lblChange;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment