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

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);
}
}

To create the required unit tests, you will need to follow these steps:

1. Create a new directory called "test" within the root directory of your project. This directory will be used to store all the test files.
2. Inside the "test" directory, create a new package called "com.example.test" or any other suitable name. This package will contain all the test classes.
3. Create a new Java class for each test case. For example, create a class called "TestSaveStudent" for the first test case.
4. Add the necessary import statements to the test class to import the required classes and libraries.
5. Write the test code inside the test class. Each test method should have the "@Test" annotation to indicate that it is a unit test.
6. Provide the necessary inputs to the methods being tested and assert the expected results using assertions like `assertEquals()`, `assertTrue()`, etc.
7. Repeat steps 3-6 for each test case.

Here is an example of how the test class for the "TestSaveStudent" test case might look like:

```java
import org.junit.Test;

public class TestSaveStudent {

@Test
public void testSaveStudent() {
// Create a new instance of the StudentManagement class and other necessary objects
StudentManagement studentManagement = new StudentManagement();
Scanner scanner = new Scanner("1\n1\nJohn Doe\n20\njohn\nComputer Science\n");

// Call the method being tested with the necessary inputs
studentManagement.captureStudent(scanner);

// Assert the expected results using assertions
// For example, you can check if the student was saved successfully by checking if the students list is not empty
assertFalse(studentManagement.getStudents().isEmpty());

// Add more assertions to check if the correct student was saved, such as checking the details of the first student in the list
Student savedStudent = studentManagement.getStudents().get(0);
assertEquals(1, savedStudent.getId());
assertEquals("John Doe", savedStudent.getName());
assertEquals(20, savedStudent.getAge());
assertEquals("john", savedStudent.getEmail());
assertEquals("Computer Science", savedStudent.getCourse());
}
}
```

Repeat these steps for each test case, replacing the test class name, method name, and test code accordingly. Remember to use the appropriate assertions and provide all necessary inputs to the methods being tested.

To create the unit tests for the Student Management Application, follow these steps:

1. Create a new package in the same project to store the unit tests. Right-click on the project folder, select "New" -> "Package" and give it a meaningful name, such as "test".

2. Inside the test package, create a new Java class for each test case. Right-click on the test package, select "New" -> "Class", and give it the name of the test case, such as "TestSaveStudent".

3. In each test class, import the necessary classes for the tests.

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

4. Add the `@Test` annotation above each test method to mark it as a test case.

```java
@Test
public void TestSaveStudent() {
// Code for the test
}

@Test
public void TestSearchStudent() {
// Code for the test
}

// Add the test methods for the other test cases similarly
```

5. Implement the code for each test case according to its purpose.

```java
public void TestSaveStudent() {
// Create a new instance of Student
Student student = new Student(1, "John", 20, "john", "Computer Science");

// Add the student to the ArrayList
ArrayList<Student> students = new ArrayList<>();
students.add(student);

// Check if the student was saved successfully
assertTrue(students.contains(student));
}

public void TestSearchStudent() {
// Create a new instance of Student and add it to the ArrayList
Student student = new Student(1, "John", 20, "john", "Computer Science");
ArrayList<Student> students = new ArrayList<>();
students.add(student);

// Search for the student by ID
int searchId = 1;
Student foundStudent = null;
for (Student s : students) {
if (s.getId() == searchId) {
foundStudent = s;
break;
}
}

// Check if the correct student details were returned
assertNotNull(foundStudent);
assertEquals(searchId, foundStudent.getId());
assertEquals("John", foundStudent.getName());
assertEquals(20, foundStudent.getAge());
assertEquals("john", foundStudent.getEmail());
assertEquals("Computer Science", foundStudent.getCourse());
}

// Implement the other test methods similarly
```

6. Run the unit tests by right-clicking on each test method or the test class and selecting "Run As" -> "JUnit Test". This will execute the test and provide feedback on its success or failure.

7. Repeat steps 4 to 6 for each test case.

By following these steps, you can create and execute the unit tests for the Student Management Application to determine the code coverage and ensure the correctness of the application.