import java.util.ArrayList;

import java.util.Scanner;

public class Student
{
private int id;
private String name;
private int age;
private String email;
private String course;

public Student(int id, String name, int age, String email, String course) {
this.id = id;
this.name = name;
this.age = age;
this.email = email;
this.course = course;
}
public int getId() {
return id;
}

public String getName() {
return name;
}
public int getAge() {
return age;
}

public String getEmail() {
return email;
}

public String getCourse() {
return course;
}
public void displayDetails() {
System.out.println("-----------------------------------------------");
System.out.println("Student ID: " + id);
System.out.println("Student Name: " + name);
System.out.println("Student Age: " + age);
System.out.println("Student Email: " + email);
System.out.println("Student Course: " + course);
System.out.println("-----------------------------------------------");
}

}
public class Main
{
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
int menuSelection;
do {
System.out.println("STUDENT MANAGEMENT APPLICATION");
System.out.println("*************************************************");
System.out.println("Please enter (1) to launch menu or any other key to exit");
if (!scanner.hasNextInt()) {
break;
}
menuSelection = scanner.nextInt();
switch (menuSelection) {
case 1:
System.out.println("Please select one of the menu items:");
System.out.println("(1) Capture a new student");
System.out.println("(2) Search for a student");
System.out.println("(3) Delete a student");
System.out.println("(4) Print a student report");
System.out.println("(5) Exit application");
int menuItem = scanner.nextInt();
switch (menuItem) {
case 1:
captureStudent(students, scanner);
break;
case 2:
searchStudent(students, scanner);
break;
case 3:
deleteStudent(students, scanner);
break;
case 4:
printStudentReport(students);
break;
case 5:
System.out.println("Exiting application");
break;
default:
System.out.println("Invalid menu item");
break;
}
break;
default:
System.out.println("Exiting application");
break;
}
} while (scanner.hasNext());

scanner.close();
}

public static void captureStudent(ArrayList<Student> students, Scanner scanner) {
System.out.println("CAPTURE A NEW STUDENT");
System.out.println("***********************************");
System.out.print("Enter the student ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume newline character
System.out.print("Enter student name: ");
String name = scanner.nextLine();
System.out.print("Enter student age: ");
int age = inputValidAge(scanner);
System.out.print("Enter student email: ");
String email = scanner.nextLine();
System.out.print("Enter student course: ");
String course = scanner.nextLine();

Student student = new Student(id, name, age, email, course);
students.add(student);

System.out.println("Student details have been successfully saved.");
System.out.println("Enter (1) to launch menu or any other key to exit");
}

public static int inputValidAge(Scanner scanner) {
while (true) {
String ageInput = scanner.nextLine();
try {
int age = Integer.parseInt(ageInput);
if (age >= 16) {
return age;
} else {
System.out.println("You have entered an incorrect student age!!!");
System.out.print("Please re-enter the student age: ");
}
} catch (NumberFormatException e) {
System.out.println("You have entered an incorrect student age!!!");
System.out.print("Please re-enter the student age: ");
}
}
}

public static void searchStudent(ArrayList<Student> students, Scanner scanner) {
System.out.print("Enter the student ID to search: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume newline character

boolean found = false;
for (Student student : students) {
if (student.getId() == id) {
student.displayDetails();
found = true;
break;
}
}
if (!found) {
System.out.println("Student with ID: " + id + " was NOT found!!!");
}

System.out.println("Enter (1) to launch menu or any other key to exit");
}

public static void deleteStudent(ArrayList<Student> students, Scanner scanner) {
System.out.print("Enter the student ID to delete: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume newline character

boolean deleted = false;
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() == id) {
System.out.print("Are you sure you want to delete Student " + id + " from the system? ");
String confirmation = scanner.nextLine();
if (confirmation.equalsIgnoreCase("y")) {
students.remove(i);
deleted = true;
System.out.println("Student with student ID: " + id + " was deleted!");
}
break;
}
}
if (!deleted) {
System.out.println("Student with ID: " + id + " was NOT found!!!");
}

System.out.println("Enter (1) to launch menu or any other key to exit");
}

public static void printStudentReport(ArrayList<Student> students) {
System.out.println("STUDENT REPORT");
System.out.println("-----------------------------------------------");
for (int i = 0; i < students.size(); i++) {
System.out.println("STUDENT " + (i + 1));
students.get(i).displayDetails();
}
System.out.println("Enter (1) to launch menu or any other key to exit");
}
}

