0

I have attached my current source code to this message. It is in .java format.

Here is a brief description of my problem:

I have a program which accepts input for a Purchase Price, an Amount Tendered, and then displays the Change Due. Also, it must display the number of dollars, quarters, dimes, nickels, and pennies. The input is complete and I can calculate the Change Due, but I cannot figure out how to calculate each piece of change individually.

Note: The output is not in the code, but I know how to properly write it, so I won't need any help with output

Thanks in advance for any help:


Also, here is the look my application is to have:

Purchase Price: $ ________ (completed)
Amount Tendered: $ ________ (completed)

Your change is: $ ________ (completed)

__ one dollar bill (s)
__ quarter (s)
__ dimes (s)
__ nickel (s)
__ penn (y/ies)


Thank you for your business. Come back soon. (completed)

Attachments
/* 
     Chapter 3: Exercise 24, pg.146
     
     Problem:  Develop an application that reads a purchase price and an amount tendered and then 
	  displays the change in dollars, quarters, dimes, nickels, and pennies.  Two input values are
	  entered in cents, for example, 3480 for $34.80 and 70 for $0.70
	  
	  Note:  The input values are to be entered in cents (int data type), but the echo-printed values
	  must be displayed with decimal points (float data type).
*/


import javax.swing.*;
import java.text.*;

class Exercise3_24 {

     public static void main( String[] args ) {


          //Instatiate objects that will be used
	 		 final int			PENNY_PER_DOLLAR     = 100,
			 						DIMES_PER_DOLLAR     = 10,
									NICKLES_PER_DOLLAR   = 20,
									QUARTERS_PER_DOLLAR  = 4;
			 
			 final double		PRICE_OF_PENNY     = .01,
			 						PRICE_OF_NICKEL    = .05,
									PRICE_OF_DIME      = .10,
									PRICE_OF_QUARTER   = .25;	
												
			 String				purchaseStr, cashStr;
			 
			 int					purchaseAmt, paidAmt;
			 
			 float				purchasePrice, amountTendered, changeDue;	
			 
			 DecimalFormat    df1 = new DecimalFormat("0.00");
			 
			 								
	  		 //Retrieve purchase price from teller
	  
	  		 purchaseStr =  JOptionPane.showInputDialog(null, "Enter purchase price. (In pennies) ");

	  		 purchaseAmt =  Integer.parseInt(purchaseStr);

	  
	  		 //Retrieve total amount tendered
	  
	  		 cashStr =  JOptionPane.showInputDialog(null, "Enter amount tendered. (In pennies) ");

	 		 paidAmt =  Integer.parseInt(cashStr);

	  
	  		 //Convert int data type to float data type
			 			 
			 purchasePrice  = (float)(purchaseAmt * PRICE_OF_PENNY);
			 
			 amountTendered = (float)(paidAmt * PRICE_OF_PENNY);
			 
						 
			 //Calculat change to be returned
			 
			 changeDue 		 = amountTendered - purchasePrice;

			 
			 
			 //Display the change in dollars, quarters, dimes, nickels, and pennies
			 
			 System.out.println("Purchase Price:     $ " + df1.format(purchasePrice));
			 System.out.println("Amount Tendered:    $ " + df1.format(amountTendered));
			 System.out.println("");
			 
			 System.out.println("Your change is:     $ " + df1.format(changeDue));
			 					  
	}
}
6
Contributors
8
Replies
10
Views
12 Years
Discussion Span
Last Post by cdpothen
0

Well as you can see in the problem message in the source code, it is required. This is a homework assignment, so I have to do what the instructor says. I hope that explanation helps.

0

So all you need to do is find out how to most efficiently give change?

How do you determine what combination of coins and bills to use in a store? All you have to do is put that reasoning into code.

Say you give $1.26 in change.
How many dollars is that?
When those are gone, how many quarters are there in the leftover amount?
etc. etc. etc.

It's not hard.

0

I have to do the same basic problem. My prof. said to use integer division & modulus to find the amount for each coin in nested if loops. How to write it is the next big headache.

0

I have to do the same basic problem. My prof. said to use integer division & modulus to find the amount for each coin in nested if loops. How to write it is the next big headache.We're to specify if the coin is singular/plural also.

0

I tried working on this and this was my solution. Not very great maybe, i hope its of use to you

Attachments
/*
 * Main.java
 *
 * Created on 09 September 2005, 11:00
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package javaapplication1;
import javax.swing.*;
import java.text.*;
import java.math.*;
/**
 *
 * @author VABRAHAM
 */
public class Main {
    
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("kidiluu");

