-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
79 lines (67 loc) · 3.07 KB
/
Main.java
File metadata and controls
79 lines (67 loc) · 3.07 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StudentService service = new StudentService();
int choice;
do {
System.out.println("\n===== Student Record Management System =====");
System.out.println("1. Add Student");
System.out.println("2. View All Students");
System.out.println("3. Search Student by ID");
System.out.println("4. Update Student by ID");
System.out.println("5. Delete Student by ID");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
sc.nextLine(); // consume newline
switch (choice) {
case 1 -> {
System.out.print("Enter ID: ");
int id = sc.nextInt();
sc.nextLine();
System.out.print("Enter Name: ");
String name = sc.nextLine();
System.out.print("Enter Age: ");
int age = sc.nextInt();
sc.nextLine();
System.out.print("Enter Course: ");
String course = sc.nextLine();
Student student = new Student(id, name, age, course);
service.addStudent(student);
}
case 2 -> service.viewAllStudents();
case 3 -> {
System.out.print("Enter ID to search: ");
int id = sc.nextInt();
Student s = service.searchStudentById(id);
if (s != null) System.out.println(s);
else System.out.println("⚠ Student not found.");
}
case 4 -> {
System.out.print("Enter ID to update: ");
int id = sc.nextInt();
sc.nextLine();
System.out.print("Enter new Name: ");
String name = sc.nextLine();
System.out.print("Enter new Age: ");
int age = sc.nextInt();
sc.nextLine();
System.out.print("Enter new Course: ");
String course = sc.nextLine();
boolean updated = service.updateStudentById(id, name, age, course);
System.out.println(updated ? "✔ Student updated successfully." : "⚠ Student not found.");
}
case 5 -> {
System.out.print("Enter ID to delete: ");
int id = sc.nextInt();
boolean deleted = service.deleteStudentById(id);
System.out.println(deleted ? "✔ Student deleted successfully." : "⚠ Student not found.");
}
case 6 -> System.out.println("Exiting program. Goodbye!");
default -> System.out.println("Invalid choice! Try again.");
}
} while (choice != 6);
sc.close();
}
}