Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello All I am working on a C# console application where I have to represent money and display how many bills were used to make each amount. I can do this The only problem I am working on figuring out is recording the number of each bill that has been used and to subtract it for the current number. I was thinking of making a money class for 100s 50s 20s 10s 5s and 1s but When I have it in my Main() I cant have each value bill with a specified number of how many. Having the value specificed I would have this : but each one of these has no value amount
C#
int hund = 10;
            int fif = 10;
            int twen = 10;
            int ten = 10;
            int five = 10;
            int one = 10;

Or if I want a value amount I was thinking this But I cant determine a fixed number to use
C#
public class Money
    {
        public int HundBill { get; set; }
        public int FiftBill { get; set; }
        public int TwentBill { get; set; }
        public int TenBill { get; set; }
        public int FiveBill { get; set; }
        public int OneBill { get; set; }

        public Money()
        {
            HundredDollarBill = cash / 100;
            cash %= 100;
            FiftyDollarBill = cash / 50;
            cash %= 50;
            TwentyDollarBill = cash / 20;
            cash %= 20;
            TenDollarBill = cash / 10;
            cash %= 10;
            FiftyDollarBill = cash / 5;
            cash %= 5;
            OneDollarBill = cash / 1;
            cash %= 1;      }

I am getting the users input and Ill get a number and I want to minus the number of bills that were used to make that amount and to keep track.

What I have tried:

Keeping track of what has been used in a variable so I can minus the dollar bills used and then I want to restock but thatll be easy after i figure this part out
Posted
Updated 8-Feb-19 16:46pm
Comments
enhzflep 8-Feb-19 21:29pm    
When working with money, the standard approach is to use integer-only arithmetic in order to avoid any errors introduced by the lack of precision when dealing with floating-point numbers.

In order to do this, one simply records the number of cents or whatever the smallest unit of currency is. You'll only ever have whole numbers of them and will avoid problems of accuracy..

From there, you can simply keep an array that has one element for each item of currency. You simply increment the correct element each time another piece of money is encountered and voilla! You know how much you've got and how you received it.
TheBigBearNow 8-Feb-19 22:01pm    
So basically like what I have make a money class like I did and how i have this
HundredDollarBill = cash / 100;
            cash %= 100;

And I can use an array or list to keep track of money.hundbill => 10 ?
Could I get a little example I think I am understanding what you mean but if I see it then for 1 bill ill know if I am understanding it right

1 solution

I think you have another problem
Quote:
I can do this The only problem I am working on figuring out is recording the number of each bill that has been used and to subtract it for the current number.

Your code compute the best possible answer, it just don't take into account that you have to have enough banknotes to make the amount.
Your code
Java
HundredDollarBill = cash / 100;  // compute number of notes needed
cash %= 100;  // and subtract from amount

What you need to do is
Java
HundredDollarBill = cash / 100;  // compute number of notes needed
HundredDollarBill = ...  // check number of notes available
cash -= HundredDollarBill * 100;  // subtract those notes from amount
 
Share this answer
 
Comments
TheBigBearNow 8-Feb-19 23:08pm    
So this is what I have come up with and it has done everything right except for I did a value of 258 and it gave me a ten with the other correct values. It just gave me a extra 10 but everything else I tried worked..
        public static int[] MakeChange(int amount)
        {
            int[] cashBills = { 100, 50, 20, 10, 5, 1 };
            int[] billCounts = { 0, 0, 0, 0, 0, 0 };

            int billrem = 0;    //bill=0
            int reminder = amount;  //remainder
            while(reminder > 0)
            {

                billCounts[billrem] = reminder / cashBills[billrem];

                reminder = amount % cashBills[billrem];
                billrem++;
            }
            return billCounts;
        }
Patrice T 9-Feb-19 3:41am    
To help understand what is going on, the debugger is the tool to use.
TheBigBearNow 8-Feb-19 23:41pm    
I ended up going with a different direction

My remainder was adding 10 to the amount after it got down to 8 for some odd reason. So I switched to a for loop with the amount of kind of bills being used and an if to minus the amount left. Now I got this working properly. So onto the next steps keeping track.
Thank you
TheBigBearNow 9-Feb-19 0:15am    
I am getting the users input and Ill get a number and I want to minus the number of bills that were used to make that amount and to keep track.

Quick question to set the spot in an array each value why is my spot not able to assign a position?
       public static int[] Reset( int[] billsRefill)
        {
            //int[] cashRefill = { 10, 10, 10, 10, 10, 10 };
            foreach(int spot in billsRefill)
            {
                spot = 10;
            }
            return billsRefill;
        }
TheBigBearNow 9-Feb-19 1:49am    
Thank you skydiver
That helped explaining alot to me.

Now I have a new question.
I have an array of the amount of bills I have to be used.
I am subtracting a bill each time i use one.
my 100s are working but when I go to the next bill the remainder is wrong so i cant move on here is some of the stuff i tried..
public static void countbills(int amount, int[] bills, int[] billsCount)
        {
            int remainder = amount;
            int billIterator = 1;
            for(int i = 0; i < billsCount.Length; i++)   // while(remainder > 0)
            {
                //if(remainder >= bills[billIterator])  //{  if(billsCount[])  }
                if (remainder >= bills[i])
                {
                    if (billsCount[i] != 0)
                    {
                        billsCount[i]--;
                        remainder -= bills[billIterator];//bills[i] * billsCount[billIterator];
                        billIterator++;
                        //= bills[i];//amount / bills[i];     //remainder -= counts[bill] * dollarSizes[bill]; //reminder = amount % cashBills[billrem];
                        //amount -= billsCount[i] * bills[i];
                        //billsCount[i]--;
                    }
                }
            }
        }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900