Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Import Java - util.ArrayList

Uploaded by

jayatikichu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Import Java - util.ArrayList

Uploaded by

jayatikichu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

INPUT :

import java.util.ArrayList;

import java.util.Scanner;

class Student {

int id;

String name;

int age;

Student(int id, String name, int age) {

this.id = id;

this.name = name;

this.age = age;

@Override

public String toString() {

return "ID: " + id + ", Name: " + name + ", Age: " + age;

public class StudentManagementSystem {

static ArrayList<Student> students = new ArrayList<>();

static Scanner scanner = new Scanner(System.in);


public static void main(String[] args) {

while (true) {

System.out.println("\n===== Student Management System =====");

System.out.println("1. Add Student");

System.out.println("2. View Students");

System.out.println("3. Update Student");

System.out.println("4. Delete Student");

System.out.println("5. Exit");

System.out.print("Enter your choice: ");

int choice = scanner.nextInt();

switch (choice) {

case 1 -> addStudent();

case 2 -> viewStudents();

case 3 -> updateStudent();

case 4 -> deleteStudent();

case 5 -> {

System.out.println("Exiting the program. Goodbye!");

return;

default -> System.out.println("Invalid choice. Please try again.");

}
static void addStudent() {

System.out.print("Enter Student ID: ");

int id = scanner.nextInt();

scanner.nextLine(); // Consume newline

System.out.print("Enter Student Name: ");

String name = scanner.nextLine();

System.out.print("Enter Student Age: ");

int age = scanner.nextInt();

students.add(new Student(id, name, age));

System.out.println("Student added successfully!");

static void viewStudents() {

if (students.isEmpty()) {

System.out.println("No students available.");

} else {

System.out.println("\nList of Students:");

for (Student student : students) {

System.out.println(student);

}
}

static void updateStudent() {

System.out.print("Enter Student ID to update: ");

int id = scanner.nextInt();

for (Student student : students) {

if (student.id == id) {

scanner.nextLine(); // Consume newline

System.out.print("Enter new Name: ");

String name = scanner.nextLine();

System.out.print("Enter new Age: ");

int age = scanner.nextInt();

student.name = name;

student.age = age;

System.out.println("Student updated successfully!");

return;

System.out.println("Student with ID " + id + " not found.");

}
static void deleteStudent() {

System.out.print("Enter Student ID to delete: ");

int id = scanner.nextInt();

for (Student student : students) {

if (student.id == id) {

students.remove(student);

System.out.println("Student deleted successfully!");

return;

System.out.println("Student with ID " + id + " not found.");

OUTPUT:
1. Add Student

2. View Students

3. Update Student

4. Delete Student

5. Exit

Enter your choice: 1

Enter Student ID: 1

Enter Student Name: Harsh

Enter Student Age: 21

Student added successfully!


1. Add Student

2. View Students

3. Update Student

4. Delete Student

5. Exit

Enter your choice: 1

Enter Student ID: 2

Enter Student Name: Riya

Enter Student Age: 22

Student added successfully!

1. Add Student

2. View Students

3. Update Student

4. Delete Student

5. Exit

Enter your choice: 2

List of Students:

ID: 1, Name: Harsh, Age: 21

ID: 2, Name: Riya, Age: 22

You might also like