Your line manager has instructed you to write unit tests to determine the code coverage in the
student management application. You are required to create a test package within the application
you created in the above code, which will contain the required unit tests.
You are required to write the following unit tests:
Test Name: TestSaveStudent()
Test Purpose:To supply the student ID, name, age, email,
and course to the save student method. The
test will determine whether the correct
student will be saved to memory

Test Name: TestSearchStudent ()
Test Purpose: To supply the student id to the search
student method. The test will determine
that the correct student details have been
returned.

Test Name: TestSearchStudent_StudentNotFound()
Test Purpose: To supply an incorrect student ID to the
search student method. The test will
determine that no student was found.

Test Name: TestDeleteStudent()
Test Purpose: To supply the student ID to the delete
student method. The test will determine
that the student has been successfully
deleted.

Test Name: TestDeleteStudent_StudentNotFound()
Test Purpose: To supply an incorrect student ID to the
delete student method. The test will
determine that no student could be found to
delete

Test Name: TestStudentAge_StudentAgeValid()
Test Purpose: To supply a valid student age to the student
age method. The test will determine that the
student’s age is valid.

Test Name: TestStudentAge_StudentAgeInvalid()
Test Purpose: To supply an invalid student age to the
student age method. The test will determine
that the student’s age is less than 16

Test Name: TestStudentAge_StudentAgeInvalidCharacter()
Test Purpose: To supply an invalid character to the student
age method. The test will determine that the
student age supplied is not a number

1 answer

import org.junit.Test;
import static org.junit.Assert.*;

public class StudentManagerTest {

@Test
public void TestSaveStudent() {
ArrayList<Student> students = new ArrayList<>();
Scanner scanner = new Scanner(System.in);

Student student = new Student(1, "John Doe", 18, "john.doe", "Computer Science");
students.add(student);

assertEquals(student, students.get(0));
}

@Test
public void TestSearchStudent() {
ArrayList<Student> students = new ArrayList<>();
Scanner scanner = new Scanner(System.in);

Student student = new Student(1, "John Doe", 18, "john.doe", "Computer Science");
students.add(student);

Student result = searchStudent(students, 1);

assertEquals(student, result);
}

@Test
public void TestSearchStudent_StudentNotFound() {
ArrayList<Student> students = new ArrayList<>();
Scanner scanner = new Scanner(System.in);

Student student = new Student(1, "John Doe", 18, "john.doe", "Computer Science");
students.add(student);

Student result = searchStudent(students, 2);

assertNull(result);
}

@Test
public void TestDeleteStudent() {
ArrayList<Student> students = new ArrayList<>();
Scanner scanner = new Scanner(System.in);

Student student1 = new Student(1, "John Doe", 18, "john.doe", "Computer Science");
Student student2 = new Student(2, "Jane Smith", 20, "jane.smith", "Mathematics");
students.add(student1);
students.add(student2);

deleteStudent(students, 1);

assertFalse(students.contains(student1));
assertTrue(students.contains(student2));
}

@Test
public void TestDeleteStudent_StudentNotFound() {
ArrayList<Student> students = new ArrayList<>();
Scanner scanner = new Scanner(System.in);

Student student1 = new Student(1, "John Doe", 18, "john.doe", "Computer Science");
Student student2 = new Student(2, "Jane Smith", 20, "jane.smith", "Mathematics");
students.add(student1);
students.add(student2);

deleteStudent(students, 3);

assertTrue(students.contains(student1));
assertTrue(students.contains(student2));
}

@Test
public void TestStudentAge_StudentAgeValid() {
Scanner scanner = new Scanner(System.in);

int age = StudentManager.inputValidAge(scanner);

assertEquals(18, age);
}

@Test
public void TestStudentAge_StudentAgeInvalid() {
Scanner scanner = new Scanner(System.in);

ByteArrayInputStream in = new ByteArrayInputStream("15".getBytes());
System.setIn(in);

int age = StudentManager.inputValidAge(scanner);

assertEquals(16, age);
}

@Test
public void TestStudentAge_StudentAgeInvalidCharacter() {
Scanner scanner = new Scanner(System.in);

ByteArrayInputStream in = new ByteArrayInputStream("abc".getBytes());
System.setIn(in);

int age = StudentManager.inputValidAge(scanner);

assertEquals(16, age);
}
}