Results 1 to 3 of 3
- 10-27-2012, 09:01 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 16
- Rep Power
- 0
Program that gives exact change of quarters, dimes, etc...
Hey guys. I'm trying to write a program that calculates the change of something that was bought, and then I have to say, in dollars,quarters, dimes, nickels, and pennies, how I will give the change back to the customer. I have most of it done but I'm having some problems. Could anyone tell me what's wrong with my code?
Java Code:/* * A cash register totals up sales and computes change due */ public class CashRegister { private double cost; private int amountpayed; private double change; private double dollarnext; private double quarternext; private double dimenext; private double nickelnext; /* * Constructs a cash register with no money on it */ public CashRegister() { cost = 0; amountpayed = 0; } /** * Records the sale of an item */ public void RecordPurchase(double purchasecost) { cost = purchasecost; } /** * Enters the payment received from the customer */ public void enterPayment(int dollar, int quarter, int dime, int nickel, int penny) { amountpayed = (int) ((dollar * 1.00) + (quarter * 0.25) + (dime * 0.10) + (nickel * 0.05) + (penny * 0.01)); change = amountpayed - cost; } public int giveDollars() { int dollarchange = (int) (change/1); dollarnext = (int) (change % 1); return dollarchange; } public int giveQuarters() { int quarterchange = (int) (dollarnext/0.25); quarternext = (int) (dollarnext % 0.25); return quarterchange; } public int giveDimes() { int dimechange = (int) (quarternext/0.10); dimenext = (int) (quarternext % 0.10); return dimechange; } public int giveNickels() { int nickelchange = (int) (dimenext/0.05); int pennynext = (int) (quarternext % 0.05); return nickelchange; } public int givePennies() { int pennychange = (int) (nickelnext/0.01); return pennychange; } }
Java Code:public class ChangeTester { public static void main(String[] args) { CashRegister register = new CashRegister(); register.RecordPurchase(8.37); register.enterPayment(10, 0, 0, 0, 0); System.out.println("Dollars: " + register.giveDollars()); System.out.println("Quarters: " + register.giveQuarters()); System.out.println("Dimes: " + register.giveDimes()); System.out.println("Nickels: " + register.giveNickels()); System.out.println("Pennies: " + register.givePennies()); } }
Output:
The expected output should be 1 Dollar, 2 Quarters, 1 Dime, 0 Nickels, 3 Pennies. I have the dollar. How do I fix the other four?Dollars: 1
Quarters: 0
Dimes: 0
Nickels: 0
Pennies: 0
- 10-27-2012, 09:23 AM #2
Member
- Join Date
- Aug 2012
- Posts
- 16
- Rep Power
- 0
Re: Program that gives exact change of quarters, dimes, etc...
I have updated the code and now I'm almost there. I'm supposed to get 3 pennies, yet I get 0. Not sure why, though:
Java Code:/* * A cash register totals up sales and computes change due */ public class CashRegister { private double cost; private double amountpayed; private double change; private double dollarnext; private double quarternext; private double dimenext; private double nickelnext; private double pennynext; /* * Constructs a cash register with no money on it */ public CashRegister() { cost = 0; amountpayed = 0; } /** * Records the sale of an item */ public void RecordPurchase(double purchasecost) { cost = purchasecost; } /** * Enters the payment received from the customer */ public void enterPayment(int dollar, int quarter, int dime, int nickel, int penny) { amountpayed = ((dollar * 1.00) + (quarter * 0.25) + (dime * 0.10) + (nickel * 0.05) + (penny * 0.01)); change = amountpayed - cost; } public int giveDollars() { int dollarchange = (int) (change/1); dollarnext = (change % 1); return dollarchange; } public int giveQuarters() { int quarterchange = (int) (dollarnext/0.25); quarternext = (dollarnext % 0.25); return quarterchange; } public int giveDimes() { int dimechange = (int) (quarternext/0.10); dimenext = (quarternext % 0.10); return dimechange; } public int giveNickels() { int nickelchange = (int) (dimenext/0.05); pennynext =(quarternext % 0.05); return nickelchange; } public int givePennies() { int pennychange = (int) (nickelnext/0.01); pennynext = (nickelnext % 0.01); return pennychange; } }Dollars: 1
Quarters: 2
Dimes: 1
Nickels: 0
Pennies: 0
- 10-27-2012, 10:08 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,410
- Blog Entries
- 7
- Rep Power
- 17
Re: Program that gives exact change of quarters, dimes, etc...
Your method is fine, except you shouldn't do all those calculations in the double domain; use the int domain instead; all you have to do is consider the penny as the monetary unit, so a quarter is worth 25 pennies, a dollar is 100 pennies etc.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
A program that give change
By Nixter1997 in forum New To JavaReplies: 4Last Post: 08-23-2012, 02:42 PM -
NEED HELP! converting change given to dollars, quarters, dimes, etc..
By mrodr359 in forum New To JavaReplies: 8Last Post: 06-16-2012, 05:18 AM -
Change a program to a LoopTest, and change a LoopTest to an Array List???
By Valerie1067 in forum New To JavaReplies: 12Last Post: 04-20-2012, 05:43 PM -
Find Exact Program name under Java process
By sjunejo in forum Threads and SynchronizationReplies: 0Last Post: 04-14-2011, 12:20 PM -
Change the color in my program
By carl in forum New To JavaReplies: 5Last Post: 04-03-2009, 12:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks