Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
How can I not repeat the same case after I use it

So, I made a burger class with a method for extra stuff, my question is how can I use case 0,1,2 only 1 time, like if I use case 0, I can't use it anymore, I can use only 1 and 2, If I use case 1 after 0 , then I can use only case 2 since I used case 0 and 1 before , It's possible to do something like that ? If yes how ? The code:

         boolean flag=true;
         while(flag){
             System.out.println("Enter your choice for extra toppings ");
             int choice=scanner.nextInt();
             scanner.nextLine();
             switch(choice) {
                 case 0:
                     double salad = 0.35;
                     setAdditional(getAdditional() + salad);
                     System.out.println("salad added\n");
                     break;
                 case 1:
                     double bacon=1.05;
                     setAdditional(getAdditional()+bacon);
                     System.out.println("Bacon added \n");
                     break;
                 case 2:
                     double fries=0.79;
                     setAdditional(getAdditional()+fries);
                     System.out.println("fries added \n");
                     break;
                 case 3:
                     System.out.println("Done");
                     flag=false;

        }

        }
    } ```
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

  boolean flag = true;
    Set<Integer> set = new HashSet<>();
    while (flag) {
        System.out.println("Enter your choice for extra toppings ");
        int choice = scanner.nextInt();
        scanner.nextLine();
            switch (choice) {
                case 0:
                    if (!set.contains(choice)) {
                        double salad = 0.35;
                        setAdditional(getAdditional() + salad);
                        System.out.println("salad added\n");
                        break;
                    }
                    else {
                        System.out.println("Added already");
                    }
                    continue;
                case 1:
                    double bacon = 1.05;
                    setAdditional(getAdditional() + bacon);
                    System.out.println("Bacon added \n");
                    break;
                case 2:
                    double fries = 0.79;
                    setAdditional(getAdditional() + fries);
                    System.out.println("fries added \n");
                    break;
                case 3:
                    System.out.println("Done");
                    flag = false;

            }
            set.add(choice);
        }

    }

so I did it with Set in the end, only for salad, but it's the same for the rest, if someone else needs it.

over 4 years ago · Santiago Trujillo Report

0

your cases go by integer numbers, so an array of boolean with 1 element for every option.

boolean[] allowed = new boolean[options]; (faster than hasMap of string to boolean).

add a check just before the "switch" statement:

if(allowed[choice] && choice != 3) {...}

you should also create an integer constant STOP_OPTION or something like that and use it in the above if-statement and in the final "case" of your switch statement. in your example, set it to 3. then later you can change it without replacing all instances of "3" in your code. but that's more of a styling suggestion.

the "flag" boolean is also redundant, the while loop can just check if choice != 3. be careful of NumberFormatExceptions!

good luck!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!