Skip to content

Instantly share code, notes, and snippets.

@ansisec
Last active February 21, 2024 10:24
Show Gist options
  • Save ansisec/95399a2bc811b521b8f3bc8fd8da6b06 to your computer and use it in GitHub Desktop.
Save ansisec/95399a2bc811b521b8f3bc8fd8da6b06 to your computer and use it in GitHub Desktop.
PA - Exercicios 10 e 11
Aula 03 - ficheiros auxiliares
public class JogoEnforcadoIU {
JogoEnforcadoModelo jogo;
Scanner sc;
public JogoEnforcadoIU(JogoEnforcadoModelo jogo) {
this.jogo = jogo;
}
public void jogar() {
sc = new Scanner(System.in);
while (!jogo.concluido()) {
System.out.println("\nSituação atual: " + jogo.getSituacao()); // mostrar as letras descobertas
// se a palavra for CAFE
// inicialmente deverá mostrar: ....
System.out.println("Nr. tentativas: " + jogo.getNrTentativas()); // inicio: 0
System.out.printf("Erros: %d (máx.: %d)\n",jogo.getNrErros(),JogoEnforcadoModelo.getMaxErros());
System.out.println("Caracteres já tentados: " + jogo.getLetrasUsadas());
System.out.print("\nTentativa: ");
String opcao = sc.nextLine().trim();
if (opcao.length()>0)
jogo.tentaOpcao(opcao);
}
if (jogo.acertou())
System.out.printf("Parabéns! Acertou na palavra %s em %d tentativas\n",
jogo.getPalavra(), jogo.getNrTentativas());
else
System.out.println("Perdeste!!! A palavra era: "+jogo.getPalavra());
}
}
public class ReportUI {
Report report;
public void menu() {
int option;
do {
option = PAInput.chooseOption("Report", "Create report","Edit report","Show report","Exit");
switch (option) {
case 1:
String title = PAInput.readString("Report title: ", false);
report = new Report(title);
menuReport();
break;
case 2:
if (report == null)
System.out.println("You must create a report first.");
else
menuReport();
break;
case 3:
showReport();
break;
}
} while (option != 4);
}
private void menuReport() {
int option;
String [] options = {
"Add author",
"Remove author",
"Add text",
"Capitalize sentences",
"Count all the words",
"Number of occurrences of a word",
"Show report",
"Return to the main menu"
};
do {
option = PAInput.chooseOption("Edit report", options);
switch (option) {
case 1 -> addAuthor();
case 2 -> removeAuthor();
case 3 -> addText();
case 4 -> capitalizeSentences();
case 5 -> countWords();
case 6 -> countOccurrences();
case 7 -> showReport();
}
} while (option!=options.length);
}
private void showReport() {
if (report==null) {
System.out.println("You must create a report first.");
return;
}
System.out.println();
System.out.println(report.toString());
System.out.println();
}
private void addAuthor() {
String author = PAInput.readString("New author: ", false);
report.addAuthor(author);
}
private void removeAuthor() {
String author = PAInput.readString("Author to remove: ", false);
report.removeAuthor(author);
}
private void addText() {
String text = PAInput.readString("New sentence: ", false);
report.addText(text);
}
private void capitalizeSentences() {
report.capitalizeSentences();
}
private void countWords() {
System.out.printf("\nNumber of words: %d\n\n",report.getNumberOfWords());
}
private void countOccurrences() {
String word = PAInput.readString("Word to search: ", true);
System.out.printf("\nNumber of occurrences of the word \"%s\": %d\n\n",word,report.getNumberOfOccurrences(word));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment