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

Programming Assignment Unit 3

The document outlines a programming assignment to create a terminal-based Student Management System using Java. It details the implementation of classes, methods for adding, updating, viewing student details, and managing student records using ArrayList and Scanner. The assignment includes code snippets and explanations for each functionality, along with references for further learning.

Uploaded by

mudasirshah129
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programming Assignment Unit 3

The document outlines a programming assignment to create a terminal-based Student Management System using Java. It details the implementation of classes, methods for adding, updating, viewing student details, and managing student records using ArrayList and Scanner. The assignment includes code snippets and explanations for each functionality, along with references for further learning.

Uploaded by

mudasirshah129
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

1

Programming Assignment Unit 3: Student Management System

Department of Computer Science, University of People

CS 1102-01 Programming 1– AY2024-t4

2 May, 2024
2

Programming Assignment Unit 3: Student Management System

In this assignment I am going to create a simple student management system which will

be terminal based and you can add, view, update, view all student list and also you can exit the

program. I am going to use to built-in function of java for this oneArrayList and another will be

Scanner. References of these will be provided on the reference page.


3

Importing Functions / Methods:

First of all I am going to import libraries which are import for our student management

system. First I am going to import java.util.ArrayList; second will be import java.util.Scanner;

first will be used for storing data while the other will be used for taking input from the user and

storing it in the data.

Code:

import java.util.ArrayList;

import java.util.Scanner;

Student Class:

In this class first I am going to declare variables which am I am going to use (name, id,

age and grade). I set them as private so I can use them in the constructor. Now I am going to

create constructor to initialize the student object, in this object I am going to assign attributes like

name, id, age and grade. After that I am going to create getter to retrieve the values of the private

instance variables, and lastly I am going to create setters to update the values of private instance

variables.

Code:

// Student class to represent a student with name, ID, age, and grade

class Student {

private String name;

private int id;


4

private int age;

private double grade;

// Constructor to initialize the student object

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

this.name = name;

this.id = id;

this.age = age;

this.grade = grade;

// Getters to retrieve the values of private instance variables

public String getName() {

return name;

public int getId() {

return id;

public int getAge() {

return age;
5

public double getGrade() {

return grade;

// Setters to update the values of private instance variables

public void setName(String name) {

this.name = name;

public void setId(int id) {

this.id = id;

public void setAge(int age) {

this.age = age;

public void setGrade(doauble grade) {

this.grade = grade;
6

Main Class:

Now I am going to create main class which is going to contain the program logic. In the

first line I am going to create an array which is going to store the student object in it, I am going

to set it’s modifier as private. Now I am going to create a private variable totalStudents to count

total number of students. Next I am going to create the main method from where the program

will start. In this method first I am going to build a scanner object to get the input from the user,

and at the same time am going to declare another variable which will be I am going to name

choice. After that I am going to create a do-while loop to create the menu. In the do part of the

loop I am going to ask user to choice one of the five options. User will have to enter only integer

values else program will keep ask for user to enter the correct input. In the while part I am using

condition to check the user choice. After that I am going to close the scanner.

Add New Student Method:

In the main class I am going to create another method which will be used to add new

student to the student object. First I am going to print out the line “Enter Student Name: ”, then I

am going to create a variable name in which I am going to use scanner.nexLine() method to get the user

input and store it in the name variable. Next I am going to create a do-while loop. In the do part of the

loop I am going to ask user to “Entert Student ID:” and create a variable id in which I am going to use

scanner.nextInt() method to get the id from the user, then I am going to user scanner.nextLine() method to

consume the newline character like I did with the name variable. Next I am going to user an if statement
7

to check if the id is already exists in the array or not, if it’s already in the array then I am going to show a

message saying “Student with this ID already exists and keep repeating the loop until user give it a new id

else it is going to break the loop. Next I am going to do same thing with age and grade but I am not going

to use any loop in it.

To check if student is already exist or not I am going to create a method. The name of the method

is isIdAlreadyUsed this method will take the id as the parameter and then run a for loop of the students

array. It will return true if ID already exists and false if it is a unique id. (Note: in the if statement which is

mentioned above I am going to use this method to check the id exists or not same method will be also

used in the while statement as a condition.)

Update Student Method:

Now I am going to create another method to update student information based on user input. First

I am going to as user for student id then I am going to declare a variable index which is going to use a

method name findStudentIndex it will take the id as a parameter (I am going to define this function later

in the code). Now I am going try-catch block, in try I am going to write an if statement which will check

the student id provided by the user. If user does not provide it negative it will ask user to choose a field to

update, like name, age, grade, and lastly update all fields now I am going to create a variable fieldChoice

and save user input in this after that I am going to use switch statement to check which field user want to

update. For example if user want to update the name of the student, then I am going to ask from user to

enter the updated name then store it in a newName variable then I am going to get the index from the

students array and sent that to the newName variable, for the rest of the cases logic will be the same, and

for the last case which is update all fields logic will be the same but I will have to prompt user one by one

and save that data in different variables, after that go to the index in the students array and update name

age and grade. Lastly I am going to make a default case which will print “Invalid choice. No fields

updated” and it will return and run switch statement again. If Student information is updated It will print
8

“Student information updated successfully” else it will print “Student ID not found”. If user have given

the wrong value it will go back to the main menu.

View Student Details Method:

Now is time to create a method to view the detail of a student. First I am going to prompt user to

ask the id to view detail of the student, in the next step I am going to create a variable searchId and store

user input in that variable then I am going to call the findStudentIndex function and give searchId as a

parameter. Then I am going to write an if statement and check if user have given the positive integer or

not, after if user have given the correct output it will get the student details an print it. If user have enter

wrong value it will go back to the main menu.

View All Student Details Method (Optional):

Now I am going to create a method to display a list of all the student in the array. First I am going

to check if array named students is empty or not. If it’s empty I am going to prompt the user with message

“No students available. Please add students first” else I am going prompt user a message “List of All

students” then run a for loop which will display all the student id, name, age, grade in a table format

(without borders) to the user.

Find Student Index Method:

As you know I used this method for finding the index of student id in the students array. First I

created a method name findStudentIndex which take id as parameter (note that this method is private.). In

this method I am running a for loop which will iterates through the students list (array) the counter in this

loop is 0 and the loop will continues as long as I (which is the counter for the loop) is less than the size of

the students list (array). This will ensure that the loop iterates through all the elements in the students. I+

+ will increments the counter I which will move each iteration to the next element in the list. Next I am

going to use if statement in the loop to check if id which is provided by the user is matched in the students
9

list (array) or not. If it’s in the students list it will return I which will be the student id else it will return -1

and exist the loop.

Code of all Main Class:

public class Main {

private static ArrayList<Student> students = new ArrayList<>(); // List to store student objects

private static int totalStudents = 0; // Counter for the total number of students

// Main method where the program execution starts

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in); // Scanner for user input

int choice;

// Do-while loop for the main menu until the user chooses to exit (option 6)

do {

// Displaying the main menu

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. View All Students");


10

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

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

choice = scanner.nextInt(); // User input for the menu choice

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

// Switch statement to perform actions based on the user's choice

switch (choice) {

case 1:

addNewStudent(scanner);

break;

case 2:

updateStudentInformation(scanner);

break;

case 3:

viewStudentDetails(scanner);

break;

case 4:

viewAllStudents();

break;
11

case 5:

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

break;

default:

System.out.println("Invalid choice. Please enter a valid option.");

} while (choice != 5); // Continue the loop until the user chooses to exit

scanner.close(); // Close the scanner to prevent resource leak

// Method to add a new student to the list

private static void addNewStudent(Scanner scanner) {

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

String name = scanner.nextLine();

int id;

// Validate and ensure the uniqueness of the student ID

do {

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

id = scanner.nextInt();
12

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

if (isIdAlreadyUsed(id)) {

System.out.println("Student with this ID already exists. Please use a different ID.");

} while (isIdAlreadyUsed(id));

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

int age = scanner.nextInt();

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

double grade = scanner.nextDouble();

// Create a new student object and add it to the list

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

totalStudents++;

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

// Method to check if a student ID is already used


13

private static boolean isIdAlreadyUsed(int id) {

for (Student student : students) {

if (student.getId() == id) {

return true; // ID is already used

return false; // ID is unique

// Method to update student information based on user input

private static void updateStudentInformation(Scanner scanner) {

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

int searchId = scanner.nextInt();

int index = findStudentIndex(searchId);

if (index != -1) {

// Display options to choose the field to update

System.out.println("Choose the field to update:");

System.out.println("1. Name");

System.out.println("2. Age");
14

System.out.println("3. Grade");

System.out.println("4. Update All Fields");

int fieldChoice = scanner.nextInt();

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

switch (fieldChoice) {

case 1:

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

String newName = scanner.nextLine();

students.get(index).setName(newName);

break;

case 2:

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

int newAge = scanner.nextInt();

students.get(index).setAge(newAge);

break;

case 3:

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

double newGrade = scanner.nextDouble();


15

students.get(index).setGrade(newGrade);

break;

case 4:

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

String updatedName = scanner.nextLine();

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

int updatedAge = scanner.nextInt();

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

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

double updatedGrade = scanner.nextDouble();

// Update all fields at once

Student student = students.get(index);

student.setName(updatedName);

student.setAge(updatedAge);

student.setGrade(updatedGrade);

break;

default:

System.out.println("Invalid choice. No fields updated.");

return;
16

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

} else {

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

// Method to view details of a specific student based on user input

private static void viewStudentDetails(Scanner scanner) {

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

int searchId = scanner.nextInt();

int index = findStudentIndex(searchId);

if (index != -1) {

// Display details of the selected student

Student student = students.get(index);

System.out.println("Student Details:");

System.out.println("Name: " + student.getName());

System.out.println("ID: " + student.getId());

System.out.println("Age: " + student.getAge());

System.out.println("Grade: " + student.getGrade());

} else {
17

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

// Method to view details of all students

private static void viewAllStudents() {

if (students.isEmpty()) {

System.out.println("No students available. Please add students first.");

} else {

// Display details (ID and name) of all students

System.out.println("List of All Students:");

for (Student student : students) {

System.out.println("ID: " + student.getId() + ", Name: " + student.getName());

// Method to find the index of a student in the list based on ID

private static int findStudentIndex(int id) {

for (int i = 0; i < students.size(); i++) {

if (students.get(i).getId() == id) {

return i; // Return the index if the student is found


18

return -1; // Not found

Output:

The output of this program is shown in the figure 1.1.


19

Figure 1.1

References

Java User Input (Scanner class). (n.d.). https://www.w3schools.com/java/java_user_input.asp

Javanotes 9, Section 3.5 -- the IF statement. (n.d.). https://math.hws.edu/javanotes/c3/s5.html

Java User Input (Scanner class). (n.d.). https://www.w3schools.com/java/java_user_input.asp

Javanotes 9, Section 3.3 -- The while and do..while Statements. (n.d.-a).

https://math.hws.edu/javanotes/c3/s3.html

Javanotes 9, Section 3.6 -- The switch Statement. (n.d.).

https://math.hws.edu/javanotes/c3/s6.html

Java ArrayList. (n.d.). https://www.w3schools.com/java/java_arraylist.asp

Java Constructors. (n.d.). https://www.w3schools.com/java/java_constructors.aspll

Javanotes 9, Section 4.8 -- The truth about declarations. (n.d.).

https://math.hws.edu/javanotes/c4/s8.html

Javanotes 9, Section 4.4 -- Return values. (n.d.). https://math.hws.edu/javanotes/c4/s4.html

Javanotes 9, Section 4.6 -- APIs, Packages, Modules, and Javadoc. (n.d.).

https://math.hws.edu/javanotes/c4/s6.html

Javanotes 9, Section 4.2 -- Static subroutines and static variables. (n.d.).

https://math.hws.edu/javanotes/c4/s2.html

You might also like