-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentRecordGUI.java
More file actions
159 lines (148 loc) · 4.95 KB
/
StudentRecordGUI.java
File metadata and controls
159 lines (148 loc) · 4.95 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
import javax.swing.*;
class Node {
String name, course;
int rollno, marks;
Node next;
Node(String name, int rollno, String course, int marks) {
this.name = name;
this.rollno = rollno;
this.course = course;
this.marks = marks;
next = null;
}
}
class LinkedList {
Node head;
LinkedList() {
head = null;
}
void insert(String name, int rollno, String course, int marks) {
Node newNode = new Node(name, rollno, course, marks);
if (head == null) {
head = newNode;
} else {
Node curr = head;
while (curr.next != null) {
curr = curr.next;
}
curr.next = newNode;
}
JOptionPane.showMessageDialog(null, "Record inserted successfully!");
}
void delete(int rollno) {
if (head == null) {
JOptionPane.showMessageDialog(null, "List is empty!");
return;
}
if (head.rollno == rollno) {
head = head.next;
JOptionPane.showMessageDialog(null, "Record deleted successfully!");
return;
}
Node curr = head;
while (curr.next != null) {
if (curr.next.rollno == rollno) {
curr.next = curr.next.next;
JOptionPane.showMessageDialog(null, "Record deleted successfully!");
return;
}
curr = curr.next;
}
JOptionPane.showMessageDialog(null, "Record not found!");
}
void show() {
if (head == null) {
JOptionPane.showMessageDialog(null, "List is empty!");
return;
}
String[] columnNames = {"Name", "Roll No.", "Course", "Marks"};
Object[][] data = new Object[count()][4];
Node curr = head;
int i = 0;
while (curr != null) {
data[i][0] = curr.name;
data[i][1] = curr.rollno;
data[i][2] = curr.course;
data[i][3] = curr.marks;
curr = curr.next;
i++;
}
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
JFrame frame = new JFrame("Student Record Management System");
frame.add(scrollPane);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
void search(int rollno) {
if (head == null) {
JOptionPane.showMessageDialog(null, "List is empty!");
return;
}
Node curr = head;
while (curr != null) {
if (curr.rollno == rollno) {
String[] columnNames = {"Name", "Roll No.", "Course", "Marks"};
Object[][] data = new Object[1][4];
data[0][0] = curr.name;
data[0][1] = curr.rollno;
data[0][2] = curr.course;
data[0][3] = curr.marks;
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
JFrame frame = new JFrame("Student Record Management System");
frame.add(scrollPane);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
JOptionPane.showMessageDialog(null, "Record found!");
return;
}
curr = curr.next;}
}
int count() {
int count = 0;
Node curr = head;
while (curr != null) {
count++;
curr = curr.next;
}
return count;
}
}
public class StudentRecordGUI {
public static void main(String[] args) {
LinkedList list = new LinkedList();
while (true) {
String[] options = {"Add Record", "Delete Record", "Show Records", "Search Record", "Exit"};
int choice = JOptionPane.showOptionDialog(null, "Select an option", "Student Record Management System",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
options, options[0]);
switch (choice) {
case 0:
String name = JOptionPane.showInputDialog("Enter name:");
int rollno = Integer.parseInt(JOptionPane.showInputDialog("Enter roll number:"));
String course = JOptionPane.showInputDialog("Enter course:");
int marks = Integer.parseInt(JOptionPane.showInputDialog("Enter marks:"));
list.insert(name, rollno, course, marks);
break;
case 1:
int roll = Integer.parseInt(JOptionPane.showInputDialog("Enter roll number to delete:"));
list.delete(roll);
break;
case 2:
list.show();
break;
case 3:
int rollNum = Integer.parseInt(JOptionPane.showInputDialog("Enter roll number to search:"));
list.search(rollNum);
break;
case 4:
System.exit(0);
default:
JOptionPane.showMessageDialog(null, "Invalid choice!");
}
}
}
}