StudentInfoSystem
Fri Dec 22 2023 15:22:59 GMT+0000 (Coordinated Universal Time)
import java.util.ArrayList;
import java.util.Scanner;
public class StudentInformationSystem {
private static ArrayList<Student> students = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
displayMenu();
int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
switch (choice) {
case 1:
addStudent();
break;
case 2:
viewStudentList();
break;
case 3:
searchStudent();
break;
case 4:
System.out.println("Exiting program. Goodbye!");
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
private static void displayMenu() {
System.out.println("Student Information System Menu:");
System.out.println("1. Add Student");
System.out.println("2. View Student List");
System.out.println("3. Search for Student");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
}
private static void addStudent() {
System.out.print("Enter student ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
System.out.print("Enter student name: ");
String name = scanner.nextLine();
students.add(new Student(id, name));
System.out.println("Student added successfully!");
}
private static void viewStudentList() {
if (students.isEmpty()) {
System.out.println("No students in the system yet.");
} else {
System.out.println("Student List:");
for (Student student : students) {
System.out.println("ID: " + student.getId() + ", Name: " + student.getName());
}
}
}
private static void searchStudent() {
System.out.print("Enter student ID to search: ");
int searchId = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
boolean found = false;
for (Student student : students) {
if (student.getId() == searchId) {
System.out.println("Student found: ID: " + student.getId() + ", Name: " + student.getName());
found = true;
break;
}
}
if (!found) {
System.out.println("Student with ID " + searchId + " not found.");
}
}
private static class Student {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
}
PROJECT



Comments