-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentEmailGUI
More file actions
157 lines (124 loc) · 4.19 KB
/
StudentEmailGUI
File metadata and controls
157 lines (124 loc) · 4.19 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
public class StudentEmailGUI extends JFrame
{
// Constants:
// GUI Component:
JTextArea studentTextArea = new JTextArea ();
JLabel idLabel = new JLabel ("ID: ");
JTextField idTextField = new JTextField (10);
JLabel nameLabel = new JLabel ("Name: ");
JTextField nameTextField = new JTextField (10);
JButton addButton = new JButton ("Add");
JButton deleteButton = new JButton ("Delete");
JButton displayAllButton = new JButton ("Display All");
JButton exitButton = new JButton ("Exit");
// Class Instance Data:
private LinkedList<StudentEmail> studentLinkedList = new LinkedList<StudentEmail> ();
public StudentEmailGUI ()
{
JPanel flow1Panel = new JPanel (new FlowLayout (FlowLayout.CENTER));
JPanel flow2Panel = new JPanel (new FlowLayout (FlowLayout.CENTER));
JPanel gridPanel = new JPanel (new GridLayout (2, 1));
studentTextArea.setEditable (false);
flow1Panel.add (idLabel);
flow1Panel.add (idTextField);
flow1Panel.add (nameLabel);
flow1Panel.add (nameTextField);
flow2Panel.add (addButton);
flow2Panel.add (deleteButton);
flow2Panel.add (displayAllButton);
flow2Panel.add (exitButton);
gridPanel.add (flow1Panel);
gridPanel.add (flow2Panel);
add (studentTextArea, BorderLayout.CENTER);
add (gridPanel, BorderLayout.SOUTH);
setLocationRelativeTo (null);
addButton.addActionListener (event -> addStudent ());
displayAllButton.addActionListener (event -> displayAll ());
exitButton.addActionListener (event -> exitApplication ());
deleteButton.addActionListener (event -> deleteStudent ());
}
private boolean isStudentIdInLinkedList (String idStr)
{
boolean inList = false;
for (StudentEmail stud : studentLinkedList)
{
if (stud.getId ().compareToIgnoreCase (idStr) == 0)
{
inList = true;
}
}
return inList;
}
private void addStudent ()
{
if (isStudentIdInLinkedList (idTextField.getText()) == true)
{
JOptionPane.showMessageDialog (null, "Error: student ID is already in the database.");
}
else
{
try
{
StudentEmail stud = new StudentEmail (nameTextField.getText(),
idTextField.getText());
studentLinkedList.add (stud);
displayAll ();
nameTextField.setText("");
idTextField.setText("");
}
catch (StudentEmailException error)
{
JOptionPane.showMessageDialog (null, error.toString ());
// myLabel.setText (error.toString ());
}
}
}
private void deleteStudent ()
{
if (studentLinkedList.size() == 0)
{
JOptionPane.showMessageDialog (null, "Error: Database is empty.");
}
else if (isStudentIdInLinkedList (idTextField.getText()) == false)
{
JOptionPane.showMessageDialog (null, "Error: student ID is not in the database.");
}
else
{
for (int s = 0; s < studentLinkedList.size(); s++)
{
String currId = studentLinkedList.get (s).getId ();
if (currId.compareToIgnoreCase (idTextField.getText()) == 0)
{
studentLinkedList.remove (s);
}
}
displayAll ();
nameTextField.setText("");
idTextField.setText("");
}
}
private void displayAll ()
{
studentTextArea.setText ("");
for (StudentEmail stud : studentLinkedList)
{
studentTextArea.append (stud + "\n");
}
}
private void exitApplication ()
{
System.exit (0); // All is OK.
}
public static void main (String[] args)
{
StudentEmailGUI app = new StudentEmailGUI ();
app.setVisible (true);
app.setSize (500, 310);
app.setLocation (200, 100);
app.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
} // public class StudentEmailGUI