CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2
  • Faltering Windows support
  • Internet Explorer 8 Click Clever Click Safe
  • Release Candidate 2 for ASP.NET MVC 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Java Programming > Java Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Java Programming Ask your Java programming question and help out others with theirs.

     
     
    Thread Tools Search this Thread Rate Thread Display Modes
    Prev Previous Post   Next Post Next
      #1    
    Old October 10th, 2008, 08:19 PM
    Live01 Live01 is offline
    Junior Member
     
    Join Date: Oct 2008
    Posts: 3
    Live01 is an unknown quantity at this point (<10)
    Easy cash register program help!

    I've recently began learning java in my grade 11 computer course, and we recently recieved an assignment in which you must have the user enter a purchase amount, calculate pst and gst, output the total, calculate change from an inputted tender amount, and output the coin denominations required in 20s 10s 5s toonies loonies quarters dimes nickels and cents.

    I have the program fully written, and it works. The problem is, the program is inaccurate by 1 cent. For example, I enter 10.00 as raw test data. the program calculate 8% tax and calculates 11.3, which is then formatted to show $11.30. I enter 20.00 as tender, and the program calculates change at 8.7 (formatted to show $8.70). Next, i multiply the change by 100 to get an exact amount of change in pennies. Here's the problem, 8.7 * 100 gets me an answer of 869 pennies, instead of 870.

    I am using integer and modulus division as required. I can't figure this one out, can anyone help me out? Here's the source code:

    import java.text.*;
    public class CashRegister
    {
    public static void main (String [] args)
    {
    //Create a number format object to be used
    NumberFormat nfc = NumberFormat.getCurrencyInstance();

    //Declare and initialize variables
    double purchase = 0.0;
    double total = 0.0;
    double change = 0.0;
    double gst = 0.0;
    double pst = 0.0;
    double tender = 0.0;
    int fullchange, changeleft = 0;
    int twenties, tens, fives, toonies, loonies, quarters, dimes, nickels, pennies = 0;


    //Declare and initilize constants
    final double GSTRATE = 0.05; //Assigns 5% as gst rate
    final double PSTRATE = 0.08; //Assigns 8% as pst rate

    //Introduce the program to th user and tell the user how to use the program
    System.out.println("\tWelcome to the Cash Register. Please follow the instructions.");

    System.out.println(""); //inputs a blank line as a spacer

    //Prompt the user for purchase amount
    System.out.print("Please enter the purchase amount, before taxes ($0.00): $");
    purchase = In.getDouble();

    System.out.println(""); //inputs a blank line as a spacer

    //Calculate gst and pst
    gst = purchase * GSTRATE;
    pst = purchase * PSTRATE;

    //Calculate total
    total = purchase + gst + pst;
    System.out.println("unformatted total is: " + total); //DEBUGGING

    //Print total to user
    System.out.println("The total amount after taxes is: " + nfc.format(total));

    System.out.println(""); //inputs a blank line as a spacer

    //Prompt the user for tender amount
    System.out.print("Please enter the tendered amount ($0.00): $");
    tender = In.getDouble();

    //Calculate change
    change = tender - total;
    System.out.println("change after calculation is: " + change);

    //Output change and add a spacer
    System.out.println("The total change from purchase is: " + nfc.format(change));
    System.out.println(""); //inputs a blank line as a spacer

    //Convert change to an integer (change amount in pennies)
    changeleft = (int)(change * 100.0);
    System.out.println("fullchange in pennies is: " + changeleft); //DEBUGGING

    //Calculate coin denomination
    twenties = changeleft / 2000;
    changeleft = changeleft % 2000;
    System.out.println("fullchange is: " + changeleft); //DEBUGGING
    tens = changeleft / 1000;
    changeleft = changeleft % 1000;
    System.out.println("fullchange is: " + changeleft); //DEBUGGING
    fives = changeleft / 500;
    changeleft = changeleft % 500;
    System.out.println("fullchange is: " + changeleft);
    toonies = changeleft / 200;
    changeleft = changeleft % 200;
    System.out.println("fullchange is: " + changeleft);
    loonies = changeleft / 100;
    changeleft = changeleft % 100;
    System.out.println("fullchange is: " + changeleft);
    quarters = changeleft / 25;
    changeleft = changeleft % 25;
    System.out.println("fullchange is: " + changeleft);
    dimes = changeleft / 10;
    changeleft = changeleft % 10;
    System.out.println("fullchange is: " + changeleft);
    nickels = changeleft / 5;
    changeleft = changeleft % 5;
    System.out.println("fullchange is: " + changeleft);
    pennies = changeleft;
    System.out.println("fullchange is: " + changeleft);

    //Output coin denomination
    System.out.println("Please give the customer the following change:");
    System.out.println("Twenties:\t\t" + twenties);
    System.out.println("Tens:\t\t\t" + tens);
    System.out.println("Fives:\t\t\t" + fives);
    System.out.println("Toonies:\t\t" + toonies);
    System.out.println("Loonies:\t\t" + loonies);
    System.out.println("Quarters:\t\t" + quarters);
    System.out.println("Dimes:\t\t\t" + dimes);
    System.out.println("Nickels:\t\t" + nickels);
    System.out.println("Pennies:\t\t" + pennies);

    System.out.println(""); //Inputs a blank line as a spacer

    //Output bill of sale
    System.out.println("Purchase amount: " + nfc.format(purchase));
    System.out.println("Gst tax amount: " + nfc.format(gst));
    System.out.println("Pst tax amount: " + nfc.format(pst));
    System.out.println("Total amount: " + nfc.format(total));
    System.out.println("Tender recieved: " + nfc.format(tender));
    System.out.println("Change amount: " + nfc.format(change));


    } //End of main method
    }//End of class
    Reply With Quote
     

    Bookmarks
    Go Back   CodeGuru Forums > Java Programming > Java Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 12:07 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.