Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Making a change dispenser!

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Making a change dispenser!

    Hey guys, could use some help on this program that I'm making.
    I'm working on a change dispensing program using a constructor, getters, setters, and a couple of different methods.

    So basically, this program takes a constructures that assumes that a change machine has been loaded with a roll of each type of coin. When it goes below 1 of that type of coin it adds another roll and records how many rolls it has added. It also reduces the penniesLeft, nickelsLeft, dimesLeft, and quartersLeft by how many of those coins are needed to make the amount of change. So when they go below 1 it adds a roll of change, I'm missing a writeReport() method which I should right it to System.out. I'm missing the constructor and my make change method that reduces the pennies left and calculates how many of each type is needed to make the correct change using as few coins is also messed up.
    I'm really lost at this point and am looking for some help, any would be appreciated, thank you.

    --- Update ---

    Here's what I want it to look like, http://i.imgur.com/1aemCiV.png

    --- Update ---

    Here's my code
    package changedispenser;
     
    public class ChangeDispenser {
     
        private static int quarters, dimes, nickels, pennies;
        private static int penniesLeft, nickelsLeft, dimesLeft, quartersLeft;
        private static int pennyRollsAdded = 1;
        private static int nickelRollsAdded = 1;
        private static int dimeRollsAdded = 1;
        private static int quarterRollsAdded = 1;
        public static final int PENNIES_PER_ROLL = 50;
        public static final int NICKELS_PER_ROLL = 40;
        public static final int DIMES_PER_ROLL = 50;
        public static final int QUARTERS_PER_ROLL = 40;
     
        public static void main(String[] args) {
     
     
     
        public String makeChange(int amount) {
     
     
            if (amount > 99 || amount < 0) {
                System.out.println("");
            }
     
            quarters = amount / 25;
     
            amount = amount % 25;
     
            dimes = amount / 10;
     
            amount = amount % 10;
     
            nickels = amount / 5;
     
            amount = amount % 5;
     
            pennies = amount;
     
     
     
     
            do {
     
                if (quarters != 0) {
                    System.out.print("  Quarters: " + quarters);
                }
                if (dimes != 0) {
                    System.out.print("  Dimes: " + dimes);
                }
                if (nickels != 0) {
                    System.out.print("  Nickels: " + nickels);
                }
                if (pennies != 0) {
                    System.out.println("  Pennies: " + pennies);
                }
     
     
     
     
     
                //Fix this so that it outputs the appropriate change IE: 23 cents is 2 dimes 3 pennies 
     
     
                System.out.println("Coins Left:");
                System.out.println("Quarters: " + quartersLeft);
                System.out.println("Dimes:    " + dimesLeft);
                System.out.println("Nickels:  " + nickelsLeft);
                System.out.println("Pennies:  " + penniesLeft);
                System.out.println("Rolls Added: ");
                System.out.println("Quarters: " + quarterRollsAdded);
                System.out.println("Dimes:    " + dimeRollsAdded);
                System.out.println("Nickels:  " + nickelRollsAdded);
                System.out.println("Pennies:  " + pennyRollsAdded);
            } while (amount > 0 && amount <= 99);
     
     
            return "Quarters: " + quarters + "  Dime: " + dimes + "   Nickels: " + nickels + "   Pennies: " + pennies;
     
        }
     
        public int getPenniesLeft() {
            return penniesLeft;
        }
     
        public void setPenniesLeft(int penniesLeft) {
            this.penniesLeft = penniesLeft;
     
            if (penniesLeft <= 0) {
                pennyRollsAdded = pennyRollsAdded++;
            }
        }
     
        public int getNickelsLeft() {
            return nickelsLeft;
        }
     
        public void setNickelsLeft(int nickelsLeft) {
            this.nickelsLeft = nickelsLeft;
     
            if (nickelsLeft <= 0) {
                nickelRollsAdded = nickelRollsAdded++;
            }
        }
     
        public int getDimesLeft() {
            return dimesLeft;
        }
     
        public void setDimesLeft(int dimesLeft) {
            this.dimesLeft = dimesLeft;
     
            if (dimesLeft <= 0) {
                dimeRollsAdded = dimeRollsAdded++;
            }
        }
     
        public int getQuartersLeft() {
            return quartersLeft;
        }
     
        public void setQuartersLeft(int quartersLeft) {
            this.quartersLeft = quartersLeft;
     
            if (quartersLeft <= 0) {
                quarterRollsAdded = quarterRollsAdded++;
            }
        }
     
        public int getPennyRollsAdded() {
            return pennyRollsAdded;
        }
     
        public void setPennyRollsAdded(int pennyRollsAdded) {
            this.pennyRollsAdded = pennyRollsAdded;
        }
     
        public int getNickelRollsAdded() {
            return nickelRollsAdded;
        }
     
        public void setNickelRollsAdded(int nickelRollsAdded) {
            this.nickelRollsAdded = nickelRollsAdded;
        }
     
        public int getDimeRollsAdded() {
            return dimeRollsAdded;
        }
     
        public void setDimeRollsAdded(int dimeRollsAdded) {
            this.dimeRollsAdded = dimeRollsAdded;
        }
     
        public int getQuarterRollsAdded() {
            return quarterRollsAdded;
        }
     
        public void setQuarterRollsAdded(int quarterRollsAdded) {
            this.quarterRollsAdded = quarterRollsAdded;
        }
    }
    Here's my change driver,
    package changedispenser;
     
    import java.util.Random;
     
     
    public class ChangeDispenserDriver {
     
        public static void main(String[] args) {
            ChangeDispenser changeMachine = new ChangeDispenser();
            Random rand = new Random();
            int amount;
            for (int i = 0; i < 1000; i++) {
                amount = rand.nextInt(99) + 1;
                System.out.println("Amount: " + amount + ": Change = " + changeMachine.makeChange(amount));
            }
            changeMachine.writeReport();
        }
    }


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Making a change dispenser!

    if (penniesLeft <= 0) {
    pennyRollsAdded = pennyRollsAdded++;
    }


    that can be reduced to

    if (penniesLeft <=0)
    {
    pennyRollsAdded++;
    }


    What exactly are you confused on?

    Is amount in pennies?


    Your makeChange() method doesn't change the value of quarters left, dimes left, nickels left, and pennies left.

    (It will, if you tell it to print out just quarters, dimes, nickels, and pennies, print out that, for 23 cents, that you need two dimes and three pennies.


    23 / 25 = 0 ;
    23 % 25 = 23;
    23 /10 = 2;
    23 % 10 = 3;
    3 / 5 = 0;
    3 % 5 = 3;
    3 /1 = 3;

    2 dimes and 3 pennies.

    For $1.23, it should do this

    123 /25 = 4; // 4 quarters
    123 % 25 = 23;
    23 / 10 = 2; // 2 dimes
    23 % 10 = 3;
    3/ 5 = 0;
    3 % 5 = 3;
    3 /1 = 3; // 3 pennies

    If I got this straight, once you run out of quarters, dimes, nickels, and pennies, you add another roll of quarters, dimes, nickels, and pennies.


    So, once it calculates the stuff above, and you have your quarters, nickels, dimes, and pennies that will need to be used, you'll subtract the quarters needed from quartersLeft. You'll subtract the dimes needed from dimesLeft, and so on.

    Also, your code in adding a roll will only go into effect AFTER you subtract the amount from what you have left, meaning that you run the possibility of going into negative quarters, dimes, nickels, and pennies left BEFORE you add the roll.

    When you add a roll, does it update the amount of quarters, nickels, dimes, and pennies? (Even if it does, it would be hard in reality to make change with quarters, dimes, nickels, and pennies that you don't have. I know, I know, the federal government does it all the time, but still.)

    Also, in addition to that, if adding a roll doesn't change anything on quarters, dimes, nickels, or pennies left, then you will KEEP adding a roll every time quarters, dimes, nickels, or pennies are needed and they have gone at or below 0 (which will be every time you need them once you reach or go below 0).

    To update, say, quarters left, in makeChange()

    setQuartersLeft(quartersLeft - quarters);

  3. The Following User Says Thank You to GoodbyeWorld For This Useful Post:

    Tonno22 (October 29th, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Making a change dispenser!

    Sorry, I've since changed my code, it now looks like this. My build is successful, but is not outputting anything. I'm not sure why,

    package changedispenser;
     
    public class ChangeDispenser {
     
        private static int quarters, dimes, nickels, pennies;
        private static int penniesLeft, nickelsLeft, dimesLeft, quartersLeft;
        private static int pennyRollsAdded = 1;
        private static int nickelRollsAdded = 1;
        private static int dimeRollsAdded = 1;
        private static int quarterRollsAdded = 1;
        public static final int PENNIES_PER_ROLL = 50;
        public static final int NICKELS_PER_ROLL = 40;
        public static final int DIMES_PER_ROLL = 50;
        public static final int QUARTERS_PER_ROLL = 40;
     
        public static void main(String[] args) {
     
     
     
        }
     
     
     
     
     
     
        public int getPenniesLeft() {
            return penniesLeft;
        }
     
        public void setPenniesLeft(int penniesLeft) {
            this.penniesLeft = penniesLeft;
     
            if (penniesLeft <= 0) {
                pennyRollsAdded = pennyRollsAdded++;
            }
        }
     
        public int getNickelsLeft() {
            return nickelsLeft;
        }
     
        public void setNickelsLeft(int nickelsLeft) {
            this.nickelsLeft = nickelsLeft;
     
            if (nickelsLeft <= 0) {
                nickelRollsAdded = nickelRollsAdded++;
            }
        }
     
        public int getDimesLeft() {
            return dimesLeft;
        }
     
        public void setDimesLeft(int dimesLeft) {
            this.dimesLeft = dimesLeft;
     
            if (dimesLeft <= 0) {
                dimeRollsAdded = dimeRollsAdded++;
            }
        }
     
        public int getQuartersLeft() {
            return quartersLeft;
        }
     
        public void setQuartersLeft(int quartersLeft) {
            this.quartersLeft = quartersLeft;
     
            if (quartersLeft <= 0) {
                quarterRollsAdded = quarterRollsAdded++;
            }
        }
     
        public int getPennyRollsAdded() {
            return pennyRollsAdded;
        }
     
        public void setPennyRollsAdded(int pennyRollsAdded) {
            this.pennyRollsAdded = pennyRollsAdded;
        }
     
        public int getNickelRollsAdded() {
            return nickelRollsAdded;
        }
     
        public void setNickelRollsAdded(int nickelRollsAdded) {
            this.nickelRollsAdded = nickelRollsAdded;
        }
     
        public int getDimeRollsAdded() {
            return dimeRollsAdded;
        }
     
        public void setDimeRollsAdded(int dimeRollsAdded) {
            this.dimeRollsAdded = dimeRollsAdded;
        }
     
        public int getQuarterRollsAdded() {
            return quarterRollsAdded;
        }
     
        public void setQuarterRollsAdded(int quarterRollsAdded) {
            this.quarterRollsAdded = quarterRollsAdded;
        }
        public String makeChange(int amount) {
     
     
            if (amount > 99 || amount < 0) {
                System.out.println("");
            }
     
            quarters = amount / 25;
     
            amount = amount % 25;
     
            dimes = amount / 10;
     
            amount = amount % 10;
     
            nickels = amount / 5;
     
            amount = amount % 5;
     
            pennies = amount;
     
     
     
     
            do {
     
                if (quarters != 0) {
                    System.out.print("  Quarters: " + quarters);
                }
                if (dimes != 0) {
                    System.out.print("  Dimes: " + dimes);
                }
                if (nickels != 0) {
                    System.out.print("  Nickels: " + nickels);
                }
                if (pennies != 0) {
                    System.out.println("  Pennies: " + pennies);
                }
            } while (amount > 0 && amount <= 99);
     
           return "Quarters: " + quarters + "  Dime: " + dimes + "   Nickels: " + nickels + "   Pennies: " + pennies;
     
        }
        public void writeReport() {
     
     
            System.out.println("Coins Left:");
            System.out.println("Quarters: " + quartersLeft);
            System.out.println("Dimes:    "+ dimesLeft);
            System.out.println("Nickels:  " + nickelsLeft);
            System.out.println("Pennies:  " +penniesLeft);   
            System.out.println("Rolls Added: ");
            System.out.println("Quarters: "+ quarterRollsAdded);
            System.out.println("Dimes:    " + dimeRollsAdded);
            System.out.println("Nickels:  " + nickelRollsAdded);
            System.out.println("Pennies:  " + pennyRollsAdded);
     
     
     
     
     
        }
     
    }
    and here is the seperate driver class that helps run the program

    package changedispenser;
     
    import java.util.Random;
     
     
    public class ChangeDispenserDriver {
     
        public static void main(String[] args) {
            ChangeDispenser changeMachine = new ChangeDispenser();
            Random rand = new Random();
            int amount;
            for (int i = 0; i < 1000; i++) {
                amount = rand.nextInt(99) + 1;
                System.out.println("Amount: " + amount + ": Change = " + changeMachine.makeChange(amount));
            }
            changeMachine.writeReport();
        }
    }


    --- Update ---

    The program only makes change for change between 0-99 thats why I have the if statement there. What do you mean by telling it to just print out those? And yes, once you run out of those it should add another roll, I thought I had it right though, why will it only do it after I subtract the amount from what I have left? When I add a roll it should update the amount of coins.[COLOR="Silver"]

  5. #4
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Making a change dispenser!

    Quote Originally Posted by Tonno22 View Post
    Sorry, I've since changed my code, it now looks like this. My build is successful, but is not outputting anything. I'm not sure why,

    package changedispenser;
     
    public class ChangeDispenser {
     
        private static int quarters, dimes, nickels, pennies;
        private static int penniesLeft, nickelsLeft, dimesLeft, quartersLeft;
        private static int pennyRollsAdded = 1;
        private static int nickelRollsAdded = 1;
        private static int dimeRollsAdded = 1;
        private static int quarterRollsAdded = 1;
        public static final int PENNIES_PER_ROLL = 50;
        public static final int NICKELS_PER_ROLL = 40;
        public static final int DIMES_PER_ROLL = 50;
        public static final int QUARTERS_PER_ROLL = 40;
     
        public static void main(String[] args) {
     
     
     
        }
     
     
     
     
     
     
        public int getPenniesLeft() {
            return penniesLeft;
        }
     
        public void setPenniesLeft(int penniesLeft) {
            this.penniesLeft = penniesLeft;
     
            if (penniesLeft <= 0) {
                pennyRollsAdded = pennyRollsAdded++;
            }
        }
     
        public int getNickelsLeft() {
            return nickelsLeft;
        }
     
        public void setNickelsLeft(int nickelsLeft) {
            this.nickelsLeft = nickelsLeft;
     
            if (nickelsLeft <= 0) {
                nickelRollsAdded = nickelRollsAdded++;
            }
        }
     
        public int getDimesLeft() {
            return dimesLeft;
        }
     
        public void setDimesLeft(int dimesLeft) {
            this.dimesLeft = dimesLeft;
     
            if (dimesLeft <= 0) {
                dimeRollsAdded = dimeRollsAdded++;
            }
        }
     
        public int getQuartersLeft() {
            return quartersLeft;
        }
     
        public void setQuartersLeft(int quartersLeft) {
            this.quartersLeft = quartersLeft;
     
            if (quartersLeft <= 0) {
                quarterRollsAdded = quarterRollsAdded++;
            }
        }
     
        public int getPennyRollsAdded() {
            return pennyRollsAdded;
        }
     
        public void setPennyRollsAdded(int pennyRollsAdded) {
            this.pennyRollsAdded = pennyRollsAdded;
        }
     
        public int getNickelRollsAdded() {
            return nickelRollsAdded;
        }
     
        public void setNickelRollsAdded(int nickelRollsAdded) {
            this.nickelRollsAdded = nickelRollsAdded;
        }
     
        public int getDimeRollsAdded() {
            return dimeRollsAdded;
        }
     
        public void setDimeRollsAdded(int dimeRollsAdded) {
            this.dimeRollsAdded = dimeRollsAdded;
        }
     
        public int getQuarterRollsAdded() {
            return quarterRollsAdded;
        }
     
        public void setQuarterRollsAdded(int quarterRollsAdded) {
            this.quarterRollsAdded = quarterRollsAdded;
        }
        public String makeChange(int amount) {
     
     
            if (amount > 99 || amount < 0) {
                System.out.println("");
            }
     
            quarters = amount / 25;
     
            amount = amount % 25;
     
            dimes = amount / 10;
     
            amount = amount % 10;
     
            nickels = amount / 5;
     
            amount = amount % 5;
     
            pennies = amount;
     
     
     
     
            do {
     
                if (quarters != 0) {
                    System.out.print("  Quarters: " + quarters);
                }
                if (dimes != 0) {
                    System.out.print("  Dimes: " + dimes);
                }
                if (nickels != 0) {
                    System.out.print("  Nickels: " + nickels);
                }
                if (pennies != 0) {
                    System.out.println("  Pennies: " + pennies);
                }
            } while (amount > 0 && amount <= 99);
     
           return "Quarters: " + quarters + "  Dime: " + dimes + "   Nickels: " + nickels + "   Pennies: " + pennies;
     
        }
        public void writeReport() {
     
     
            System.out.println("Coins Left:");
            System.out.println("Quarters: " + quartersLeft);
            System.out.println("Dimes:    "+ dimesLeft);
            System.out.println("Nickels:  " + nickelsLeft);
            System.out.println("Pennies:  " +penniesLeft);   
            System.out.println("Rolls Added: ");
            System.out.println("Quarters: "+ quarterRollsAdded);
            System.out.println("Dimes:    " + dimeRollsAdded);
            System.out.println("Nickels:  " + nickelRollsAdded);
            System.out.println("Pennies:  " + pennyRollsAdded);
     
     
     
     
     
        }
     
    }
    and here is the seperate driver class that helps run the program

    package changedispenser;
     
    import java.util.Random;
     
     
    public class ChangeDispenserDriver {
     
        public static void main(String[] args) {
            ChangeDispenser changeMachine = new ChangeDispenser();
            Random rand = new Random();
            int amount;
            for (int i = 0; i < 1000; i++) {
                amount = rand.nextInt(99) + 1;
                System.out.println("Amount: " + amount + ": Change = " + changeMachine.makeChange(amount));
            }
            changeMachine.writeReport();
        }
    }
    Not sure why your ChangeDispenser class has a main method (a blank one in fact). It doesn't really need it. Your ChangeDispenserDriver has one and that appears to be the one you're using.



    private static int quarterRollsAdded = 1;


    I wouldn't make it static. static means that it is called by the class name; you can't access it outside the ChangeDispenser class as it is private. None of your methods are static making it required for those private variables to be static.

    As for fixing makeChange(), I suggested earlier that, once you know how many quarters, dimes, nickels, and pennies you will need, you call

    setQuartersLeft(quartersLeft - quarters);
    setDimesLeft(dimesLeft - dimes);
    , etc.

    Also, as I said earlier, your setQuartersLeft() and the other setLeft methods subtract first, then see if you need to add a roll. They don't stop you from going into negatives.

    In setQuartersLeft, it has this:

    this.quartersLeft = quartersLeft;

    if (quartersLeft <= 0) {
    quarterRollsAdded = quarterRollsAdded++;


    If the value passed to the method happens to be negative, it'll set the value of quartersLeft to that negative value. And, using your order of execution, that will happen BEFORE you add the roll.

    Say that you had 1 quarter left and it was told to make change for 50 cents. Then it would need 2 quarters. So, after subtracting the quarters you have (1) from the quarters you need (2), then it will have a value of -1 quarters left. After setting the value of quartersLeft to -1, it will add a roll. (Though, adding the roll change the amount at least from your code that I can see anyway, of quartersLeft. As it would stand, assuming you updated it to subtract the quarters you need from quarters left using setQuartersLeft(quartersLeft - quarters), then, if, again, it needed 50 cents, it would take 2 quarters. You had -1 quarters so it would take 2 more from that so quartersLeft would be -3. It would see that it was negative and add another roll. But, once more, it wouldn't change your value of quartersLeft, ensuring that it will add a role again if another quarter is ever needed.)

  6. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Making a change dispenser!

    I changed my makechange() method to this,

    public String makeChange(int amount) {
     
            while (amount > 0 && amount <=99) 
        if (amount > 99) {
                System.out.println("");
     
            quarters = amount / 25;
     
            amount = amount % 25;
     
            dimes = amount / 10;
     
            amount = amount % 10;
     
            nickels = amount / 5;
     
            amount = amount % 5;
     
            pennies = amount;
     
     
        }   return ("Quarters: " + quarters + "  Dime: " + dimes + "   Nickels: " + nickels + "   Pennies: " + pennies);
        }


    --- Update ---

    In my main method now, I have
    if (quarters != 0) {
            System.out.print("  Quarters: " + quarters);
            }
            if (dimes != 0) {
            System.out.print("  Dimes: " + dimes);
            }
            if (nickels != 0) {
            System.out.print("  Nickels: "+ nickels);    
            }
            if (pennies !=0) {
            System.out.println("  Pennies: " + pennies);    
            }

    I have that so it would make the out put only the coins needed and not the others.

    Also I get an error if I don't have a main method.

    --- Update ---

    Could you give me an example of what one of the setters should be , I'm really confused about this. Also I got rid of my main method.

Similar Threads

  1. how to change DB design for a change in java
    By harry7ster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 7th, 2013, 12:57 PM
  2. change Name
    By anis.laghaei in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 3rd, 2012, 05:40 PM
  3. [SOLVED] Help with my Java program: Making change from an entered double.
    By iDizzle in forum What's Wrong With My Code?
    Replies: 20
    Last Post: March 18th, 2012, 03:16 PM
  4. Making change
    By Jerick in forum Algorithms & Recursion
    Replies: 3
    Last Post: October 7th, 2011, 06:49 PM
  5. [SOLVED] Can't get boolean to change!!
    By Day2Day in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 2nd, 2010, 07:20 PM