Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda