Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda