-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaw Code.txt
More file actions
174 lines (143 loc) · 5.23 KB
/
Raw Code.txt
File metadata and controls
174 lines (143 loc) · 5.23 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
package StudentManagementSystem.java;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
class Student {
String name;
int rollNumber;
double grades;
public Student(String name, int rollNumber, double grades) {
this.name = name;
this.rollNumber = rollNumber;
this.grades = grades;
}
public String getName() {
return name;
}
public int getRollNumber() {
return rollNumber;
}
public double getGrades() {
return grades;
}
public String toString() {
return "Student{name='" + name + "', rollNumber=" + rollNumber + ", grades=" + grades + '}';
}
}
public class StudentManagementSystem {
private static ArrayList<Student> students = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("1. Add Student");
System.out.println("2. Update Student");
System.out.println("3. Delete Student");
System.out.println("4. Display Students");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
if (!scanner.hasNextInt()) {
System.out.println("Invalid input. Please enter a number between 1 and 7.");
scanner.next();
continue;
}
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
addStudent();
break;
case 2:
updateStudent();
break;
case 3:
deleteStudent();
break;
case 4:
displayStudents();
break;
case 5:
System.out.println("Exiting... Goodbye!");
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
private static void addStudent() {
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter roll number: ");
while (!scanner.hasNextInt()) {
System.out.println("Invalid input. Roll number must be an integer.");
scanner.next();
}
int rollNumber = scanner.nextInt();
System.out.print("Enter grades: ");
while (!scanner.hasNextDouble()) {
System.out.println("Invalid input. Grades must be a decimal or integer.");
scanner.next();
}
double grades = scanner.nextDouble();
scanner.nextLine();
students.add(new Student(name, rollNumber, grades));
System.out.println("Student added successfully.");
}
private static void updateStudent() {
System.out.print("Enter roll number of student to update: ");
while (!scanner.hasNextInt()) {
System.out.println("Invalid input. Roll number must be an integer.");
scanner.next();
}
int rollNumber = scanner.nextInt();
scanner.nextLine();
for (Student student : students) {
if (student.getRollNumber() == rollNumber) {
System.out.print("Enter new name: ");
student.name = scanner.nextLine();
System.out.print("Enter new grades: ");
while (!scanner.hasNextDouble()) {
System.out.println("Invalid input. Grades must be a decimal or integer.");
scanner.next();
}
student.grades = scanner.nextDouble();
scanner.nextLine();
System.out.println("Student updated successfully.");
return;
}
}
System.out.println("Student not found.");
}
private static void deleteStudent() {
System.out.print("Enter roll number of student to delete: ");
while (!scanner.hasNextInt()) {
System.out.println("Invalid input. Roll number must be an integer.");
scanner.next();
}
int rollNumber = scanner.nextInt();
scanner.nextLine();
Student studentToRemove = null;
for (Student student : students) {
if (student.getRollNumber() == rollNumber) {
studentToRemove = student;
break;
}
}
if (studentToRemove != null) {
students.remove(studentToRemove);
System.out.println("Student deleted successfully.");
} else {
System.out.println("Student not found.");
}
}
private static void displayStudents() {
if (students.isEmpty()) {
System.out.println("No students to display.");
return;
}
for (Student student : students) {
System.out.println(student);
}
}
}