NEED HELP! converting change into dollars, quarters, dimes etc..
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: NEED HELP! converting change into dollars, quarters, dimes etc..

  1. #1
    Join Date
    Jun 2012
    Posts
    1

    NEED HELP! converting change into dollars, quarters, dimes etc..

    Hi guys!

    Well my problem is i don't know how to convert the amountGiven to change denominations such as dollars, quarters, dimes, nickels, pennies.. Ive just about given up

    heres my code:

    import java.text.NumberFormat;


    public class Cashier {

    private static final double QUARTER_VALUE = 0.25;
    private static final double DIME_VALUE = 0.1;
    private static final double NICKEL_VALUE = 0.05;
    private static final double PENNY_VALUE = 0.01;
    private static final double DOLLAR_VALUE = 1.00;

    private static String name;
    private static double total;
    private double cost;
    private int numOfItems;
    private double aver;
    private double aver1;
    private double aver2;
    private double change;
    private double amountGiven;
    String s;

    private double penny,nickel,dime,quarter,dollar;

    NumberFormat nf = NumberFormat.getCurrencyInstance();

    public Cashier()
    {
    name = "";
    s = "";
    total = 0.0;
    cost = 0.0;
    numOfItems = 0;
    }

    public void add(String name, double cost)
    {
    this.name = name;
    this.cost = cost;
    s = s + name + "....." + cost + "\n";
    numOfItems = numOfItems + 1;
    total = total + cost;
    }

    public void average()
    {
    aver = total / numOfItems;
    }

    public void tendered(double Amount)
    {
    amountGiven = Amount;
    }

    public double getAG()
    {
    return amountGiven;
    }

    public void makeChange()
    {
    change = amountGiven - total;
    }

    public String getName()
    {
    return s;
    }

    public double getCost()
    {
    return cost;
    }

    public int getNumOfItems()
    {
    return numOfItems;
    }

    public double getTotal()
    {
    return total;
    }

    public double getAver()
    {
    return aver;
    }

    public double getChange()
    {
    return change;
    }

    public double Quarter()
    {
    quarter =(int) change / 25;
    change = change % 25;


    return quarter;

    }

    public double Dime(){

    dime = (change / 25)/10;


    return dime;


    }
    public double Dollar(){

    dollar = (int)amountGiven / 100;
    amountGiven = amountGiven * 100;

    return dollar;

    }

    public double nickel(){

    nickel = (change / 10)/5;


    return nickel;

    }
    public double Penny(){

    penny = (change / 5)/1;


    return penny;

    }
    }

  2. #2
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    1

    Re: NEED HELP! converting change into dollars, quarters, dimes etc..

    Code:
    import java.io.*; // For user input.
    public class Cashier
    { // Open main program body.
        public static void main (String[] args) throws IOException
        { // take note.. throws IOException.. for user's input.
     
            // Let's begin with the declaration of variables that we are going to use..
     
            BufferedReader userInput = new BufferedReader (new InputStreamReader (System.in)); //User's input variable.
            String name; //your program had something about a name..
     
            /* Because you're dealing with money... you'll have to go through decimals, though your output are all whole numbers,
            so we begin by declaring doubles for all of dollars, quarters, dimes, etc... */
     
            double amount; // stores the amount the user will input.
            double dollars; // stores the amount of calculated dollars.
            double quarters; // ' '     ' '     ' ' quarters.
            double dimes; // ' '     ' '     ' ' dimes.
            double nickels; // ' '     ' '     ' ' nickels.
            double pennies; // ' '     ' '     ' ' pennies.
     
            // the program will have to pass down the remainder after dividing out each value, so let's declare variables for those..
     
            double dollarRem; // stores the amount of remainder from dollars.
            double quarterRem; // ' '     ' '     ' ' quarters.
            double dimeRem; // ' '     ' '     ' ' dimes.
            double nickelRem; // ' '     ' '     ' ' nickels.
     
            //Now that we have all the variables declared, let's collect data from the user and get to the calculations..
     
            System.out.println ("What is your name?"); //Request information from the user.
            name = userInput.readLine (); // store information in earlier declared "name" variable.
     
            System.out.println ("Hello " + name + ".\n" // refer to the user by their name that you just collected.
                    + "This program calculates the number of coins necessary to make change for any amount you enter."); //explain program.
     
            System.out.println ("Please enter the amount you like to make change for in dollars. e.g - '3.41' "); //Request of data.
            amount = Double.parseDouble (userInput.readLine ()); // parse the users input into a double as it is accepted as a string by default.
     
            /*That's all the data your program needs to collect, now let's calculate...
             *firstly we want to make the amount you just collected a whole number so it is WAY easier to deal with.. */
            amount = amount * 100; // multiply the two decimal place number by 100 to make it a whole number.. N.B - this assumes the user's input is not more than two decimal places.
     
            // dollars calculation..
            dollars = Math.floor (amount / 100);
            // note dividing by 100 not 1 because the amount was multiplied by 100, so the dollar is too. the "Math.floor()" ROUNDS the answer DOWN to the nearest whole number.
     
            // now the remainder after we take out dollars..
            dollarRem = amount % 100; // the "%" means it divides two numbers and returns the remainder.
     
            // quarters calculation..
     
            quarters = Math.floor (dollarRem / 25);
     
            // now the remainder after we take out quarters..
     
            quarterRem = dollarRem % 25;
     
            //See the pattern? we'll just run through the rest..
     
            dimes = Math.floor (quarterRem / 10);
            dimeRem = quarterRem % 10;
            nickels = Math.floor (dimeRem / 5);
            nickelRem = dimeRem % 5;
            pennies = nickelRem; //anything divided by 1 is the same number, so nickelRem is the amount of pennies.
     
            //Now that all calculations are done.. time for output to the user...
     
            System.out.println ("Here is the amount to each type of coin necesssary to make change for $" + (amount / 100) + " is:\n"
                    + "Dollars: " + (int) dollars + "\n"
                    + "Quarters: " + (int) quarters + "\n"
                    + "Dimes: " + (int) dimes + "\n"
                    + "Nickels: " + (int) nickels + "\n"
                    + "Pennies: " + (int) pennies); // output to user.
        }
    } // close main program.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center