Reputation: 33
I have been working on a change calculator will take input for 1) the customer name, 2) the item description, 3) purchase price of an item and 4) the amount tendered by the customer. Output the customer name, description, cost (with decimal point), amount tendered (with decimal point) and change in one dollar bills, quarters, nickels, dimes and pennies.
This is what I have so far and when I compile and run it, it gives me the wrong amount of change when it gets to the dimes nickels, and pennies.
/* CCTPG22 : Assignment02_ Changer
* @author Kevin Trujillo
* @version 8/30/2014
* Purpose: Your program will take input for the customer name,
the item description, purchase price of an item and
the amount tendered by the customer. Output the customer
ame, description, cost (with decimal point), amount tendered
(with decimal point) and change in one dollar bills, quarters,
ickels, dimes and pennies.
* Status: In Progress
*/
import java.util.*;
public class Assignment02_Changer
{
public static void main(String[] args)
{
String costumer_Name ; // Variables Declared
String item_Bought ;
int amt_DUE ;
int amt_PAID ;
final int pennies_per_dollar = 100 ;
final int nickles_per_dollar = 20 ;
final int dimes_per_dollar = 10 ;
final int quarters_per_dollar = 4 ;
final int pennies_value = 1 ;
int change_due ;
int change_left ;
int dollars_returned ; // Amt of dollars returned
int quarters_returned ; // Amt of Quarters returned
int dimes_returned ; // Amt of dimes returned
int nickles_returned ; // Amt of nickles returned
int pennies_returned ; // Amt of pennies returned
Scanner in = new Scanner(System.in); // Intoduces Input Operator
System.out.print("Change making program by Kevin Trujillo. \n") ; // First Series of Outputs and Inputs (self-explanatory)
System.out.print("What is the customer's name? ") ;
costumer_Name = in.nextLine() ;
System.out.print("Provide one line to describe the item: ") ;
item_Bought = in.nextLine() ;
System.out.print("Please enter the item price in pennies: ") ;
amt_DUE = in.nextInt() ;
System.out.print("Please enter the amount tendered in cents ") ;
amt_PAID = in.nextInt() ;
System.out.print(costumer_Name + " bought a " + item_Bought + " for " + amt_DUE ) ;
System.out.print(" and tendered " + amt_PAID + " in cents. \n") ;
change_due = amt_DUE - amt_PAID ; // caluc. total AMT of change returned
dollars_returned = change_due / pennies_per_dollar ; // calcu. AMT of dollars returned
change_left = change_due % pennies_per_dollar ; // finds remainder due
change_due = change_left ;
quarters_returned = change_due / quarters_per_dollar ; // calcu. AMT of quarters returned
change_left = change_due % quarters_per_dollar ; // finds remainder due
change_due = change_left ;
dimes_returned = change_due / dimes_per_dollar ; // finds remainder due
change_left = change_due % dimes_per_dollar ; // calcu. AMT of dimes returned
change_due = change_left ;
nickles_returned = change_due / nickles_per_dollar ; // finds remainder due
change_left = change_due % nickles_per_dollar ; // calcu. AMT of nickles returned
change_due = change_left ;
dimes_returned = change_due / dimes_per_dollar ; // finds remainder due
change_left = change_due % dimes_per_dollar ; // caluc. AMT of dimes returned
change_due = change_left ;
pennies_returned = change_due / pennies_per_dollar ; // finds remainder due
System.out.print(costumer_Name + "'s change is: \n" );
System.out.printf(" " + dollars_returned + " one-dollar bill (s)\n") ;
System.out.printf(" " + quarters_returned + " quarter (s)\n" ) ;
System.out.printf(" " + dimes_returned + " dime (s)\n") ;
System.out.printf(" " + nickles_returned + " nickle (s)\n") ;
System.out.printf(" " + pennies_returned + " penny (ies)\n") ;
System.out.print("Thank you for your business. Come back soon " + costumer_Name + "!\n" ) ;
} // braces for the main class
} // braces for the program
So my question is if I am making an arithmetic error in my code or if you can see something i cant.
This is an example of what the program should look like.
Change making program by Don Smith
What is the customer's first name? Bob
Provide one line to describe the item: Chem Book
Please enter the item price in cents: 3458
Please enter the amount tendered in cents: 4000
Bob bought a Chem Book for 34.58 and tendered 40.00
Bob's change is:
5 one-dollar bill(s)
1 quarter(s)
1 dime(s)
1 nickel(s)
2 penny(ies)
Thanks for your business. Come back soon.
Upvotes: 2
Views: 779
Reputation: 1742
Change your final static
variables to:
int dollar = 100;
int quarter = 25;
int dime = 10;
int nickel = 5;
int penny = 1;
then use:
int change = paid - due;
int dollar_c = change / dollar;
change = change % dollar;
int quarter_c = change / quarter;
change = change % quarter;
int dime_c = change / dime;
change = change % dime;
int nickel_c = change % nickel;
change = change % nickel;
Where dollar_c
is your dollars_returned
and change
is the pennies_returned
.
Upvotes: 0
Reputation: 5786
Your code and the output are not under sync. The SOPs are different and also some math mistakes. May be you posted older code that you tried. But these changes should help you.
final int pennyValue = 1 ;//Change occurrences of these subsequently
final int nicklesValue = 5 ;
final int dimesValue = 10 ;
final int quartersValue = 25 ;
. . .
change_due = amt_PAID - amt_DUE ;//You have it like amt_DUE - amt_PAID gives negative numbers
It might have typos. But this should resolve.
Upvotes: 0
Reputation: 13
By looking at your code I could spot the following:
After you get the amount of one dollar bills and you get the remainder of cents (less than 100) you are dividing that by 4 (number of quarters in a dollar), you should divide that by 25 actually, then you should follow the same logic downwards. In short you should divide by the number of cents in any of the quarter/dime/nickel/penny and not by how many of them make for a dollar.
Upvotes: 1