1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Javascript Loops - having trouble with calculations

Discussion in 'Programming' started by syxx34, Sep 23, 2018.

  1. #1
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    public class Homework_Four {
            static Scanner  scan = new Scanner(System.in);
    //declaring variables
            private static String name,jOptionInput;
            private static int menuChoice,quantity,totalQuantity=0;
            private static double price=0,saleTax=0,totalAmount;
         
         
         
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run(){
             
            //entering name
            System.out.print("Please enter your name: ");
            name=scan.nextLine();
         
            System.out.println(" ");
         
            //Printing the menu
            System.out.println("MENU");
            System.out.println("1. Face painting        $50");
            System.out.println("2. Balloon twisting     $45");
            System.out.println("3. Puppet shows         $55");
            System.out.println("4. Stilt walkers        $60");
            System.out.println("5. Bounce house         $80");
            System.out.println("6. Rock wall            $70");
            System.out.println("7. Petting zoo          $65");
            System.out.println("10. Exit");
     
            System.out.println(" ");
                 
            //using loop to read the selected menu and the quanity and exit when ten is entered
            while(true)
            {
            System.out.print("Please select your options: ");
            menuChoice =scan.nextInt();
            if(menuChoice==10){break;}
            jOptionInput = JOptionPane.showInputDialog(null,"How many hours would you like?","Number of hours",JOptionPane.INFORMATION_MESSAGE);
            quantity = Integer.parseInt(jOptionInput);
            //call method calculateCost to calculate cost
            totalQuantity=totalQuantity+quantity;
            calculateCost(menuChoice,quantity);
            }
            //getting the sale tax and total amount
            saleTax =price*(0.065);
            totalAmount=price+saleTax;
         
            //printing the displays a receipt of this information
            System.out.println();
            System.out.println("Thank you for ordering with Fantasy Entertainment, "+name);
            System.out.println("Total items ordered: "+totalQuantity);
            System.out.println("Price of food ordered: $"+price);
            System.out.println("Sales tax: $"+saleTax);
            System.out.println("Total amount due: $"+totalAmount);
            System.out.println("You saved: $"+ );
            }});}
     
     
       public static void calculateCost(int menu, int quantity ){
           //calculating sale cost
           switch(menu)
           {
               case 1:
                   price=price+(quantity*50);
               break;
               case 2:
                   price=price+(quantity*45);
               break;
               case 3:
                   price=price+(quantity*55);
               break;
               case 4:
                   price=price+(quantity*60);
               break;
               case 5:
                   price=price+(quantity*80);
               break;
               case 6:
                   price=price+(quantity*70);
               break;
               case 7:
                   price=price+(quantity*65);
               break;
               default:
               break;
           } 
         
           }
       }
    PHP:


    I went another way with it. I need to do the following for the very last output which is "You saved: $"

    There two promotions which are:
    1. if you select Face painting and balloons --> $10 off
    2. if you select all options --> $80 off

     
    Last edited: Sep 23, 2018
    syxx34, Sep 23, 2018 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    That looks more like java than javascript to me.
    how do you even run that?
     
    sarahk, Sep 23, 2018 IP
  3. syxx34

    syxx34 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    i use netbeans so right click on file and then click "run file"
     
    syxx34, Sep 23, 2018 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    I've got netbeans but I haven't downloaded all the java components so I'm just going to have to read and see what I find.

    Starting with your function calculateCost - it doesn't return a value when it's called and you have nothing to catch it either: calculateCost(menuChoice,quantity);
    Is it setting a global variable?
    Two lines down you use price: saleTax =price*(0.065);

    If you are setting an object's properties don't you have to refer to "this"?
     
    sarahk, Sep 23, 2018 IP
  5. syxx34

    syxx34 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    i have no clue....if i did i wouldnt be posting...i am just trying to figure out the savings part.
     
    syxx34, Sep 23, 2018 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #6
    This is your homework - and you're up to assignment 4.
    Go back and read your lecture notes and see how you should be referring to properties.
    If you can't take a suggestion and use trial and error to find out if it might be pointing you in the right direction then you won't get far with programming.
     
    sarahk, Sep 23, 2018 IP