Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 15, 2021 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/f4fb43be1a826328e5e3459ccdb764a7 to your computer and use it in GitHub Desktop.
Save parzibyte/f4fb43be1a826328e5e3459ccdb764a7 to your computer and use it in GitHub Desktop.
class Pregunta {
private final String pregunta;
private final List<Respuesta> respuestas;
// Nota: si crees que agregarás más respuestas, amplía las letras
private static final String letras = "abcdefghijklmnñopqrstuvwxyz";
public Pregunta(String pregunta, List<Respuesta> respuestas) {
this.pregunta = pregunta;
this.respuestas = respuestas;
}
public Pregunta(String pregunta) {
this.pregunta = pregunta;
this.respuestas = new ArrayList<>();
}
public void agregarRespuesta(Respuesta r) {
this.respuestas.add(r);
}
public boolean respuestaCorrecta(char respuesta) {
int indice = letras.indexOf(respuesta);
if (indice == -1) {
return false;
}
return this.respuestas.get(indice).esCorrecta;
}
public boolean preguntar(int numero) {
System.out.println(numero + ". " + this.pregunta);
int indice = 0;
for (Respuesta r : this.respuestas) {
System.out.printf("%c) %s\n", letras.charAt(indice), r.respuesta);
indice++;
}
System.out.println("Elige: ");
Scanner sc = new Scanner(System.in);
char respuesta = sc.nextLine().charAt(0);
return this.respuestaCorrecta(respuesta);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment