0% found this document useful (0 votes)
30 views

Programming Assignment Unit 3

The document describes a student record management system built in Java. The system allows users to add, update, and view student records by interacting with a menu. It uses classes to represent students and the overall management system.

Uploaded by

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

Programming Assignment Unit 3

The document describes a student record management system built in Java. The system allows users to add, update, and view student records by interacting with a menu. It uses classes to represent students and the overall management system.

Uploaded by

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

ASSIGNMENT-3 ROBUST STUDENT RECORD MANAGEMENT SYSTEM IN JAVA

import java.util.Scanner;

public class RecordManagementSystem {


public static void main(String[] args) {

// Create an instance of StudentManagement and run the application


StudentManagement studentManagement = new StudentManagement();
studentManagement.run();
}
}

// Class responsible for managing student-related operations


class StudentManagement {
private int totalStudents;
private Student[] studentList;

public StudentManagement() {
this.totalStudents = 0;
this.studentList = new Student[100];
}

public void run() {


Scanner scanner = new Scanner(System.in);
boolean running = true;

while (running) {
// Display menu options
System.out.println("\nStudent Data Storage and Management System\n");
System.out.println("1. Add a new student");
System.out.println("2. Update student information");
System.out.println("3. View all students");
System.out.println("4. Exit\n");
System.out.print("Enter your choice: ");

int choice = scanner.nextInt();

switch (choice) {
case 1:
addStudent(scanner);
break;
case 2:
updateStudent(scanner);
break;
case 3:
displayAllStudents();
break;
case 4:
running = false;
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}

// Method to add a new student


private void addStudent(Scanner scanner) {
System.out.println("\nAdd a new student\n");

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


String id = scanner.next();

System.out.print("Enter student name: ");


scanner.nextLine(); // Consume newline character
String name = scanner.nextLine();

System.out.print("Enter student age: ");


int age = scanner.nextInt();

System.out.print("Enter student grade: ");


int grade = scanner.nextInt();

// Create a new Student object and add it to the studentList array


Student student = new Student(id, name, age, grade);
studentList[totalStudents] = student;
totalStudents++;

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


}

// Method to update student information


private void updateStudent(Scanner scanner) {
System.out.println("\nUpdate student information\n");

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


String id = scanner.next();

boolean found = false;


for (int i = 0; i < totalStudents; i++) {
if (studentList[i].getId().equals(id)) {
System.out.print("Enter new student name: ");
scanner.nextLine(); // Consume newline character
String newName = scanner.nextLine();

System.out.print("Enter new student age: ");


int newAge = scanner.nextInt();

System.out.print("Enter new student grade: ");


int newGrade = scanner.nextInt();

// Update the student's name, age, and grade


studentList[i].setName(newName);
studentList[i].setAge(newAge);
studentList[i].setGrade(newGrade);

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


found = true;
break;
}
}

if (!found) {
System.out.println("Student not found with ID: " + id);
}
}

// Method to display details of all students


private void displayAllStudents() {
if (totalStudents == 0) {
System.out.println("No students in the system.");
} else {
System.out.println("\nAll Students:");
for (int i = 0; i < totalStudents; i++) {
System.out.println(studentList[i]);
}
}
}
}

// Class representing a Student


class Student {
private String id;
private String name;
private int age;
private int grade;
// Constructor to initialize student attributes
public Student(String id, String name, int age, int grade) {
this.id = id;
this.name = name;
this.age = age;
this.grade = grade;
}
// Getter methods
public String getId() {
return id;
}

public String getName() {


return name;
}

public int getAge() {


return age;
}

public int getGrade() {


return grade;
}
// Setter methods
public void setName(String name) {
this.name = name;
}

public void setAge(int age) {


this.age = age;
}

public void setGrade(int grade) {


this.grade = grade;
}
// Method to display student details
@Override
public String toString() {
return "Student ID: " + id + ", Name: " + name + ", Age: " + age + ", Grade: " + grade;
}
}

You might also like