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

147
Views
How to make a simulation of a shop queue with switch statement and scan utility?

I have to make a Java program using scan, switch and cases in which I can add one customer with a command "add" and remove one customer with a command "remove".

The default number of customers in queue is 5. If the count of customers gets larger than 8 it prints out "This queue is too big." If there is less than 1 customer it prints out "There's nobody in the queue."

I tried to do some of the code, but I have no idea what to do next.

import java.util.Scanner;

public class fronta {
    public static void main(String[] args) {
    
    System.out.println ("This queue has 5 people in it at the moment.");    
    Scanner scan = new Scanner(System.in);
    boolean x = true;
    String b = "ADD";
    int a = 5;
    b = scan.nextLine();
    while(x){
    switch (b) {
    case "ADD":
        
    System.out.println ("This queue has " + a + " people in it at the moment.");
    b = scan.nextLine();
    System.out.println ("This queue is too big");
        break;
    
    default:
    case "EXIT":
        System.out.println("End of simulation.");
        x = false;
        break;  
   }
  }
 }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I think you need somehting like below:

public static void main(String[] args) {
    boolean isExitRequested = false;
    int queueSize = 5;
    System.out.println ("This queue has "+queueSize+" people in it at the moment.");
    Scanner scan = new Scanner(System.in);

    while(scan.hasNextLine()){
        String input = scan.nextLine();
        switch (input){
            case "ADD":
                System.out.println ("This queue has " + queueSize++ + " people in it at the moment.");
                if (queueSize > 8) {
                    System.out.println("This queue is too big");
                }
                break;
            case "REMOVE":
                if (queueSize == 0){
                    System.out.println("There's nobody in the queue.");
                } else {
                    queueSize--;
                }
                break;
            case "EXIT":
                isExitRequested = true;
                break;
            default:
                System.out.println("Unknown input: "+input);
        }

        if(isExitRequested)
            break;
    }

}
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!