Programming Assignment Unit 3
Programming Assignment Unit 3
2 May, 2024
2
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
First of all I am going to import libraries which are import for our student management
first will be used for storing data while the other will be used for taking input from the user and
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 {
this.name = name;
this.id = id;
this.age = age;
this.grade = grade;
return name;
return id;
return age;
5
return grade;
this.name = name;
this.id = id;
this.age = age;
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.
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 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
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
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
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
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
private static ArrayList<Student> students = new ArrayList<>(); // List to store student objects
private static int totalStudents = 0; // Counter for the total number of students
int choice;
// Do-while loop for the main menu until the user chooses to exit (option 6)
do {
System.out.println("5. Exit");
switch (choice) {
case 1:
addNewStudent(scanner);
break;
case 2:
updateStudentInformation(scanner);
break;
case 3:
viewStudentDetails(scanner);
break;
case 4:
viewAllStudents();
break;
11
case 5:
break;
default:
} while (choice != 5); // Continue the loop until the user chooses to exit
int id;
do {
id = scanner.nextInt();
12
if (isIdAlreadyUsed(id)) {
} while (isIdAlreadyUsed(id));
totalStudents++;
if (student.getId() == id) {
if (index != -1) {
System.out.println("1. Name");
System.out.println("2. Age");
14
System.out.println("3. Grade");
switch (fieldChoice) {
case 1:
students.get(index).setName(newName);
break;
case 2:
students.get(index).setAge(newAge);
break;
case 3:
students.get(index).setGrade(newGrade);
break;
case 4:
student.setName(updatedName);
student.setAge(updatedAge);
student.setGrade(updatedGrade);
break;
default:
return;
16
} else {
if (index != -1) {
System.out.println("Student Details:");
} else {
17
if (students.isEmpty()) {
} else {
if (students.get(i).getId() == id) {
Output:
Figure 1.1
References
https://math.hws.edu/javanotes/c3/s3.html
https://math.hws.edu/javanotes/c3/s6.html
https://math.hws.edu/javanotes/c4/s8.html
https://math.hws.edu/javanotes/c4/s6.html
https://math.hws.edu/javanotes/c4/s2.html