-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudentManagementSystem.java
113 lines (101 loc) · 4.23 KB
/
StudentManagementSystem.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
package main;
import java.io.*;
import java.util.*;
public class StudentManagementSystem {
private List<Student> students;
public StudentManagementSystem() {
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void listStudents() {
if (students.isEmpty()) {
System.out.println("No students found.");
} else {
for (Student student : students) {
System.out.println(student);
}
}
}
public void saveToFile(String fileName) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
for (Student student : students) {
writer.write(student.getName() + "," + student.getAge() + "," + student.getStudentId());
writer.newLine();
}
}
}
public void loadFromFile(String fileName) throws IOException {
students.clear();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3) {
String name = parts[0];
int age = Integer.parseInt(parts[1]);
String studentId = parts[2];
students.add(new Student(name, age, studentId));
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
StudentManagementSystem sms = new StudentManagementSystem();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n--- Student Management System ---");
System.out.println("1. Add Student");
System.out.println("2. List Students");
System.out.println("3. Save to File");
System.out.println("4. Load from File");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter age: ");
int age = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter student ID: ");
String studentId = scanner.nextLine();
sms.addStudent(new Student(name, age, studentId));
break;
case 2:
sms.listStudents();
break;
case 3:
System.out.print("Enter file name to save: ");
String saveFileName = scanner.nextLine();
try {
sms.saveToFile(saveFileName);
System.out.println("Students saved to file.");
} catch (IOException e) {
System.out.println("Error saving to file: " + e.getMessage());
}
break;
case 4:
System.out.print("Enter file name to load: ");
String loadFileName = scanner.nextLine();
try {
sms.loadFromFile(loadFileName);
System.out.println("Students loaded from file.");
} catch (IOException e) {
System.out.println("Error loading from file: " + e.getMessage());
}
break;
case 5:
System.out.println("Exiting... Goodbye!");
scanner.close();
return;
default:
System.out.println("Invalid option. Try again.");
}
}
}
}