          //Instatiate objects that will be used
	 		 final int			PENNY_PER_DOLLAR     = 100,
			 						DIMES_PER_DOLLAR     = 10,
									NICKLES_PER_DOLLAR   = 20,
									QUARTERS_PER_DOLLAR  = 4;
			 
			 final double		PRICE_OF_PENNY     = .01,
			 						PRICE_OF_NICKEL    = .05,
									PRICE_OF_DIME      = .10,
									PRICE_OF_QUARTER   = .25,
                                                                        PRICE_OF_DOLLAR    =  1;
												
			 String				purchaseStr, cashStr;
			 
			 int					purchaseAmt, paidAmt;
			 
			 float	purchasePrice, amountTendered, changeDue;	
                         float dollars =0;
                         float quarters = 0;
                         float dimes = 0;
                         float nickels = 0;
                         float pennies = 0;
                         float temp = 0;
			 
			 DecimalFormat    df1 = new DecimalFormat("0.00");
			 DecimalFormat    df2 = new DecimalFormat("0");
			 								
	  		 //Retrieve purchase price from teller
	  
	  		 purchaseStr =  JOptionPane.showInputDialog(null, "Enter purchase price. (In pennies) ");

	  		 purchaseAmt =  Integer.parseInt(purchaseStr);

	  
	  		 //Retrieve total amount tendered
	  
	  		 cashStr =  JOptionPane.showInputDialog(null, "Enter amount tendered. (In pennies) ");

	 		 paidAmt =  Integer.parseInt(cashStr);

	  
	  		 //Convert int data type to float data type
			 			 
			 purchasePrice  = (float)(purchaseAmt * PRICE_OF_PENNY);
			 
			 amountTendered = (float)(paidAmt * PRICE_OF_PENNY);
			 
						 
			 //Calculat change to be returned
			 
			 changeDue 		 = amountTendered - purchasePrice;

			 
			 
			 //Display the change in dollars, quarters, dimes, nickels, and pennies
			 
			 System.out.println("Purchase Price:     $ " + df1.format(purchasePrice));
			 System.out.println("Amount Tendered:    $ " + df1.format(amountTendered));
			 System.out.println("");
			 
			 System.out.println("Your Balance is:     $ " + df1.format(changeDue));
                         //calculating dollars
                         if(changeDue%(PRICE_OF_DOLLAR)==0){
                            dollars = changeDue;
                         }else {
                             dollars = changeDue - (float)(changeDue%PRICE_OF_DOLLAR);
                         }
                         //calculating quarters
                         changeDue = (float)(changeDue%PRICE_OF_DOLLAR);
                         //System.out.println(" change due here" + changeDue );
                                 
                         if(changeDue%(PRICE_OF_QUARTER)==0){
                            quarters = changeDue/(float)PRICE_OF_QUARTER;
                            //System.out.println("here");
                         }else {
                            quarters = (changeDue - (float)(changeDue%PRICE_OF_QUARTER))/(float)PRICE_OF_QUARTER;
                            //System.out.println("here111");
                         }
                         
                         //calculating dimes
                         changeDue = (float)(changeDue%PRICE_OF_QUARTER);
                         System.out.println(" change due here" + changeDue );
                                          
                         if(changeDue%(PRICE_OF_DIME)==0){
                            dimes = changeDue/(float)PRICE_OF_DIME;
                            System.out.println("here dimes is" + dimes);
                         }else {
                            dimes = (changeDue - (float)(changeDue%PRICE_OF_DIME))/(float)PRICE_OF_DIME;
                            System.out.println("here111 here dimes is" + dimes);
                         }
                         
                         //calculating nickels

                         changeDue = (float)(changeDue%PRICE_OF_DIME);
                         System.out.println(" change due here in two " + changeDue );
                                          
                         if(changeDue%(PRICE_OF_NICKEL)==0){
                            nickels = changeDue/(float)PRICE_OF_NICKEL;
                            System.out.println("here in kuttus");
                         }else {
                            nickels = (changeDue - (float)(changeDue%PRICE_OF_NICKEL))/(float)PRICE_OF_NICKEL;
                            System.out.println("here111 in pittus " + nickels);
                         }                         

                         //calculating pennies

                         changeDue = (float)(changeDue%PRICE_OF_NICKEL);
                         System.out.println(" change due here tulley " + changeDue );
                         pennies = changeDue / (float)PRICE_OF_PENNY;
                       
                       
                        System.out.println("You get " + df2.format(dollars)+ " dollars" );
                        System.out.println("You get " + df2.format(quarters) + " quarters " );
                        System.out.println("You get " + df2.format(dimes) + " dimes " );
                        System.out.println("You get " + df2.format(nickels) + " nickels " ); 
                        System.out.println("You get " + df2.format(pennies) + " pennies " );
    }
}
This article has been dead for over six months. 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.