-
Notifications
You must be signed in to change notification settings - Fork 0
/
task5.java
143 lines (127 loc) · 4.38 KB
/
task5.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
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
import java.io.*;
import java.util.*;
class Student implements Serializable {
private String name;
private int rollNumber;
private String grade;
public Student(String name, int rollNumber, String grade) {
this.name = name;
this.rollNumber = rollNumber;
this.grade = grade;
}
public String getName() {
return name;
}
public int getRollNumber() {
return rollNumber;
}
public String getGrade() {
return grade;
}
public String toString() {
return "Name: " + name + ", Roll Number: " + rollNumber + ", Grade: " + grade;
}
}
public class SMS {
private static final String FILENAME="stud.dat";
Map<Integer,Student> map;
public SMS(){
map=new HashMap<>();
loadFromFile();
}
public void addStudent(Student stud){
map.put(stud.getRollNumber(),stud);
saveToFile();
}
public void removeStudent(int rollNumber){
map.remove(rollNumber);
saveToFile();
}
public Student findStudent(int rollNumber){
return map.get(rollNumber);
}
public List<Student> listAllStudens(){
return new ArrayList<>(map.values());
}
@SuppressWarnings("unchecked")
public void loadFromFile(){
try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILENAME))){
map= (Map<Integer,Student>)ois.readObject();
}
catch(IOException|ClassNotFoundException e ){
map=new HashMap<>();
}
}
public void saveToFile(){
try(ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream(FILENAME))){
oos.writeObject(map);
}
catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
SMS sms = new SMS();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Student Management System");
System.out.println("1. Add Student");
System.out.println("2. Remove Student");
System.out.println("3. Search Student");
System.out.println("4. Display All Students");
System.out.println("5. Exit");
System.out.print("Select an option: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter roll number: ");
int rollNumber = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter grade: ");
String grade = scanner.nextLine();
Student student = new Student(name, rollNumber, grade);
sms.addStudent(student);
break;
case 2:
System.out.println("Enter rollNo");
int rollNo=scanner.nextInt();
sms.removeStudent(rollNo);
break;
case 3:
System.out.print("Enter rollNo");
rollNo=scanner.nextInt();
System.out.println();
Student st=sms.findStudent(rollNo);
if(st!=null){
System.out.println("Student found\n");
System.out.println(st);
}
else{
System.out.println("Student not found");
}
break;
case 4:
List<Student> allStudents= (sms.listAllStudens());
if(allStudents.isEmpty()){
System.out.println("No students in the system");
}
else{
for(Student s: allStudents){
System.out.println(s);
}
}
break;
case 5:
System.out.println("Exiting application");
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid option . Please try again.");
}
}
}
}