Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Program not compiling correctly

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Program not compiling correctly

    So i wrote this program and basically, I want it to add up the different devices purchased and list the amount of devices, total price(price of all the devices before tax),sales tax, and final price. Everything is working except the total price. For example if when prompted to select an item from the menu I press 10, which is the same is choosing nothing this works correctly. If prompted to select and item and I just choose one item it will calculate it and display it properly. However when i try to have the program calculate multiple purchases it only adds the last purchase thus giving me an inaccurate total price and sales tax, i'm not sure where i want.

    Edit: Sorry I just figured out how to edit it properly.

     
    package grapefruitelectronicreceipt;
     
    import java.util.Scanner;
     
    /**
     *This program prompts user to select items to purchase and displays a receipt
     * @author
     * @version 9/30/2013
     */
    public class GrapeFruitElectronicReceipt {
     
        /**
         * This method accepts prompts user for total devices purchased then 
         * calculates and displays the information on a receipt
         * @param args the command line arguments
         */
        public static void main(String[] args) {
           //Declare variables
            String sCustomerName;    //Customer's name
            int    nTotal=0;           //Total number of devices ordered
            int    nTPrice=0;          //Total price
            double dTaxRate=0;         //Tax rate
            int    nItem=0;            //The item the user selects
            int    nPrice=0;           //value associated with item
            double   dFinalTotal = 0;    //Total price plus tax
           final int    GPodShuffle = 49;      //value associated with item
           final int    GPodTouch = 299;        //value associated with item
           final int    GPadMini = 329;         //value associated with item
           final  int   GPad2 = 399;            //value associated with item
           final int    GPhone = 199;           //value associated with item
           final int    GMac = 1299;             //value associated with item
           final  int    MacNovelPro = 1199;      //value associated with item
           final   int    MacNovelAir = 999;      //value associated with item
           final   int    Minimac = 599;          //value associated with item
     
            Scanner input = new Scanner (System.in); //Used to read input from the console
     
            //Declare constants
            final double TAX = .065;
            final int    SENTINEL = 10;
     
            //Prompt user to enter name
            System.out.print("Please enter your name:");
            //Record person's name
            sCustomerName = input.nextLine();
     
            //Blank line
            System.out.println();
     
            //Display title GRAPEFRUIT PRODUCT
            System.out.println("GRAPEFRUIT PRODUCT");
            //Display first product
            System.out.println("1." + " gPod shuffle " +   "   $49 " );
            //Display second product
            System.out.println("2." + " gPod Touch "   +     "     $299 ");
            //Display third produc
            System.out.println("3." + " gPad Mini "    + "      $329" );
            //Display fourth product
            System.out.println("4." + " gPad 2 "        + "         $399 ");
            //Display fifth product
            System.out.println("5." + " gPhone "         + "         $199 " );
            //Display sixth product
            System.out.println("6." + " gMac "            + "           $1299 " );
            //Display seventh product
            System.out.println("7." + " MacNovel Pro "     + "   $1199 ");
            //Display eight product
            System.out.println("8." + " MacNovel Air "      + "   $999 ");
            //Display ninth product
            System.out.println("9." + " Minimac "            + "        $599 ");
            //Complete order
            System.out.println("10." + " Complete my order" );
     
     
            //Prompt user to pick an item
            System.out.print("Please select an item from the menu above:");
            //Record their answer
            nItem = input.nextInt();
            //assign value to products
            if (nItem == 1){
                //assign price to gpod
                nPrice = GPodShuffle;
            }
            //assign value to second product
            else if (nItem == 2){
                //assign price to gpod touch
                nPrice = GPodTouch;
            }
            //assign value to third product
            else if (nItem == 3) {
                //assign prodcut to gpad mini
                nPrice = GPadMini;
            }
            //assign value to fourth product
            else if (nItem == 4){
                //assign prodcut to gpad 2
                nPrice = GPad2;
            }
            //assign value to fifth product
            else if (nItem == 5){
                //assign product to gphone
                nPrice = GPhone;
            }
            //assign value to sixth product
            else if (nItem == 6){
                //assign product to gmac
                nPrice = GMac;
            }
            //assign value to seventh product
            else if (nItem == 7) {
                //assign value to macnovel pro
                nPrice = MacNovelPro;
            }
            //assign value to eigth product
            else if (nItem == 8 ) {
                //assign value to macnovel air
                nPrice = MacNovelAir;
            }
            //assign value to ninth product
            else if  (nItem == 9) {
                    nPrice = Minimac;
                } 
             //Price of Items ordered
             nTPrice = nTPrice + nPrice;
            //Declare loop
        while (nItem != SENTINEL){
            //Statement 
     
        System.out.print("Please select another item from the menu above:"); 
        nTotal++;
        //Record item 
        nItem = input.nextInt();
          //assign value to products
            if (nItem == 1){
                //assign price to gpod
                nPrice = GPodShuffle;
            }
            //assign value to second product
            else if (nItem == 2){
                //assign price to gpod touch
                nPrice = GPodTouch;
            }
            //assign value to third product
            else if (nItem == 3) {
                //assign prodcut to gpad mini
                nPrice = GPadMini;
            }
            //assign value to fourth product
            else if (nItem == 4){
                //assign prodcut to gpad 2
                nPrice = GPad2;
            }
            //assign value to fifth product
            else if (nItem == 5){
                //assign product to gphone
                nPrice = GPhone;
            }
            //assign value to sixth product
            else if (nItem == 6){
                //assign product to gmac
                nPrice = GMac;
            }
            //assign value to seventh product
            else if (nItem == 7) {
                //assign value to macnovel pro
                nPrice = MacNovelPro;
            }
            //assign value to eigth product
            else if (nItem == 8 ) {
                //assign value to macnovel air
                nPrice = MacNovelAir;
            }
            //assign value to ninth product
            else if  (nItem == 9) {
                    nPrice = Minimac;
     
                  //Price of Items ordered
             nTPrice = nTPrice + nPrice;
     
     
     
     
                } 
     
     
     
            } 
     
     
     
        //formula for tax rate
        dTaxRate = TAX *nTPrice;
        //formula for total price plus tax
        dFinalTotal = dTaxRate + nTPrice;
     
        //Display thank you message
      System.out.println("Thank you for odering with Grapefruit Company:"
              + sCustomerName);
      //Display items ordered
      System.out.println("Total items ordered:" +  nTotal );
      //Display Price of items ordered
      System.out.println("Price of items ordered:" +  "$" + nTPrice );
      //Display sales tax
      System.out.println("Sales tax:" +  "$" + dTaxRate  );
      //Total amount due
      System.out.println("Total amound due:" +  "$" + dFinalTotal );
     
    }
    }


    Just choosing one device works:
    1. gPod shuffle $49
    2. gPod Touch $299
    3. gPad Mini $329
    4. gPad 2 $399
    5. gPhone $199
    6. gMac $1299
    7. MacNovel Pro $1199
    8. MacNovel Air $999
    9. Minimac $599
    10. Complete my order
    Please select an item from the menu above:10
    Thank you for odering with Grapefruit Company:zach
    Total items ordered:0
    Price of items ordered:$0
    Sales tax:$0.0
    Total amound due:$0.0
    BUILD SUCCESSFUL (total time: 4 seconds)



    No devices works
    GRAPEFRUIT PRODUCT
    GRAPEFRUIT PRODUCT
    1. gPod shuffle $49
    2. gPod Touch $299
    3. gPad Mini $329
    4. gPad 2 $399
    5. gPhone $199
    6. gMac $1299
    7. MacNovel Pro $1199
    8. MacNovel Air $999
    9. Minimac $599
    10. Complete my order
    Please select an item from the menu above:1
    Please select another item from the menu above:10
    Thank you for odering with Grapefruit Company:zach
    Total items ordered:1
    Price of items ordered:$49
    Sales tax:$3.185
    Total amound due:$52.185
    BUILD SUCCESSFUL (total time: 6 seconds)


    This is what happenss when i try to add multiple devices

    Please enter your name:zach

    GRAPEFRUIT PRODUCT
    1. gPod shuffle $49
    2. gPod Touch $299
    3. gPad Mini $329
    4. gPad 2 $399
    5. gPhone $199
    6. gMac $1299
    7. MacNovel Pro $1199
    8. MacNovel Air $999
    9. Minimac $599
    10. Complete my order
    Please select an item from the menu above:1
    Please select another item from the menu above:2
    Please select another item from the menu above:10
    Thank you for odering with Grapefruit Company:zach
    Total items ordered:2
    Price of items ordered:$49
    Sales tax:$3.185
    Total amound due:$52.185 It only calculates the first item and leave out the second, I cant figure out why.


  2. #2
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: Program not compiling correctly

    - Edit -

    Looks like I jumped the gun, I didn't see the scope of the program because of the formatting.

    --- Update ---

    // Price of Items ordered
    nTPrice = nTPrice + nPrice;

    Check this part. Have a look when this runs. Only when case 9 is selected. Move it outside that case.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Program not compiling correctly

    Please read the Announcement at the top of the sub-Forums to learn how to post your code in code/formatting tags and how to ask questions that get attention and receive helpful answers.

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Program not compiling correctly

    If I move it outside of the case these are my results

     
          nPrice = MacNovelAir;
            }
            //assign value to ninth product
            else if  (nItem == 9) {
                    nPrice = Minimac;
     
     
     
     
     
     
                } 
     
     
     
            } 
     
              //Price of Items ordered
             nTPrice = nTPrice + nPrice;
     
        //formula for tax rate
        dTaxRate = TAX *nTPrice;
        //formula for total price plus tax
        dFinalTotal = dTaxRate + nTPrice;

    I can calculate two devices:
    GRAPEFRUIT PRODUCT
    1. gPod shuffle $49
    2. gPod Touch $299
    3. gPad Mini $329
    4. gPad 2 $399
    5. gPhone $199
    6. gMac $1299
    7. MacNovel Pro $1199
    8. MacNovel Air $999
    9. Minimac $599
    10. Complete my order
    Please select an item from the menu above:1
    Please select another item from the menu above:2
    Please select another item from the menu above:10
    Thank you for odering with Grapefruit Company:zach
    Total items ordered:2
    Price of items ordered:$348
    Sales tax:$22.62
    Total amound due:$370.62



    But i can no longer just calculate one device :
    GRAPEFRUIT PRODUCT
    1. gPod shuffle $49
    2. gPod Touch $299
    3. gPad Mini $329
    4. gPad 2 $399
    5. gPhone $199
    6. gMac $1299
    7. MacNovel Pro $1199
    8. MacNovel Air $999
    9. Minimac $599
    10. Complete my order
    Please select an item from the menu above:1
    Please select another item from the menu above:10
    Thank you for odering with Grapefruit Company:zach
    Total items ordered:1
    Price of items ordered:$98
    Sales tax:$6.37
    Total amound due:$104.37
    BUILD SUCCESSFUL (total time: 4 seconds) the price should be 49 not 98.

Similar Threads

  1. how to loop correctly this program
    By ericgomez in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2013, 05:27 PM
  2. Program not ending correctly
    By alex067 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 15th, 2012, 07:54 AM
  3. Program not compiling + calling methods help
    By alex067 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 11th, 2012, 07:41 AM
  4. Program not executing correctly
    By daemonlies in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 1st, 2012, 11:14 PM
  5. program not compiling
    By russ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 26th, 2012, 12:39 AM