1. Member
Join Date
Jan 2009
Posts
7
Rep Power
0

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. 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. ## 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

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

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

5. So ... did you get the program working?

Luck,
CJSL

6. Member
Join Date
Jan 2009
Posts
7
Rep Power
0
Yes, I did =)

7. ## Post it ...

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

CJSL

8. and also mark this thread as solved, if solved.

9. ## 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));
}
}```

#### Posting Permissions

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