0

Hey guys, I'm having trouble with a program that I can't seem to get to work. It's supposed to be able to give you back your change in $130 bills, $55 bills, $25 bills, $5 bills, $1 bills, $.75 cent coins, $.30 cent coints, and $.01 cent coints. My program works for the most part in java but my outputs for the pennies are always off by one. Can anyone tell me what's wrong with my code or how to fix it?

public static void main(String [] args)
    {
        System.out.println("What is the cost of the item?");
        double cost=IO.readDouble();

        System.out.println("How much money do you have?");
        double money=IO.readDouble();

        change(cost, money);
    }
        public static void change(double cost, double money)
        {

            int bill130=0;
            int bill55=0;
            int bill25=0;   
            int bill5=0;
            int bill1=0;
            int cent75=0;
            int cent30=0;
            int cent1=0;
            double change=money-cost;

            if (cost<=money && cost>0)
            {
                if(change%130==0)
                {
                    bill130=(int)(change/130);
                }
                else
                {
                    bill130=(int)(change/130);
                    change=change%130;

                    if(change%55==0)
                    {
                        bill55=(int)(change/55);
                    }
                    else
                    {
                        bill55=(int)(change/55);
                        change=change%55;

                        if(change%25==0)
                        {
                            bill25=(int)(change/25);
                        }
                        else
                        {
                            bill25=(int)(change/25);
                            change=change%25;

                            if(change%5==0)
                            {
                                bill5=(int)(change/5);
                            }
                            else
                            {
                                bill5=(int)(change/5);
                                change=change%5;

                                if(change%1==0)
                                {
                                    bill1=(int)(change/1);
                                }
                                else
                                {
                                    bill1=(int)(change/1.0);
                                    change=change%1.0;

                                    if(change%.75==0)
                                    {
                                        cent75=(int)(change/.75);
                                    }
                                    else
                                    {
                                        cent75=(int)(change/.75);
                                        change=change%.75;

                                        if(change%.30==0)
                                        {
                                            cent30=(int)(change/.30);
                                        }
                                        else
                                        {
                                            cent30=(int)(change/.30);
                                            change=change%.30;

                                            if(change%.01==0)
                                            {
                                                cent1=(int)(change/.01);
                                            }
                                            else
                                            {

                                                cent1=(int)(change/.01);
                                                change=change%.01;
                                            }

                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                IO.outputIntAnswer(bill130);
                IO.outputIntAnswer(bill55);
                IO.outputIntAnswer(bill25);
                IO.outputIntAnswer(bill5);
                IO.outputIntAnswer(bill1);
                IO.outputIntAnswer(cent75);
                IO.outputIntAnswer(cent30);
                IO.outputIntAnswer(cent1);

            }

            else if(money<cost)
            {
                IO.reportBadInput();
                IO.outputIntAnswer(-1);
            }
            else  if(money<0 || cost<0)
            {
                IO.reportBadInput();
                IO.outputIntAnswer(-1);
            }
            else 
            {
                IO.reportBadInput();
                IO.outputIntAnswer(-1);                         
            }
        }
}
2
Contributors
1
Reply
24
Views
1 Month
Discussion Span
Last Post by Reverend Jim
0

Your approach is completely wrong. You should be creating an array of denominations from largest to smallest, then using a loop to remove multiples of each decreasing denomination. Once you have your array set up, your code to do the change shouldn't be more than 10 lines.

Edited by Reverend Jim

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.