Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Program that gives exact change of quarters, dimes, etc...

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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?

    /*
     *  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;
    	}
    }


    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
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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:

    /*
     *  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
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    My Mood
    Sneaky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Program that gives exact change of quarters, dimes, etc...

    public int giveNickels()
    {
    int nickelchange = (int) (dimenext/0.05);
    nickelnext =(quarternext % 0.05);
    return nickelchange;
    }

    Fix the underlined variable, you were assigning pennynext and not nickelnext. That should fix your problem.
    Last edited by NickNumero; October 27th, 2012 at 12:26 PM.

Similar Threads

  1. write the program in C, C++, Java or C#. Change variable types accordingly.
    By indrasensingh01 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 8th, 2012, 06:34 PM
  2. program : giving back change with dollars, dimes and pennies
    By hellynam in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 24th, 2012, 09:21 PM
  3. [SOLVED] Help with my Java program: Making change from an entered double.
    By iDizzle in forum What's Wrong With My Code?
    Replies: 20
    Last Post: March 18th, 2012, 03:16 PM
  4. Program is attempting to change booleans yet the result is unsuccesful.
    By CoolGuy134 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2011, 08:48 AM
  5. Bouncing Ball Program Random Color Change
    By coderEvolution in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 3rd, 2011, 04:01 PM