-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentProjectManagementGUI.java
More file actions
643 lines (527 loc) · 27 KB
/
StudentProjectManagementGUI.java
File metadata and controls
643 lines (527 loc) · 27 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
package STUDENTMANAGEMNT;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class StudentProjectManagementGUI extends JFrame {
private List<Student> students;
private List<Course> courses;
private Map<Integer, Student> rollNumberToStudentMap;
private boolean teacherLoggedIn;
private DefaultTableModel studentTableModel;
private DefaultTableModel courseTableModel;
private JTextArea outputTextArea;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public StudentProjectManagementGUI() {
students = new ArrayList<>();
courses = new ArrayList<>();
rollNumberToStudentMap = new HashMap<>();
teacherLoggedIn = false;
setTitle("Student Project Management");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JTabbedPane tabbedPane = new JTabbedPane();
JPanel actionPanel = createActionPanel();
JPanel studentPanel = createStudentPanel();
JPanel coursePanel = createCoursePanel();
Color backgroundColor = new Color(213, 205, 205);
tabbedPane.addTab("Registration", actionPanel);
actionPanel.setBackground(backgroundColor);
studentPanel.setBackground(backgroundColor);
coursePanel.setBackground(backgroundColor);
actionPanel.setBorder(null);
tabbedPane.addTab("Students", studentPanel);
tabbedPane.addTab("Courses", coursePanel);
tabbedPane.setBackground(Color.black);
tabbedPane.setForeground(Color.gray);
add(tabbedPane, BorderLayout.CENTER);
outputTextArea = new JTextArea();
pack();
setLocationRelativeTo(null);
}
private JPanel createStudentPanel() {
JPanel panel = new JPanel(new BorderLayout());
studentTableModel = new DefaultTableModel();
studentTableModel.addColumn("Name");
studentTableModel.addColumn("ID Number");
studentTableModel.addColumn("Address");
studentTableModel.addColumn("Contact Details");
JTable studentTable = new JTable(studentTableModel);
JScrollPane scrollPane = new JScrollPane(studentTable);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
private JPanel createCoursePanel() {
JPanel panel = new JPanel(new BorderLayout());
courseTableModel = new DefaultTableModel();
courseTableModel.addColumn("Course Name");
courseTableModel.addColumn("Course Code");
JTable courseTable = new JTable(courseTableModel);
JScrollPane scrollPane = new JScrollPane(courseTable);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
private JPanel createActionPanel() {
JPanel panel = new JPanel(null);
JButton addStudentButton = new JButton("Register Student");
addStudentButton.setBounds(415,20, 250, 70);
addStudentButton.setBackground(Color.black);
addStudentButton.setForeground(Color.white);
addStudentButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addStudent();
}
});
JButton addCourseButton = new JButton("Register Course");
addCourseButton.setBounds(415, 110, 250, 70);
addCourseButton.setBackground(Color.black);
addCourseButton.setForeground(Color.white);
addCourseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addCourse();
}
});
JButton assignGradesButton = new JButton("Teachers-Login");
assignGradesButton.setBounds(15, 70, 250, 70);
assignGradesButton.setBackground(Color.black);
assignGradesButton.setForeground(Color.white);
assignGradesButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
assignGrades();
}
});
JButton displayRegisteredStudentsButton = new JButton("Display Registered Students");
displayRegisteredStudentsButton.setBackground(Color.black);
displayRegisteredStudentsButton.setForeground(Color.white);
displayRegisteredStudentsButton.setBounds(15, 340, 250, 70);
displayRegisteredStudentsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayRegisteredStudents();
}
});
JButton displayRegisteredCoursesButton = new JButton("Display Registered Courses");
displayRegisteredCoursesButton.setBounds(15, 430, 250, 70);
displayRegisteredCoursesButton.setBackground(Color.black);
displayRegisteredCoursesButton.setForeground(Color.WHITE);
displayRegisteredCoursesButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayRegisteredCourses();
}
});
panel.add(addStudentButton);
panel.add(addCourseButton);
panel.add(assignGradesButton);
panel.add(displayRegisteredStudentsButton);
panel.add(displayRegisteredCoursesButton);
return panel;
}
private void addStudent() {
JPanel panel = new JPanel(new GridLayout(4, 2));
JTextField nameField = new JTextField();
panel.add(new JLabel("Name:"));
panel.add(nameField);
JTextField rollNumberField = new JTextField();
panel.add(new JLabel("ID Number:"));
panel.add(rollNumberField);
JTextField addressField = new JTextField();
panel.add(new JLabel("Address:"));
panel.add(addressField);
JTextField contactDetailsField = new JTextField();
panel.add(new JLabel("Contact Details:"));
panel.add(contactDetailsField);
int result = JOptionPane.showConfirmDialog(this, panel, "Add Student",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String name = nameField.getText();
int rollNumber = Integer.parseInt(rollNumberField.getText());
String address = addressField.getText();
String contactDetails = contactDetailsField.getText();
// Check if student with the same roll number already exists in the file
boolean studentExistsInFile = false;
try (BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\PC\\Desktop\\studentrecordmanagementsystem\\StudentRecordManagementSystem\\student.txt"))) {
String line;
boolean isFirstLine = true;
while ((line = reader.readLine()) != null) {
if (isFirstLine) {
isFirstLine = false;
continue; // Skip the header row
}
String[] data = line.split("\t\t");
if (data.length >= 2) { // Check if the data array has at least 2 elements
int existingRollNumber = Integer.parseInt(data[1]);
if (existingRollNumber == rollNumber) {
studentExistsInFile = true;
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
if (studentExistsInFile) {
JOptionPane.showMessageDialog(this, "Student with the same ID number already exists in the file.",
"Error", JOptionPane.ERROR_MESSAGE);
} else {
Student student = new Student(name, rollNumber, address, contactDetails);
students.add(student);
rollNumberToStudentMap.put(rollNumber, student);
// Update the student table
studentTableModel.addRow(new Object[]{name, rollNumber, address, contactDetails});
// Write student data to file
try (FileWriter writer = new FileWriter("C:\\Users\\PC\\Desktop\\studentmanagemnt\\STUDENTMANAGEMNT\\student.txt", true);
BufferedWriter bufferedWriter = new BufferedWriter(writer)) {
// Check if file is empty
File file = new File("C:\\Users\\PC\\Desktop\\studentmanagemnt\\STUDENTMANAGEMNT\\student.txt");
if (file.length() == 0) {
bufferedWriter.write("Student Name\t\tRoll Number\t\t\tAddress\t\t\tContact Details\n");
}
bufferedWriter.write(name + "\t\t" + rollNumber + "\t\t" + address + "\t\t" + contactDetails + "\n");
} catch (IOException e) {
e.printStackTrace();
}
outputTextArea.setText("Student added successfully!");
}
}
}
private void addCourse() {
JPanel panel = new JPanel(new GridLayout(2, 2));
JTextField courseNameField = new JTextField();
JTextField courseCodeField = new JTextField();
panel.add(new JLabel("Course Name:"));
panel.add(courseNameField);
panel.add(new JLabel("Course Code:"));
panel.add(courseCodeField);
int result = JOptionPane.showConfirmDialog(this, panel, "Add Course",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String courseName = courseNameField.getText();
String courseCode = courseCodeField.getText();
Course course = new Course(courseName, courseCode);
courses.add(course);
// Update the course table
courseTableModel.addRow(new Object[]{courseName, courseCode});
// Write course data to file
try (FileWriter writer = new FileWriter("C:\\Users\\PC\\Desktop\\studentmanagemnt\\STUDENTMANAGEMNT\\course.txt", true);
BufferedWriter bufferedWriter = new BufferedWriter(writer)) {
// Check if file is empty
File file = new File("C:\\Users\\PC\\Desktop\\studentmanagemnt\\STUDENTMANAGEMNT\\course.txt");
if (file.length() == 0) {
bufferedWriter.write("Course Name\t\tCourse Code\n");
}
bufferedWriter.write(courseName + "\t\t" + courseCode + "\n");
} catch (IOException e) {
e.printStackTrace();
}
outputTextArea.setText("Course added successfully!");
}
}
private void displayRegisteredStudents() {
JPanel panel = new JPanel(new BorderLayout());
JComboBox<Course> courseComboBox = new JComboBox<>(courses.toArray(new Course[0]));
courseComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panel.add(courseComboBox, BorderLayout.NORTH);
JTable studentTable = new JTable();
studentTable.setModel(new DefaultTableModel(
new Object[]{"Name", "ID Number", "Address", "Contact Details"}, 0));
JScrollPane scrollPane = new JScrollPane(studentTable);
panel.add(scrollPane, BorderLayout.CENTER);
JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); // Set FlowLayout alignment to the right
JTextField searchField = new JTextField(15);
JButton searchButton = new JButton("Search");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String searchRollNumber = searchField.getText();
try {
searchStudentsByRollNumber(searchRollNumber, studentTable.getModel());
} catch (StudentNotFoundException ex) {
JOptionPane.showMessageDialog(panel, ex.getMessage(), "Student Not Found", JOptionPane.ERROR_MESSAGE);
}
}
});
searchPanel.add(searchField);
searchPanel.add(searchButton);
panel.add(searchPanel, BorderLayout.NORTH); // Set the search panel in the BorderLayout.NORTH position
JButton displayButton = new JButton("Display");
displayButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel model = (DefaultTableModel) studentTable.getModel();
model.setRowCount(0); // Clear the table
try (BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\PC\\Desktop\\studentmanagemnt\\STUDENTMANAGEMNT\\student.txt"))) {
String line;
boolean isFirstLine = true;
while ((line = reader.readLine()) != null) {
if (isFirstLine) {
isFirstLine = false;
continue; // Skip the header row
}
String[] data = line.split("\t\t");
String name = data[0];
int rollNumber = Integer.parseInt(data[1]);
String address = data[2];
String contactDetails = data[3];
model.addRow(new Object[]{name, rollNumber, address, contactDetails});
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
panel.add(displayButton, BorderLayout.SOUTH);
// Add the panel to your UI or frame
JOptionPane.showMessageDialog(this, panel, "Display Registered Students", JOptionPane.PLAIN_MESSAGE);
}
private void searchStudentsByRollNumber(String rollNumber, TableModel model) throws StudentNotFoundException {
DefaultTableModel defaultModel = (DefaultTableModel) model;
defaultModel.setRowCount(0); // Clear the table
try (BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\PC\\Desktop\\studentmanagemnt\\STUDENTMANAGEMNT\\student.txt"))) {
String line;
boolean isFirstLine = true;
while ((line = reader.readLine()) != null) {
if (isFirstLine) {
isFirstLine = false;
continue; // Skip the header row
}
String[] data = line.split("\t\t");
String name = data[0];
int studentRollNumber = Integer.parseInt(data[1]);
String address = data[2];
String contactDetails = data[3];
if (String.valueOf(studentRollNumber).equalsIgnoreCase(rollNumber)) {
defaultModel.addRow(new Object[]{name, studentRollNumber, address, contactDetails});
return; // Exit the loop after finding the matching student
}
}
} catch (IOException e) {
e.printStackTrace();
}
throw new StudentNotFoundException("No student found with the given roll number.");
}
class StudentNotFoundException extends Exception {
public StudentNotFoundException(String message) {
super(message);
}
}
private void displayRegisteredCourses() {
File file = new File("C:\\Users\\PC\\Desktop\\studentmanagemnt\\STUDENTMANAGEMNT\\course.txt");
if (file.length() == 0) {
JOptionPane.showMessageDialog(this, "No courses available.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
JPanel panel = new JPanel(new BorderLayout());
JTable courseTable = new JTable();
courseTable.setModel(new DefaultTableModel(new Object[]{"Course Name", "Course Code"}, 0));
JScrollPane scrollPane = new JScrollPane(courseTable);
panel.add(scrollPane, BorderLayout.CENTER);
JButton displayButton = new JButton("Display");
displayButton.addActionListener(new ActionListener() {
private Object currentStudent;
public void actionPerformed(ActionEvent e) {
DefaultTableModel model = (DefaultTableModel) courseTable.getModel();
model.setRowCount(0); // Clear the table
List<Course> registeredCourses = new ArrayList<>(); // Create a new list to hold registered courses
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
boolean isFirstLine = true;
while ((line = reader.readLine()) != null) {
if (isFirstLine) {
isFirstLine = false;
continue; // Skip the header row
}
String[] data = line.split("\t\t");
if (data.length >= 2) { // Check if the data array has at least 2 elements
String courseName = data[0];
String courseCode = data[1];
model.addRow(new Object[]{courseName,courseCode});
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}});
panel.add(displayButton, BorderLayout.SOUTH);
JOptionPane.showMessageDialog(this, panel, "Display Registered Courses", JOptionPane.PLAIN_MESSAGE);
}
//assign point and grades to each student for each course from file
private void assignGrades() {
String password = JOptionPane.showInputDialog(this, "Enter password:");
try (BufferedReader passwordReader = new BufferedReader(new FileReader("C:\\Users\\PC\\Desktop\\studentmanagemnt\\STUDENTMANAGEMNT\\password.txt"))) {
String storedPassword;
boolean passwordMatch = false;
while ((storedPassword = passwordReader.readLine()) != null) {
if (password != null && password.equals(storedPassword)) {
passwordMatch = true;
break;
}
}
// Compare the entered password with the stored password
if (passwordMatch) {
JPanel panel = new JPanel(new BorderLayout());
// Create the table with required columns
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.addColumn("Student Name");
tableModel.addColumn("ID Number");
tableModel.addColumn("Address");
tableModel.addColumn("Contact Details");
JTable gradesTable = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(gradesTable);
panel.add(scrollPane, BorderLayout.CENTER);
// Read student data from file and populate the table
try (BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\PC\\Desktop\\studentmanagemnt\\STUDENTMANAGEMNT\\student.txt"))) {
String line;
int lineCounter = 0;
while ((line = reader.readLine()) != null) {
if (lineCounter > 0) { // Skip the first line
String[] studentData = line.split("\t\t");
tableModel.addRow(studentData);
}
lineCounter++;
}
} catch (IOException ex) {
ex.printStackTrace();
}
JButton assignButton = new JButton("Assign Grades");
assignButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Get the data from the table and assign grades for each course
for (int i = 0; i < tableModel.getRowCount(); i++) {
String studentName = (String) tableModel.getValueAt(i, 0);
String idNumber = (String) tableModel.getValueAt(i, 1);
String address = (String) tableModel.getValueAt(i, 2);
String contactDetails = (String) tableModel.getValueAt(i, 3);
// Read course data from file
try (BufferedReader courseReader = new BufferedReader(new FileReader("C:\\Users\\PC\\Desktop\\studentmanagemnt\\STUDENTMANAGEMNT\\course.txt"))) {
String courseLine;
int courseCounter = 0;
while ((courseLine = courseReader.readLine()) != null) {
if (courseCounter > 0) { // Skip the first line
String[] courseData = courseLine.split("\t\t");
String courseIdFromFile = courseData[0];
String courseName = courseData[0];
// Get the points entered by the teacher for the current course
String points = JOptionPane.showInputDialog(panel, "Enter points for " + studentName + " in " + courseName + ":");
if (points != null) {
// Assign grades based on student data and course
String grades = calculateGrades(points, courseName);
// Update the table with assigned grades and points
tableModel.addColumn(courseName + " Points"); // Add column for course points
tableModel.addColumn(courseName + " Grades"); // Add column for course grades
tableModel.setValueAt(points, i, tableModel.getColumnCount() - 2); // Set points value in table
tableModel.setValueAt(grades, i, tableModel.getColumnCount() - 1); // Set grades value in table
}
}
courseCounter++;
}ArrayList<String> coursePoints = new ArrayList<>();
ArrayList<String> courseGrades = new ArrayList<>();
while ((courseLine = courseReader.readLine()) != null) {
if (courseCounter > 0) { // Skip the first line
String[] courseData = courseLine.split("\t\t");
String courseIdFromFile = courseData[0];
String courseName = courseData[0];
// Get the points entered by the teacher for the current course
String points = JOptionPane.showInputDialog(panel, "Enter points for " + studentName + " in " + courseName + ":");
if (points != null) {
// Assign grades based on student data and course
String grades = calculateGrades(points, courseName);
// Add course points and grades to ArrayLists
coursePoints.add(points);
courseGrades.add(grades);
}
}
courseCounter++;
}
for (int k = 0; k < tableModel.getRowCount(); k++) {
for (int j = 0; j < coursePoints.size(); j++) {
// Set points value in table
tableModel.setValueAt(coursePoints.get(j), k, (j * 2) + 2);
// Set grades value in table
tableModel.setValueAt(courseGrades.get(j), k, (j * 2) + 3);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
// Write the table data to the grades file
try (PrintWriter writer = new PrintWriter(new FileWriter("C:\\Users\\PC\\Desktop\\studentmanagemnt\\STUDENTMANAGEMNT\\grades.txt"))) {
for (int i = 0; i < tableModel.getRowCount(); i++) {
StringBuilder line = new StringBuilder();
for (int j = 0; j < tableModel.getColumnCount(); j++) {
line.append(tableModel.getValueAt(i, j));
if (j < tableModel.getColumnCount() - 1) {
line.append("\t\t");
}
}
writer.println(line.toString());
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
panel.add(assignButton, BorderLayout.SOUTH);
JOptionPane.showMessageDialog(this, panel);
} else {
JOptionPane.showMessageDialog(this, "Invalid password!");
}
}catch (IOException ex) {
ex.printStackTrace();
}
}
private String calculateGrades(String points, String courseName) {
int pointsValue = Integer.parseInt(points);
if (pointsValue >= 90 && pointsValue <= 100) {
return "A+";
}
if (pointsValue > 85 && pointsValue < 90) {
return "A";
} if (pointsValue >= 80 && pointsValue < 85) {
return "A-";
}else if (pointsValue >= 75 && pointsValue < 80) {
return "B+";
} else if (pointsValue >= 70 && pointsValue < 75) {
return "B";
} else if (pointsValue >= 65 && pointsValue < 70) {
return "B-";
} else if (pointsValue >= 60 && pointsValue < 65) {
return "C+";
} else if (pointsValue >= 50 && pointsValue < 60) {
return "C";
} else if (pointsValue >= 45 && pointsValue < 50) {
return "C-";
} else if (pointsValue >= 40 && pointsValue < 45) {
return "D";
} else if (pointsValue >= 0 && pointsValue < 40) {
return "F";
}
// Return the course name as the grade if it is not recognized
else{
return "NG";
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
StudentProjectManagementGUI graphics = new StudentProjectManagementGUI();
graphics.setSize(700, 600);
graphics.setVisible(true);
graphics.setResizable(false);
graphics.setUndecorated(true);
}});
}
}