Results 1 to 9 of 9
Thread: A little help please?
- 01-24-2009, 11:53 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 7
- Rep Power
- 0
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.
-
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.
- 01-25-2009, 12:17 AM #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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-25-2009, 01:23 AM #4
Member
- Join Date
- Jan 2009
- Posts
- 7
- Rep Power
- 0
Thank you both for replying.
Fubarable that was very helpful, I almost have it done now =)
- 01-25-2009, 06:02 PM #5
So ... did you get the program working?
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-27-2009, 04:01 AM #6
Member
- Join Date
- Jan 2009
- Posts
- 7
- Rep Power
- 0
Yes, I did =)
- 01-27-2009, 12:22 PM #7
Post it ...
Why not post your code for the benefit of the other people who visit the forum?
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-27-2009, 08:11 PM #8
and also mark this thread as solved, if solved.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 01-29-2009, 02:09 PM #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)); } }Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks