Week 3 Java

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

import java.util.

Scanner;

class Student {

String name;

int id;

int age;

double grade;

// Constructor

public Student(String name, int id, int age, double grade) {

this.name = name;

this.id = id;

this.age = age;

this.grade = grade;

// Display student details

public void displayDetails() {

System.out.println("Student ID: " + id);

System.out.println("Name: " + name);

System.out.println("Age: " + age);

System.out.println("Grade: " + grade);

class StudentManagement {

// Static variables to store total students and student list

private static int totalStudents = 0;

private static Student[] students = new Student[100]; // Assuming a maximum of 100 students
// Add a new student

public static void addStudent(String name, int age, double grade) {

students[totalStudents] = new Student(name, totalStudents + 1, age, grade);

totalStudents++;

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

// Update student information

public static void updateStudent(int id, String name, int age, double grade) {

if (id > 0 && id <= totalStudents) {

students[id - 1].name = name;

students[id - 1].age = age;

students[id - 1].grade = grade;

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

} else {

System.out.println("Invalid Student ID. Please try again.");

// View student details

public static void viewStudentDetails(int id) {

if (id > 0 && id <= totalStudents) {

students[id - 1].displayDetails();

} else {

System.out.println("Invalid Student ID. Please try again.");

}
// Main method to interact with the administrator interface

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

while (true) {

System.out.println("\nStudent Record Management System");

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

System.out.println("2. Update Student Information");

System.out.println("3. View Student Details");

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

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

int choice = scanner.nextInt();

scanner.nextLine(); // Consume the newline character

switch (choice) {

case 1:

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

String name = scanner.nextLine();

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

int age = scanner.nextInt();

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

double grade = scanner.nextDouble();

addStudent(name, age, grade);

break;

case 2:

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


int updateId = scanner.nextInt();

scanner.nextLine(); // Consume the newline character

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

String newName = scanner.nextLine();

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

int newAge = scanner.nextInt();

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

double newGrade = scanner.nextDouble();

updateStudent(updateId, newName, newAge, newGrade);

break;

case 3:

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

int viewId = scanner.nextInt();

viewStudentDetails(viewId);

break;

case 4:

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

scanner.close();
Documentation:

Student Class: Represents the blueprint for a student with attributes like name, id, age, and grade.

StudentManagement Class: Manages student-related operations such as adding, updating, and viewing
students.

Administrator Interface: Displays a menu for administrators to interact with the StudentManagement
system.

Error Handling: Invalid student ID inputs are handled gracefully.

Instructions for Running the Program: Compile and run the program. Follow the on-screen prompts to
add, update, and view student records.

This example provides a basic structure for a Student Record Management System. You can enhance and
modify it based on specific project requirements and best practices.

You might also like