Question: Please add an extra option to the Java code below and keep the code exactly the same. The option is shown in bold (5. Add
Please add an extra option to the Java code below and keep the code exactly the same. The option is shown in bold (5. Add Enrollment.). Please use separate methods for each of the functionalities below and pass the arguments back and forth as necessary.
1. Add student
2. Add Course
3. Display Student
4. Display Course.
5. Add Enrollment.
6. Exit
When the user wants to Add student , collect student information, such as ID, first name, last name, address, city, state, zip. Store it in the student file. If the file already exists, append the record to the end of the file. Also, verify that the student with the ID does not already exist in the file.
When a user wants to Add course i, collect course information such as Course id, Course name, and Course description. Store it in the course file. If the file already exists, append the record to the end of the file. Also, verify that the course with the ID does not already exist in the file.
When the user wants to Display a student, ask the user to enter the student id, open the student file, search the student file, and display the record if found.
When the user wants to Display the course, is selected, ask the user to enter the course id, open the course file, search the course file, and display the record if found.
When the user wants to Add Enrollment, is selected, ask the user to enter the Enrollment ID, Student ID, Couse ID, Semester, Year, and the Grade. Make sure that the Student ID and course ID entered by the user do exist in the student and course file respectively. You can do this by opening the student and course file and scanning each record for the the student and course id respectively until you find it. If the user is trying to create a new enrollment for a student or a course that does not exist, display the appropriate message.
Please use the following code to make the modification:
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class StudentCourseProgram {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println("Please choose an option:");
System.out.println("1. Add student");
System.out.println("2. Add course");
System.out.println("3. Display student");
System.out.println("4. Display course");
System.out.println("5. Exit");
choice = input.nextInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
addCourse();
break;
case 3:
displayStudent();
break;
case 4:
displayCourse();
break;
case 5:
System.out.println("Exiting program...");
break;
default:
System.out.println("Invalid option. Please try again.");
}
} while (choice != 5);
}
private static void addStudent() {
Scanner input = new Scanner(System.in);
System.out.println("Adding a student...");
System.out.print("Enter student ID: ");
int id = input.nextInt();
System.out.print("Enter student first name: ");
String firstName = input.next();
System.out.print("Enter student last name: ");
String lastName = input.next();
System.out.print("Enter student address: ");
String address = input.next();
System.out.print("Enter student city: ");
String city = input.next();
System.out.print("Enter student state: ");
String state = input.next();
System.out.print("Enter student zip code: ");
int zip = input.nextInt();
try {
FileWriter writer = new FileWriter("student.txt", true);
Scanner reader = new Scanner(new File("student.txt"));
while (reader.hasNextLine()) {
String line = reader.nextLine();
String[] parts = line.split(",");
if (Integer.parseInt(parts[0]) == id) {
System.out.println("Error: Student with that ID already exists.");
return;
}
}
writer.write(id + "," + firstName + "," + lastName + "," + address + "," + city + "," + state + "," + zip + " ");
System.out.println("Student added successfully.");
reader.close();
writer.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
private static void addCourse() {
Scanner input = new Scanner(System.in);
System.out.println("Adding a course...");
System.out.print("Enter course ID: ");
int id = input.nextInt();
System.out.print("Enter course name: ");
String name = input.next();
System.out.print("Enter course description: ");
String description = input.next();
try {
FileWriter writer = new FileWriter("course.txt", true);
Scanner reader = new Scanner(new File("course.txt"));
while (reader.hasNextLine()) {
String line = reader.nextLine();
String[] parts = line.split(",");
if (Integer.parseInt(parts[0]) == id) {
System.out.println("Error: Course with that ID already exists.");
return;
}
}
writer.write(id + "," + name + "," + description + " ");
System.out.println("Course added successfully.");
reader.close();
writer.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
private static void displayCourse() {
try {
Scanner input = new Scanner(System.in);
System.out.println("Displaying course information...");
System.out.print("Enter course ID: ");
int id = input.nextInt();
Scanner reader = new Scanner(new File("course.txt"));
boolean found = false;
while (reader.hasNextLine()) {
String line = reader.nextLine();
String[] parts = line.split(",");
if (Integer.parseInt(parts[0]) == id) {
found = true;
System.out.println("Course ID: " + parts[0]);
System.out.println("Course Name: " + parts[1]);
System.out.println("Description: " + parts[2]);
break;
}
}
reader.close();
if (!found) {
System.out.println("Error: Course with that ID does not exist.");
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
private static void displayStudent() {
try {
Scanner input = new Scanner(System.in);
System.out.println("Displaying student information...");
System.out.print("Enter student ID: ");
int id = input.nextInt();
Scanner reader = new Scanner(new File("student.txt"));
boolean found = false;
while (reader.hasNextLine()) {
String line = reader.nextLine();
String[] parts = line.split(",");
if (Integer.parseInt(parts[0]) == id) {
found = true;
System.out.println("Student ID: " + parts[0]);
System.out.println("Student Name: " + parts[1] + " " + parts[2]);
System.out.println("Address: " + parts[3] + ", " + parts[4] + ", " + parts[5] + " " + parts[6]);
break;
}
}
reader.close();
if (!found) {
System.out.println("Error: Student with that ID does not exist.");
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
