-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentManagementSystem.java
More file actions
283 lines (237 loc) · 8.89 KB
/
StudentManagementSystem.java
File metadata and controls
283 lines (237 loc) · 8.89 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StudentManagementSystem extends JFrame implements ActionListener {
// Linked List for storing students
StudentLinkedList studentList = new StudentLinkedList();
// GUI Components
JTextField studentNumberField, surnameField, otherNamesField, searchField;
JTextArea displayArea;
JButton addButton, updateButton, deleteButton, searchButton, displayButton;
JButton sortAscButton, sortDescButton, sortAlphaButton;
// Constructor to set up the GUI
public StudentManagementSystem() {
setTitle("Student Management System");
setSize(700, 600);
setLayout(new FlowLayout());
// Input Fields
studentNumberField = new JTextField(10);
surnameField = new JTextField(10);
otherNamesField = new JTextField(10);
searchField = new JTextField(10);
// Buttons
addButton = new JButton("Add Student");
updateButton = new JButton("Update Student");
deleteButton = new JButton("Delete Student");
searchButton = new JButton("Search Student");
displayButton = new JButton("Display All");
sortAscButton = new JButton("Sort Ascending");
sortDescButton = new JButton("Sort Descending");
sortAlphaButton = new JButton("Sort Alphabetically");
// Text Area
displayArea = new JTextArea(20, 55);
displayArea.setEditable(false);
// Add components to the frame
add(new JLabel("Student Number:"));
add(studentNumberField);
add(new JLabel("Surname:"));
add(surnameField);
add(new JLabel("Other Names:"));
add(otherNamesField);
add(new JLabel("Search Query:"));
add(searchField);
add(addButton);
add(updateButton);
add(deleteButton);
add(searchButton);
add(displayButton);
add(sortAscButton);
add(sortDescButton);
add(sortAlphaButton);
add(new JScrollPane(displayArea));
// Add action listeners
addButton.addActionListener(this);
updateButton.addActionListener(this);
deleteButton.addActionListener(this);
searchButton.addActionListener(this);
displayButton.addActionListener(this);
sortAscButton.addActionListener(this);
sortDescButton.addActionListener(this);
sortAlphaButton.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String number = studentNumberField.getText();
String surname = surnameField.getText();
String otherNames = otherNamesField.getText();
studentList.addStudent(number, surname, otherNames);
displayArea.setText(studentList.getStudentListAsString());
}
if (e.getSource() == updateButton) {
String number = studentNumberField.getText();
String surname = surnameField.getText();
String otherNames = otherNamesField.getText();
studentList.updateStudent(number, surname, otherNames);
displayArea.setText(studentList.getStudentListAsString());
}
if (e.getSource() == deleteButton) {
String number = studentNumberField.getText();
studentList.deleteStudent(number);
displayArea.setText(studentList.getStudentListAsString());
}
if (e.getSource() == searchButton) {
String query = searchField.getText();
displayArea.setText(studentList.getSearchResultsAsString(query));
}
if (e.getSource() == displayButton) {
displayArea.setText(studentList.getStudentListAsString());
}
if (e.getSource() == sortAscButton) {
studentList.sortAscending();
displayArea.setText(studentList.getStudentListAsString());
}
if (e.getSource() == sortDescButton) {
studentList.sortDescending();
displayArea.setText(studentList.getStudentListAsString());
}
if (e.getSource() == sortAlphaButton) {
studentList.sortAlphabetically();
displayArea.setText(studentList.getStudentListAsString());
}
}
public static void main(String[] args) {
new StudentManagementSystem();
}
}
// ========== LINKED LIST ========
class StudentLinkedList {
class Node {
String studentNumber;
String surname;
String otherNames;
Node next;
Node(String studentNumber, String surname, String otherNames) {
this.studentNumber = studentNumber;
this.surname = surname;
this.otherNames = otherNames;
this.next = null;
}
}
private Node head;
// Add Student
public void addStudent(String studentNumber, String surname, String otherNames) {
Node newNode = new Node(studentNumber, surname, otherNames);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}
// Update Student
public void updateStudent(String studentNumber, String newSurname, String newOtherNames) {
Node current = head;
while (current != null) {
if (current.studentNumber.equals(studentNumber)) {
current.surname = newSurname;
current.otherNames = newOtherNames;
return;
}
current = current.next;
}
}
// Delete Student
public void deleteStudent(String studentNumber) {
if (head == null) return;
if (head.studentNumber.equals(studentNumber)) {
head = head.next;
return;
}
Node current = head;
while (current.next != null) {
if (current.next.studentNumber.equals(studentNumber)) {
current.next = current.next.next;
return;
}
current = current.next;
}
}
// Search Results with Partial Matching
public String getSearchResultsAsString(String query) {
StringBuilder sb = new StringBuilder();
Node current = head;
while (current != null) {
if (current.studentNumber.startsWith(query) ||
current.surname.toLowerCase().startsWith(query.toLowerCase()) ||
current.otherNames.toLowerCase().startsWith(query.toLowerCase())) {
sb.append(current.studentNumber + " | " + current.surname + " | " + current.otherNames + "\n");
}
current = current.next;
}
if (sb.length() == 0) {
return "No results found.\n";
}
return sb.toString();
}
// Display All Students
public String getStudentListAsString() {
StringBuilder sb = new StringBuilder();
sb.append("Number | Surname | Other Names\n");
sb.append("------------------------------\n");
Node current = head;
while (current != null) {
sb.append(current.studentNumber + " | " + current.surname + " | " + current.otherNames + "\n");
current = current.next;
}
return sb.toString();
}
// SORTING METHODS
// Sort by Student Number in Ascending Order
public void sortAscending() {
for (Node i = head; i != null && i.next != null; i = i.next) {
for (Node j = i.next; j != null; j = j.next) {
if (i.studentNumber.compareTo(j.studentNumber) > 0) {
swap(i, j);
}
}
}
}
// Sort by Student Number in Descending Order
public void sortDescending() {
for (Node i = head; i != null && i.next != null; i = i.next) {
for (Node j = i.next; j != null; j = j.next) {
if (i.studentNumber.compareTo(j.studentNumber) < 0) {
swap(i, j);
}
}
}
}
// Sort by Surname in Alphabetical Order
public void sortAlphabetically() {
for (Node i = head; i != null && i.next != null; i = i.next) {
for (Node j = i.next; j != null; j = j.next) {
if (i.surname.compareTo(j.surname) > 0) {
swap(i, j);
}
}
}
}
// Helper method to swap two nodes
private void swap(Node i, Node j) {
String tempNum = i.studentNumber;
String tempSurname = i.surname;
String tempOtherNames = i.otherNames;
i.studentNumber = j.studentNumber;
i.surname = j.surname;
i.otherNames = j.otherNames;
j.studentNumber = tempNum;
j.surname = tempSurname;
j.otherNames = tempOtherNames;
}
}