Results 1 to 3 of 3
  1. #1
    jean28 is offline Member
    Join Date
    Aug 2012
    Posts
    16
    Rep Power
    0

    Default 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:

    Dollars: 1
    Quarters: 0
    Dimes: 0
    Nickels: 0
    Pennies: 0
    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?

  2. #2
    jean28 is offline Member
    Join Date
    Aug 2012
    Posts
    16
    Rep Power
    0

    Default 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

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,512
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. A program that give change
    By Nixter1997 in forum New To Java
    Replies: 4
    Last Post: 08-23-2012, 02:42 PM
  2. Replies: 8
    Last Post: 06-16-2012, 05:18 AM
  3. Replies: 12
    Last Post: 04-20-2012, 05:43 PM
  4. Find Exact Program name under Java process
    By sjunejo in forum Threads and Synchronization
    Replies: 0
    Last Post: 04-14-2011, 12:20 PM
  5. Change the color in my program
    By carl in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 12:20 PM

Posting Permissions

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