-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstd.java
More file actions
116 lines (102 loc) · 3.88 KB
/
std.java
File metadata and controls
116 lines (102 loc) · 3.88 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
import java.util.*;
class Student {
String name;
int rollNumber;
float marks;
public Student(String name, int rollNumber, float marks) {
this.name = name;
this.rollNumber = rollNumber;
this.marks = marks;
}
public void display() {
System.out.println("Name: " + name + ", Roll Number: " + rollNumber + ", Marks: " + marks);
}
}
class StudentManagement {
private static Student[] students = new Student[100];
private static int count = 0; // To track the number of students added
// Method to add a new student
public static void addStudent(String name, int rollNumber, float marks) {
students[count++] = new Student(name, rollNumber, marks);
}
// Method to view all students
public static void viewStudents() {
if (count == 0) {
System.out.println("No students to display!");
} else {
for (int i = 0; i < count; i++) {
students[i].display();
}
}
}
// Method to search student by roll number
public static void searchStudent(int rollNumber) {
for (int i = 0; i < count; i++) {
if (students[i].rollNumber == rollNumber) {
students[i].display();
return;
}
}
System.out.println("Student with Roll Number " + rollNumber + " not found.");
}
// Method to delete student
public static void deleteStudent(int rollNumber) {
for (int i = 0; i < count; i++) {
if (students[i].rollNumber == rollNumber) {
for (int j = i; j < count - 1; j++) {
students[j] = students[j + 1];
}
students[--count] = null; // Reduce count and clear last slot
System.out.println("Student with Roll Number " + rollNumber + " deleted.");
return;
}
}
System.out.println("Student with Roll Number " + rollNumber + " not found.");
}
}
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println("\n=== Student Management System ===");
System.out.println("1. Add Student");
System.out.println("2. View All Students");
System.out.println("3. Search Student by Roll Number");
System.out.println("4. Delete Student by Roll Number");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter Name: ");
String name = sc.next();
System.out.print("Enter Roll Number: ");
int rollNumber = sc.nextInt();
System.out.print("Enter Marks: ");
float marks = sc.nextFloat();
StudentManagement.addStudent(name, rollNumber, marks);
break;
case 2:
StudentManagement.viewStudents();
break;
case 3:
System.out.print("Enter Roll Number to Search: ");
int searchRollNumber = sc.nextInt();
StudentManagement.searchStudent(searchRollNumber);
break;
case 4:
System.out.print("Enter Roll Number to Delete: ");
int deleteRollNumber = sc.nextInt();
StudentManagement.deleteStudent(deleteRollNumber);
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice! Please try again.");
}
} while (choice != 5);
sc.close();
}
}