0

Design a cashier change maker that will take as input: a floating point value. Value: "change due" and will convert it to proper currency (from $20 down to pennies) amounts to be handed as change to the customer

i am not sure how to use the rounding correctly every time i enter a new amount it does not work

import java.util.Scanner;
public class cashier 
{
	public static void main(String [] args)
	{
		Scanner kbd = new Scanner(System.in);
		float bill_20;
		float bill_10;
		float bill_5;
		float bill_1;
		float quarters;
		float dimes;
		float nickels;
		float pennies;
		float change_Due;
		
		System.out.println("How much money do you have ");
		change_Due = kbd.nextFloat();
		
		
		bill_20 = change_Due / 20f;
		change_Due = change_Due % 20f;
		
		bill_10 = change_Due / 10f;
		change_Due = change_Due % 10f;
		
		bill_5 = change_Due / 5f;
		change_Due = change_Due % 5f;
		
		bill_1 = change_Due / 1f;
		change_Due = change_Due % 1f;
		
		quarters = change_Due / 0.25f;
		change_Due = change_Due % 0.25f;
		
		dimes = change_Due / 0.10f;
		change_Due = change_Due % 0.10f;
		
		nickels = change_Due / 0.05f;
		change_Due = change_Due % 0.05f;
		
		pennies = change_Due / 0.01f;
		change_Due = change_Due % 0.01f;
			
		System.out.printf("$20 bill: %1.0f\n",  bill_20);
		System.out.printf("$10 bill: %1.0f\n", bill_10);
		System.out.printf("$5 bill: %1.0f\n" , bill_5 );
		System.out.printf("$1 bill: %1.0f\n" , bill_1 );
		System.out.printf("quarter: %1.0f\n" , quarters);
		System.out.printf("dimes: %1.0f\n" , dimes);
		System.out.printf("nickels: %1.0f\n" , nickels);
		System.out.printf("pennies: %1.0f\n" , pennies);
		
	}
}

Edited by Ezzaral: Added problem description from other thread.

2
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by nick6987
0

Consider that bills and coins are not fractional. They are whole integers.

okay how about if the input was $32.17
the output should be:
1 20 bill
1 10 bill
0 5 bill
2 1 bill
0 quarters
1 dime
1 nickel
2 pennies

but when i do this this is what i get
2 20 bill
1 10 bill
0 5 bill
1 quarter
2 dimes
1 nickel
2 pennies

0

And the reason is that bills and coins are integer quantities - not floats. Treat them as such.

in this program my teacher wanted us to use float and this code was the closest i came to but i can not get it to work correctly. Besides changing the type of data is there another way to do this program with float?

0

Unless you're allowed to use the Math.floor() function, which seems unlikely, you need to keep only the integer portion of your divisions.

In your case of 32/20, you can't use the 0.608 portion, you can only return 1 bill. You can't round it because then you're giving back too much. You must discard the decimal portion of that calculation.

0

i figured it out

import java.util.Scanner;
public class cashier 
{
	public static void main(String [] args)
	{
		Scanner kbd = new Scanner(System.in);
		float bill_20;
		float bill_10;
		float bill_5;
		float bill_1;
		float quarters;
		float dimes;
		float nickels;
		float pennies;
		float change_Due;
		
		System.out.println("How much money do you have ");
		change_Due = kbd.nextFloat();
		
		
		bill_20 = change_Due / 20f;
		change_Due = change_Due % 20f;
		bill_20 = (int)Math.round(bill_20 * 1000) / 1000;
		
		bill_10 = change_Due / 10f;
		change_Due = change_Due % 10f;
		bill_10 =(int)Math.round(bill_10 * 1000)/1000;
		
		bill_5 = change_Due / 5f;
		change_Due = change_Due % 5f;
		bill_5 =(int)Math.round(bill_5 * 1000)/1000;
		
		bill_1 = change_Due / 1f;
		change_Due = change_Due % 1f;
		bill_1 =(int)Math.round(bill_1 * 1000)/1000;
		
		quarters = change_Due / 0.25f;
		change_Due = change_Due % 0.25f;
		quarters =(int)Math.round(quarters * 1000)/1000;
		
		dimes = change_Due / 0.10f;
		change_Due = change_Due % 0.10f;
		dimes =(int)Math.round(dimes * 1000)/1000;
		
		nickels = change_Due / 0.05f;
		change_Due = change_Due % 0.05f;
		nickels =(int)Math.round(nickels * 1000)/1000;
		
		pennies = change_Due / 0.01f;
		change_Due = change_Due % 0.01f;
		pennies =(int)Math.round(pennies * 1000)/1000;
			
		System.out.printf("$20 bill: %1.0f\n",  bill_20);
		System.out.printf("$10 bill: %1.0f\n", bill_10);
		System.out.printf("$5 bill: %1.0f\n" , bill_5 );
		System.out.printf("$1 bill: %1.0f\n" , bill_1 );
		System.out.printf("quarter: %1.0f\n" , quarters);
		System.out.printf("dimes: %1.0f\n" , dimes);
		System.out.printf("nickels: %1.0f\n" , nickels);
		System.out.printf("pennies: %1.0f\n" , pennies);
		
	}
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.