-
Notifications
You must be signed in to change notification settings - Fork 0
/
Student_management_system.java
141 lines (121 loc) · 3.18 KB
/
Student_management_system.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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Student{
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 int getRollNumber(){
return rollNumber;
}
@Override
public String toString(){
return "Name: "+ name+ ",Roll Number: " + rollNumber+ ",Grade: "+grade;
}
}
class StudentManagementSystem{
private List<Student> students;
public StudentManagementSystem(){
students = new ArrayList<>();
}
public void addStudent(Student student){
students.add(student);
}
public boolean removeStudent(int rollNumber){
for(Student student : students){
if(student.getRollNumber() == rollNumber){
students.remove(student);
return true;
}
}
return false;
}
public Student searchStudent(int rollNumber){
for(Student student : students){
if(student.getRollNumber() == rollNumber){
return student;
}
}
return null;
}
public List<Student> getAllStudents(){
return students;
}
}
public class Student_management_system{
public static void main(String[] args){
Scanner scanner= new Scanner(System.in);
StudentManagementSystem sms= new StudentManagementSystem();
while(true){
System.out.println("1. Add Student");
System.out.println("2. Remove Student");
System.out.println("3. Search Student");
System.out.println("4. Display All Student");
System.out.println("5. Exit");
System.out.println("Enter your choice:");
int choice = scanner.nextInt();
switch(choice){
case 1:
scanner.nextLine();
System.out.print("Enter student name: ");
String name = scanner.nextLine();
System.out.println("Enter roll number: ");
int rollNumber = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter grade: ");
String grade = scanner.nextLine();
Student newStudent = new Student(name, rollNumber, grade);
sms.addStudent(newStudent);
System.out.println("Student added.");
break;
case 2:
System.out.println("Enter roll number of student to remove: ");
int rollToRemove = scanner.nextInt();
boolean removed = sms.removeStudent(rollToRemove);
if(removed){
System.out.println("Student removed.");
}
else{
System.out.println("Student not found.");
}
break;
case 3:
System.out.println("Enter roll number of student to search: ");
int rollToSearch = scanner.nextInt();
Student searchedStudent = sms.searchStudent(rollToSearch);
if(searchedStudent != null){
System.out.println("Student found:");
System.out.println(searchedStudent);
}
else{
System.out.println("Student not found.");
}
break;
case 4:
List<Student> allStudents = sms.getAllStudents();
if (allStudents.isEmpty()){
System.out.println("No students to display.");
}else{
System.out.println("All Students.");
for(Student student : allStudents){
System.out.println(student);
}
}
break;
case 5:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please choose again..");
break;
}
}
}
}