vendredi 7 avril 2017

Correct use for Switch? Menu option order a Sandwich OR Salad

Good Morning/Afternoon,

Below is a the code I've written for my Java class ( As in school homework ). I'm not here trying to get someone to do the homework for me.

My question is: Can the switch/case be used how I have it set? Prompt user for input 1 or 2. Answer 1 they are getting a sandwich, answer 2 they are getting a salad.

The instructions for this program starts with the customer will order a sandwich or a salad. Then proceed to add toppings for an additional charge and can decline extra toppings as well.Then print out the order and total. The professor specified to use IF statements and the Switch. However, I have a feeling I am missing something because it looks like the Switch is the replacement of the IF statements in the examples I've found on Stack. Thank you for any and all assistance!

import java.util.Scanner;

//loaded scanner to take user input
public class Restaurant


//dude change this, do 3 outputs asking for input 1 input 2 input 3 make those if statements after
{
/* Author: 
   Date: 
   Program: create a class that will offer a sandwich for $7.00 with optional three 
   toppings lettuce, tomato, cheese $1.00 each
   or a salad  with optional two toppings tomotos, cheese $0.50 each.
*/   
   public static void main(String[] args)

   {

   // Declarations sandwich,salad,Sandwich-cheese,Sandwich-tomato,Sandwich-lettuce, salad-cheese, salad-tomato.
      double sandwich = 7.00;
      double salad = 7.00;
      double sandChe = 1.00;
      double sandTom = 1.00;
      double sandLet = 1.00;
      double salChe = .50;
      double salTom = .50;
      int userInput;
      int userInput1;
      int userInput2;
      int userInput3;
      double sandTotal;
      double saladTotal;

      Scanner scanner = new Scanner(System.in);

      System.out.println("Enter 1 for a Sandwich or 2 for a Salad");
      int userInput = scanner.nextLine();

      switch(userInput)
      {
         case 1: // a sandwich was ordered




            System.out.println("Enter 1 for additional topping of lettuce or press 2");
            int userInput1 = scanner.nextLine();


            System.out.println("Enter 1 for additional topping of cheese or press 2");
            int userInput2 = scanner.nextLine();


            System.out.println("Enter 1 for additional topping of tomato or press 2");
            int userInput3 = scanner.nextLine();



            if (userInput1 == 1 && userInput2 == 1 && userInput3 == 1)
            {
               saladTotal = (sandwich + sandLet + sandChe + sandTom);
               System.out.println("Your bill comes to a total of: " + sandTotal + " Thank you, Have a great day!");



               if (userInput1 == 1 && userInput2 == 2 && userInput3 == 2)
               {
                  sandTotal = (sandwich + sandLet);
                  System.out.println("Your bill for a salad with additional tomato toppings comes too: " + sandTotal + " Thank you, Have a great day!");


                  if (userInput1 == 1 && userInput2 == 1 && userInput3 == 2)
                  {
                     sandTotal = (sandwich + sandLet + sandChe);
                     System.out.println("Your bill for a salad with no additional toppings comes too: " + salad + " Thank you, Have a great day!");


                     if (userInput1 == 1 && userInput2 == 2 && userInput3 == 1)
                     {
                        sandTotal = (sandwich + sandLet + sandTom );
                        System.out.println("Your bill for a sandwich `enter code here`lettuce and tomato comes too: " + sandTotal + " Thank you, Have a great day!");     


                        if (userInput1 == 2 && userInput2 == 1 && userInput == 1)
                        { 
                           sandTotal = (sandwich + sandChe + sandTom);
                           System.out.println("Your bill for a sandwich with cheese and tomato comes too: " + sandTotal + " Thank you, Have a great day!");


                           if (userInput1 == 2 && userInput2 == 2 && userInput3 == 2)
                           {
                              System.out.println("Your bill for a sandwich comes too: " + sandwich + " Thank you, Have a great day!");
                           }// end if 1
                        }//end if 2
                     }//end if 3
                  }//end if 4
               }//end if 5
            }//end if 6

      }
   }
}            


         // switch case below


         case 2: // a salad was ordered




            {
               System.out.println("Press 1 for additional topping of cheese or press 2");
               int userInput1 = scanner.nextLine();



               System.out.println("Press 1 for additional topping of tomato or press 2");
               int userInput2 = scanner.nextLine();


               if (userInput1 == 1 && userInput2 == 2)
               {
                  saladTotal = (salad + salChe);
                  System.out.println("Your bill comes to a total of: " + saladTotal + " Thank you, Have a great day!");



                  if (userInput1 == 2 && userInput2 == 1)
                  {
                     saladTotal = (salad + salTom);
                     System.out.println("Your bill for a salad with additional tomato toppings comes too: " + saladTotal + " Thank you, Have a great day!");


                     if (userInput1 == 2 && userInput2 == 2)
                     {
                        System.out.println("Your bill for a salad with no additional toppings comes too: " + salad + " Thank you, Have a great day!");


                        if (userInput1 == 1 && userInput2 == 1)

                        {
                           saladTotal = (salad + salChe + salTom );
                           System.out.println("Your bill for a salad with Tomato and Cheese toppings comes too: " + saladTotal + " Thank you, Have a great day!");

                        }//end if 1
                     }//end if 2
                  }//end if 3
               }//end if 4


            }



}// end of class

Aucun commentaire:

Enregistrer un commentaire