Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default A little help please?

    I have an assignment at the moment that I'm a bit stuck on. We're doing a very simple "vending machine" type program that only accepts dollar bills and outputs the correct change (in quarters, nickles and dimes.) I understand how to go about it for the most part, but I'm having difficulty with one of the rules.

    The user needs to enter how much the item they're buying cost and it can only be from $.25 - $1. The part that gets me is that it has to be in 5 cent increments (for ex. 25, 30, 35, etc.)

    I don't know what I need to use so that the program checks to make sure that's correct. Can anyone one point me in the right direction so I at least know what I need to work with to get that part right?

    Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I assume that you're creating a simple console app and not a GUI Swing app. I find that what would work well here is an if block and then use the mod operator "%" to see if the inputted number is divisible by 5. If mod 5 == 0, then it is a multiple of 5. You'll of course use if blocks to check the upper (100) and lower (25) bounds too.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Paper and pencil...

    First, try to figure it out on paper. Once you have that, then try to write the program , but do it in parts... first code the user's input. Once that's working, code the part that calculates much change has to be returned, etc... Don't try to do it in one big, continous effort.
    As for calculating how the change should be returned, first find how many quarters have to be returned. Then, with what's left, figure out how many dimes. Atfer that... and so on.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    Thank you both for replying.

    Fubarable that was very helpful, I almost have it done now =)

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    So ... did you get the program working?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    Yes, I did =)

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Post it ...

    Why not post your code for the benefit of the other people who visit the forum?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    and also mark this thread as solved, if solved.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Welll... at least here's what I did

    I guess we're not getting the OP's solution.... here's what I thought would work:
    Java Code:
    import javax.swing.JOptionPane;
    import java.util.*; 
    import java.lang.System.*;
    import java.text.*;
    
    public class ChangeMachine
    {
    public static void main(String[] args)
    {
    double change = 0;
    final double COST_LIMIT = 1.00;
    final double QUARTER = 0.25;
    final double DIME = 0.10;
    final double NICKEL = 0.05;
    //int newChange = 0;
    String prodName = JOptionPane.showInputDialog("What product did you purchase?");
    double prodCost = Double.valueOf(JOptionPane.showInputDialog("How much did it cost?"));
    
    //change = COST_LIMIT - prodCost;
    change = roundTwoDecimals(COST_LIMIT - prodCost);
    
    System.out.println("Change to be returned: " + change);
    
    double changeArray [] = {QUARTER, DIME, NICKEL};
    String changeName [] = {"Quarters", "Dimes", "Nickels"};
    
    for (int i = 0; i<changeArray.length;i++)
    {
    	if (change >= changeArray[i])
        {
    	int numOfChange = (int)(change/changeArray[i]);
    	System.out.println(changeName[i] + ": " + numOfChange);
    	change  = roundTwoDecimals(change%changeArray[i]);
    	//System.out.println("New value of change: " + change);
       }
    }
    
    }
    
    
    public static double roundTwoDecimals(double d) {
            	DecimalFormat twoDForm = new DecimalFormat("#.##");
    		return Double.valueOf(twoDForm.format(d));
    }
    }
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •