Hi everyone
Actually studding Business Management, but trying C# programming in my free time so I don't have tutor to ask about this.
Using Microsoft Visual Studio 2010
I have to write a program that prompts the user for the Cost of goods sold and the amount received by the customer.
There needs to be a class named clsCash with a method named GetChange () that accepts decimal arguments named Cost and CashRecieved. The integer arguments are: Hundreds, Fifties, Twenties, Tens, Fives, Twos, Ones, 50c, 10c, 5c, 2c, and 1c.
The program should calculate the exact number of notes and coins required to be returned as change by subtracting the Cost from the CashReceived.
This is what I have written but it's not working and does not accept coins without giving me an error:
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ChangeCalculator
{
class Program
{
static void Main(string[] args)
{
clsCash Money = new clsCash();
clsCash Paid = new clsCash();
Console.WriteLine("What is the cost of the goods?");
Money.Cost = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("How much was recived?");
Paid.CashRecieved = Convert.ToDecimal(Console.ReadLine());
Money.GetChange(Money.Cost, Paid.CashRecieved);
Console.Read();
}
}
class clsCash
{
private decimal cost;
private decimal cashRecieved;
public decimal Cost
{
get
{
return cost;
}
set
{
cost = value;
}
}
public decimal CashRecieved
{
get
{
return cashRecieved;
}
set
{
cashRecieved = value;
}
}
public void GetChange(decimal Cost, decimal CashRecieved)
{
// number to find values from
decimal change = CashRecieved - Cost;
int _hundreds = 100;
int _fifty = 50;
int _twenty = 20;
int _ten = 10;
int _five = 5;
int _two = 2;
int _one = 1;
int _centsfifty = 1/2;
int _centsten = 1/10;
int _centsfive = 1/20;
int _centstwo = 1/50;
int _centsone = 1/100;
int hundreds = (int)(change / _hundreds);
int fifty = (int)((change % _hundreds) / _fifty);
int twenty = (int)(((change % _hundreds) % _fifty) / _twenty);
int ten = (int)((((change % _hundreds) % _fifty) % _twenty) / _ten);
int five = (int)(((((change % _hundreds) % _fifty) % _twenty) % _ten) / _five);
int two = (int)((((((change % _hundreds) % _fifty) % _twenty) % _ten) % _five) / _two);
int one = (int)(((((((change % _hundreds) % _fifty) % _twenty) % _ten) % _five) % _two) / _one);
int centsfifty = (int)((((((((change % _hundreds) % _fifty) % _twenty) % _ten) % _five) % _two) % _one) /_centsfifty);
int centsten = (int)(((((((((change % _hundreds) % _fifty) % _twenty) % _ten) % _five) % _two) % _one) % _centsfifty) /_centsten);
int centsfive = (int)((((((((((change % _hundreds) % _fifty) % _twenty) % _ten) % _five) % _two) % _one) % _centsfifty) % _centsten) / _centsfive);
int centstwo = (int)(((((((((((change % _hundreds) % _fifty) % _twenty) % _ten) % _five) % _two) % _one) % _centsfifty) % _centsten) % _centsfive) /_centstwo);
int centsone = (int)((((((((((((change % _hundreds) % _fifty) % _twenty) % _ten) % _five) % _two) % _one) % _centsfifty) % _centsten) % _centsfive) % _centstwo) /_centsone);
{
if (change >= 100)
{
decimal rand = change / 100;
hundreds= (int)rand;
change = change % 100;
}
if (change >= 50)
{
decimal rand = (int)change / 50;
fifty = (int)rand;
change = change % 50;
}
if (change >= 20)
{
decimal rand = change / 20;
twenty = (int)rand;
change = change % 20;
}
//while (change > 0);
else if (change >= 50)
{
fifty = (int)change / 50;
change = change % 50;
}
else if (change >= 20)
{
twenty = (int)change / 20;
change = change % 20;
}
else if (change >= 10)
{
ten = (int)change / 10;
change = change % 10;
}
else if (change >= 5)
{
five = (int)change / 5;
change = change % 5;
}
else if (change >= 2)
{
two = (int)change / 2;
change = change % 2;
}
else if (change >= 1)
{
one = (int)change / 1;
change = change % 1;
}
else if (change < 1)
{
decimal fhu = change / 0.5m;
centsfifty = (int)fhu;
change = change % 0.5m;
Console.WriteLine("YOUR CHANGE IS:");
}
} while (change >= 0);
Console.WriteLine("YOUR CHANGE IS:");
Console.WriteLine("---------------");
Console.WriteLine("HUNDREDS RANDS \t: {0}", hundreds);
Console.WriteLine("FIFTY RANDS \t: {0}", fifty);
Console.WriteLine("TWENTY RANDS \t: {0}", twenty);
Console.WriteLine("TEN RANDS \t: {0}", ten);
Console.WriteLine("FIVE RANDS \t: {0}", five);
Console.WriteLine("TWO RANDS \t: {0}", two);
Console.WriteLine("ONE RANDS \t: {0}", one);
Console.WriteLine("50 CENTS \t: {0}", centsfifty);
Console.WriteLine("10 CENTS \t: {0}", centsten);
Console.WriteLine("2 CENTS \t: {0}", centstwo);
Console.WriteLine("1 CENTS \t: {0}", centsone);
}
}
}