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


good morning/afternoon,

below code i've written java class ( in school homework ). i'm not here trying homework me.

my question is: can switch/case used how have set? prompt user input 1 or 2. answer 1 getting sandwich, answer 2 getting salad.

the instructions program starts customer order sandwich or salad. proceed add toppings additional charge , can decline toppings well.then print out order , total. professor specified use if statements , switch. however, have feeling missing because looks switch replacement of if statements in examples i've found on stack. thank , assistance!

import java.util.scanner;  //loaded scanner take user input public class restaurant   //dude change this, 3 outputs asking input 1 input 2 input 3 make if statements after { /* author:     date:     program: create class offer sandwich $7.00 optional 3     toppings lettuce, tomato, cheese $1.00 each    or salad  optional 2 toppings tomatos, 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 sandwich or 2 salad");       int userinput = scanner.nextline();        switch(userinput)       {          case 1: // sandwich ordered                 system.out.println("enter 1 additional topping of lettuce or press 2");             int userinput1 = scanner.nextline();               system.out.println("enter 1 additional topping of cheese or press 2");             int userinput2 = scanner.nextline();               system.out.println("enter 1 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 total of: " + sandtotal + " thank you, have great day!");                   if (userinput1 == 1 && userinput2 == 2 && userinput3 == 2)                {                   sandtotal = (sandwich + sandlet);                   system.out.println("your bill salad additional tomato toppings comes too: " + sandtotal + " thank you, have great day!");                     if (userinput1 == 1 && userinput2 == 1 && userinput3 == 2)                   {                      sandtotal = (sandwich + sandlet + sandche);                      system.out.println("your bill salad no additional toppings comes too: " + salad + " thank you, have great day!");                        if (userinput1 == 1 && userinput2 == 2 && userinput3 == 1)                      {                         sandtotal = (sandwich + sandlet + sandtom );                         system.out.println("your bill sandwich `enter code here`lettuce , tomato comes too: " + sandtotal + " thank you, have great day!");                                if (userinput1 == 2 && userinput2 == 1 && userinput == 1)                         {                             sandtotal = (sandwich + sandche + sandtom);                            system.out.println("your bill sandwich cheese , tomato comes too: " + sandtotal + " thank you, have great day!");                              if (userinput1 == 2 && userinput2 == 2 && userinput3 == 2)                            {                               system.out.println("your bill sandwich comes too: " + sandwich + " thank you, have 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: // salad ordered                 {                system.out.println("press 1 additional topping of cheese or press 2");                int userinput1 = scanner.nextline();                   system.out.println("press 1 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 total of: " + saladtotal + " thank you, have great day!");                      if (userinput1 == 2 && userinput2 == 1)                   {                      saladtotal = (salad + saltom);                      system.out.println("your bill salad additional tomato toppings comes too: " + saladtotal + " thank you, have great day!");                        if (userinput1 == 2 && userinput2 == 2)                      {                         system.out.println("your bill salad no additional toppings comes too: " + salad + " thank you, have great day!");                           if (userinput1 == 1 && userinput2 == 1)                          {                            saladtotal = (salad + salche + saltom );                            system.out.println("your bill salad tomato , cheese toppings comes too: " + saladtotal + " thank you, have great day!");                          }//end if 1                      }//end if 2                   }//end if 3                }//end if 4               }    }// end of class 

in general, switch-case statements , if/else blocks interchangeable. switch-cases bit different because of use of break (an example below). though in cases if-statements used. use switch-case handling cases based on enumerations.

lets have enumeration foodtype:

public enum foodtype {     sandwitch,     salad,     fruit } 

so if want ask user questions based on food chooses, this:

foodtype chosentype = foodtype.sandwitch; // can user input  switch (chosentype) { case sandwitch:     system.out.println("you want cheese?"); case salad:     system.out.println("sauce?");     break; case fruit:     system.out.println("which one?");     break; default:     throw new illegalstateexception(); } 

note here:

  • when sandwitch chosen, questions 'you want cheese?' , 'sauce?' printed, because there no break statement.
  • when fruit chosen, 'which one?' printed.
  • default happen, if user chose somthing else, not sandwitch or salad or fruit.

you of course write if statements well:

if (foodtype.salad.equals(chosentype)) {...} 

when there few cases rater use simple if statements. also, code inside 'case' should not overly long/complicated. think if want know when use statement; there sure blog posts or stackoverflow answers.

// sidenote: aware default: not reachable code in example.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -