Cash Machine II

Just a quick revisit to Cash machine with some quick UI

<!–DOCTYPE html>

xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title>Cash Machine II</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
Type Amount of $ you would like, please.
<asp:TextBox ID=”txtAmount1″ runat=”server”>
<asp:Button ID=”btnGetCash” runat=”server” Text=”Withdraw” OnClick=”btnGetCash_Click” />
</div>
</form>
</body>
</html>

 

protected void btnGetCash_Click(object sender, EventArgs e)
{
string msg = string.Empty;
Response.Write(Withdraw(System.Convert.ToDecimal(txtAmount1.Text), ref msg).ToString());
if (msg != null)
{
Response.Write(msg);
}
}

private decimal Withdraw(decimal amount, ref string message)
{
decimal currentAmount = 0;
int cashAvailable = 1000;

StringBuilder CashUsed = new StringBuilder(”
“);

if (amount > cashAvailable)
{
message = “Not enough funds available”;
return (decimal)0.0;
}

for (int i = 1; i < cashAvailable; i++)
{

if (currentAmount == amount)
{
message += CashUsed.ToString();
return currentAmount;
}
if (currentAmount < amount && ((amount – currentAmount) / 100) % 100 == 0 || currentAmount + 100 < amount && ((amount – currentAmount) / 100) / 100 > 0)
{
currentAmount += 100;
CashUsed.Append(“$100”);
}
if (currentAmount < amount && ((amount – currentAmount) / 10) % 10 == 0 || currentAmount + 10 < amount && ((amount – currentAmount) / 10) / 10 > 0)
{
currentAmount += 10;
CashUsed.Append(“$10”);
}
else if (currentAmount < amount && ((amount – currentAmount) / 5) % 5 == 0 || currentAmount + 10 < amount && ((amount – currentAmount) / 5) / 5 > 0)
{
currentAmount += 5;
CashUsed.Append(“$5”);
}
else if (currentAmount < amount && ((amount – currentAmount) / 1) % 1 == 0)
{
currentAmount += 1;
CashUsed.Append(“$1”);
}
}

return currentAmount;
}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s