954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

java program to make change please help

need help to making change
I have read textbook a lot but i still dont know what to do next steps.
Here is problem.
The program should prompt the user for and accept two inputs:
1. The price (including any applicable tax, etc.) for whatever was purchased by the
customer.
2. The amount of money provided to the cashier by the customer

Given this information, the program should provide output as follows

• Display appropriate messages if the customer provided to little money or the exact
amount.
• Otherwise, display the number of bills and coins that should be given to the customer
as change.
• Change should be provided using the largest bill and coin denominations as possible.
The available bill denominations are $20, $10, $5 and $1; the available coin
denominations are 25¢, 10¢, 5¢, 1¢ (there are no half-dollar coins). For example, if
the change amount is $26.56, the customer should be given one $20 bill, one $5 bill,
one $1 bill, two quarters, one nickel, and one penny.
• Denominations that are not used should not be displayed. For example, in Sample
Run 2 below, the dime coin is not included in the output.

Important: You should store all of your amounts as cents using Java’s int type, not double.
In order to allow users of your program to enter amounts normally, you should convert entered
dollar amounts to cents by multiplying by 100 and “casting” from double to int, e.g.:

int price = (int) Math.round(keyboard.nextDouble()*100);

Sample Run 1

Enter the purchase price: 13.44
Enter amount provided by customer: 15.00
The customer should be given change as follows:
1 $1 bill(s)
2 quarter coin(s)
1 nickel coin(s)
1 penny coin(s)

Sample Run 2

Enter the purchase price: 13.44
Enter amount provided by customer: 13.44
No change is necessary!!

Sample Run 3

Enter the purchase price: 26.14
Enter amount provided by customer: 100.00
The customer should be given change as follows:
3 $20 bill(s)
1 $10 bill(s)
3 $1 bill(s)
3 quarter coin(s)
1 dime coin(s)
1 penny coin(s)

please help
this is what i have so far

import java.util.Scanner;
public class changeMaker {

	/**
	 * 
	 */
	public static void main(String[] args) {
		Scanner keyboard= new Scanner(System.in);
		int price;
		int provided;
		int change;
		int ones= 0, fives= 0, tens= 0, twenties= 0;
		int pennies= 0, nickels= 0, dimes= 0, quaters= 0;
		System.out.print("Enter the purchase price:");
		price = (int)Math.round(keyboard.nextDouble()*100);
		
		System.out.print("Enter the amount given by  the customer:");
		provided = (int) Math.round(keyboard.nextDouble()*100);
nickh2o
Newbie Poster
3 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

That's a good start. Now you need to work out how many of each note/coin.
Move away from your computer, sit down with pencil & paper, and work through the 3 sample runs solving them by hand. Think about exactly what you have to do (in complete detail) to get to the right answer. Then, and only then, write a program to follow exactly the same steps as you did on paper.
(No, I'm not being funny - this is what experienced programmers do when they can't see how to code logic like this)

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

i understand the concept of what i have to do in the program mathmatics wise, i just am confused on how to put it in the program

nickh2o
Newbie Poster
3 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

It's not so much about maths as procedures - what are the exact steps, one at a time, that you have to do.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

thankfuly i finished the program and it works now i have to write a algorithm for it, and suggestions or idea on what my algorithm should be? and/or more comments i should add?

import java.util.Scanner;

public class changeMaker {
	/** 
       * 
       */
	public static void main(String[] args) {
		Scanner keyboard = new Scanner(System.in);

		int price;
		int provided;
		int change;

		System.out.print("Enter the purchase price:");

		price = (int) Math.round(keyboard.nextDouble() * 100);
		System.out.print("Enter the amount given by the customer:");
		provided = (int) Math.round(keyboard.nextDouble() * 100);

		if (provided > price) {

			System.out
					.println("The customer should be given the change as follows:");

			change = provided - price;
             // Since you multiplied by 100 you have to divide by 2000 to get the number of twenties for change.
			 
			int twenties = change / 2000;
			if (twenties > 0) {  //if the change is less than $20 this will be a zero
				change = change % 2000; // this resets the value of change to the remainder after the twenties are calculated but only if there was at least enough to make one twenty 
				System.out.println(twenties + " $20 bill(s)");
			}
			int tens = change / 1000;
			if (tens > 0) {
				change = change % 1000;
				System.out.println(tens + " $10 bill(s)");
			}
			int fives = change / 500;
			if (fives > 0) {
				change = change % 500;
				System.out.println(fives + " $5 bill(S)");
			}
			int ones = change / 100;
			if (ones > 0) {
				change = change % 100;
				System.out.println(ones + " $1 bill(s)");
			}
			int quarters = change / 25;
			if (quarters > 0) {
				change = change % 25;
				System.out.println(quarters + " quarter coin(s)");
			}
			int dimes = change / 10;
			if (dimes > 0) {
				change = change % 10;
				System.out.println(dimes + " dime coin(s)");
			}
			int nickels = change / 5;
			if (nickels > 0) {
				change = change % 5;
				System.out.println(nickels + " nickel coin(s)");
			}
			int pennies = change;
			System.out.println(pennies + " penny coin(s)");
		}

		if (provided < price) {  // this statement is saying that if the customer doesn't pay enough, it will tell the user

			System.out.print("Not enough money!");
		}

		else if (provided == price) { // this statement says if the amount provided matches the price, then there is no change necessary

			System.out.print("No change is necessary!");
		}

	}

}
nickh2o
Newbie Poster
3 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: