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 2 of 2

Thread: [Homework] Calculating Change

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default [Homework] Calculating Change

    The idea of this assignment is to simulate a customer buying goods from a store. The problem that I am having is that when I try and calculate how much change the customer should receive, my program treats $20.99 as $20 so when I try and convert the double into an integer it gives me 900 instead of 901. I have been working at this for a couple hours now and just can't seem to find where my error lies. Any suggestions or pointers to the right direction and I would be grateful.

    I haven't finished my if statement or PrintWriter statements because I know my arithmetic is not working the way I want and need it to.

    Here is my code:

    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.Scanner;
     
     
    public class store {
     
    	/**
    	 * @throws FileNotFoundException 
    	 * @name	
    	 * @purpose	Calculate purchases
    	 * @date	February 6th, 2013
    	 */
    	public static void main(String[] args) throws FileNotFoundException 
    	{
    		Scanner Keyboard = new Scanner(System.in);
    		PrintWriter prw = new PrintWriter("outputPA3.txt");
     
    		double Beer = 1.95;
    		double Chips = 1.79;
    		double Coke = 3.99;
     
    		System.out.println("Please enter your name:");
    		String name = Keyboard.nextLine();
    		System.out.println("Welcome to Stop and Rob, " + name + "! Today we have the following items on sale: ");
    		System.out.println("Beer: $" + Beer);
    		System.out.println("Chips: $" + Chips);
    		System.out.println("Coke: $" + Coke);
    		System.out.println();
    		System.out.println("Please place your order:");
    		System.out.println();
    		System.out.println("How many beers would you like today?");
    		int numBeer = Keyboard.nextInt();
    		System.out.println(numBeer + " beers");
    		System.out.println("How many bags of chips? ");
    		int numChips = Keyboard.nextInt();
    		System.out.println(numChips + " bags of chips");
    		System.out.println("How many 12-packs of Coke would you like? ");
    		int numCoke = Keyboard.nextInt();
    		System.out.println(numCoke + " packs of Coke");
     
    		double total = (Beer * numBeer) + (Chips * numChips) + (Coke * numCoke);
    		System.out.printf("Your bill today is $%4.2f", total);
     
    		System.out.println();
    		System.out.println(name + " enter the amount you will be paying: ");
    		double payment = Keyboard.nextDouble();
    		System.out.println("The amount you entered is: $" + payment);
     
    		if (payment >= total)
    		{	
    			int rAmount = (int)((payment - total) * 100);
    			int rFives = rAmount / 500;
    			rAmount = rAmount % 500;
    			int rDollars = rAmount / 100;
    			rAmount = rAmount % 100;
    			int rQuarters = rAmount / 25;
    			rAmount = rAmount % 25;
    			int rDimes = rAmount / 10;
    			rAmount = rAmount % 10;
    			int rNickels = rAmount / 5;
    			rAmount = rAmount % 5;
    			int rPennies = rAmount;
    			System.out.println("Your change of " + rAmount + " consists of \n" +
    					"\t" + rFives + " fives\n" +
    					"\t" + rDollars + " dollars\n" +
    					"\t" + rQuarters + " quarters\n" +
    					"\t" + rDimes + " dimes\n" +
    					"\t" + rNickels + " nickels\n" +
    					"\t" + rPennies + " pennies\n");
    		}
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: [Homework] Calculating Change

    doubles have rounding errors and other problems and are not recommended for use with money calculations.
    Try to do all the computations in pennies and only convert for displaying.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help with calculating stock with joptionpane
    By joregorn in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 23rd, 2012, 06:51 AM
  2. Need help with calculating stock code.
    By joregorn in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 22nd, 2012, 07:06 PM
  3. Calculating Percentile
    By lakshmivaraprasad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2011, 08:26 PM
  4. Calculating Strings?
    By SkyAphid in forum Java Theory & Questions
    Replies: 2
    Last Post: August 30th, 2011, 09:38 PM
  5. question on calculating
    By meowCat in forum Java Theory & Questions
    Replies: 5
    Last Post: August 9th, 2010, 05:06 PM