-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuNotebook.java
112 lines (101 loc) · 2.94 KB
/
MenuNotebook.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class MenuNotebook
{
// Storage for an arbitrary number of notes.
private ArrayList notes;
//An array of strings
private String alternatives[] = {"List", "Add", "Show", "Exit"};
/**
* Perform any initialization that is required for the
* notebook.
*/
public MenuNotebook()
{
notes = new ArrayList();
}
/**
* Store a new note into the notebook.
* @param note The note to be stored.
*/
public void storeNote(String note)
{
notes.add(note);
}
/**
* @return The number of notes currently in the notebook.
*/
public int numberOfNotes()
{
return notes.size();
}
/**
* Show a note.
* @param noteNumber The number of the note to be shown.
*/
public void showNote(int noteNumber)
{
if(noteNumber < 0) {
// This is not a valid note number, so do nothing.
}
else if(noteNumber < numberOfNotes()) {
// This is a valid note number, so we can print it.
System.out.println(notes.get(noteNumber));
}
else {
// This is not a valid note number, so do nothing.
}
}
public void listNotes()
{
int index = 0;
while(index < notes.size()) {
System.out.println(notes.get(index));
index++;
}
}
public void runMenu()
{
int option = getChoice();
while (option != 3)
{
//Action depending on choice
if (option == 0)//List notes
{
if (numberOfNotes() >0 )
{
listNotes();
}
else System.out.println("No notes in list");
}
else if (option == 1) //Add note
{
String newNote= JOptionPane.showInputDialog(null,"Enter a new note","New Note", JOptionPane.PLAIN_MESSAGE);
storeNote(newNote) ;
}
else if (option == 2)
{
if (numberOfNotes() >0 )
{
String strNum= JOptionPane.showInputDialog(null,"Enter a note Number","Show Note", JOptionPane.PLAIN_MESSAGE);
int num = Integer.parseInt(strNum);
if (num <= numberOfNotes())
{
showNote(num) ;
}
else System.out.println("Invalid number");
}
else System.out.println("No notes in list");
}
else JOptionPane.showMessageDialog(null, "No Comprendo " +option);
//Prompt again
option = getChoice();
}//End while
}
public int getChoice()
{
int choice = JOptionPane.showOptionDialog(null, "Select from ..","Notebook Menu",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, alternatives, alternatives[0]);
return choice;
}
}