-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.java
72 lines (62 loc) · 1.54 KB
/
Menu.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.io.Serializable;
public class Menu implements Serializable {
private List<String> escolhas;
private int nEscolhas;
/**
* Construtor por cópia
*/
public Menu(String[] opcoes) {
this.escolhas = new ArrayList<String>();
for (String op : opcoes)
this.escolhas.add(op);
this.nEscolhas = 0;
}
/**
* Construtor vazio
*/
public Menu(){
this.escolhas = new ArrayList<String>();
this.nEscolhas = 0;
}
/**
* Método para apresentar o menu e ler uma opção.
*
*/
public void executa() {
showMenu();
this.nEscolhas = lerOpcao();
}
public void showMenu() {
for(String opcoes: escolhas){
System.out.println(opcoes);
}
}
/** Ler uma opção válida */
private int lerOpcao() {
int op;
Scanner is = new Scanner(System.in);
System.out.print("Opcao: ");
try{
op = is.nextInt();
}
catch(InputMismatchException e){
op=-1;
}
if (op<0 || op>this.escolhas.size()) {
System.out.println("Opcao Invalida!!!");
return lerOpcao();
}
return op;
}
/**
* Método para obter a última opção lida
*/
public int getOpcao() {
return this.nEscolhas;
}
}