Question: Java coding Help Write a program to display the following menu until the user types 5 to exit. 1. Add student 2. Add Course 3.

Java coding Help

Write a program to display the following menu until the user types 5 to exit.

1. Add student

2. Add Course

3. Display Student

4. Display Course.

5. 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 student file, and display the record if found.

I need help with choices 3 and 4

What I have so far:

import java.io.*; import java.util.*; import java.io.FileWriter; import java.io.PrintWriter; import java.io.IOException;

public class Main { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); int choice = 0;

while (choice != 5) { 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 = scanner.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..."); break; } } }

private static void addStudent() throws IOException { Scanner scanner = new Scanner(System.in); int studentId = 0;

System.out.print("Please enter your student ID:"); studentId = scanner.nextInt();

if (checkExistingStudent(studentId)) { System.out.println("Student with ID " + studentId + " already exists."); return; }

System.out.println(" "); String zero = scanner.nextLine();

System.out.print("Please enter your first name: "); String firstName = scanner.nextLine();

System.out.print("Please enter your last name: "); String lastName = scanner.nextLine();

System.out.print("Please enter your address: "); String address = scanner.nextLine();

System.out.print("Please enter your city: "); String city = scanner.nextLine();

System.out.print("Please enter your state: "); String state = scanner.nextLine();

System.out.print("Please enter your zipcode: "); String zipcode = scanner.nextLine();

PrintWriter outputFile = new PrintWriter("students.txt");

outputFile.println(studentId); outputFile.println(firstName); outputFile.println(lastName); outputFile.println(address); outputFile.println(city); outputFile.println(state); outputFile.println(zipcode);

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

}

private static boolean checkExistingStudent(int studentId) { try { File file = new File("students.txt"); if (!file.exists()) { return false; } Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); String[] parts = line.split(","); int id = Integer.parseInt(parts[0]); if (id == studentId) { return true; } }

scanner.close(); return false; } catch (IOException e) { System.out.println("Error occurred while checking for existing student: " + e.getMessage()); return false; } }

private static void addCourse() throws IOException { Scanner scanner = new Scanner(System.in);

System.out.print("Please enter the course ID:"); int courseID = scanner.nextInt();

if (checkExistingCourse(courseID)) { System.out.println("Course with ID " + courseID + " already exists."); return; }

System.out.print("Please enter the course name:"); String courseName = scanner.next();

System.out.print("Please enter the course description:"); String courseDescription = scanner.next();

FileWriter fw = new FileWriter("courses.txt", true); PrintWriter outputFile = new PrintWriter(fw);

outputFile.println(courseID); outputFile.println(courseName); outputFile.println(courseDescription);

outputFile.close(); System.out.println("courses added successfully."); }

private static boolean checkExistingCourse(int courseID) { try { File file = new File("courses.txt"); if (!file.exists()) { return false; } Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); String[] parts = line.split(","); int id = Integer.parseInt(parts[0]); if (id == courseID) { return true; } }

scanner.close(); return false; } catch (IOException e) { System.out.println("Error occurred while checking for existing course: " + e.getMessage()); return false; } }

private static void displayStudent() { }

private static void displayCourse() {

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!