|
@ -2,6 +2,7 @@ package org.example; |
|
|
import java.io.*; |
|
|
import java.io.*; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.ArrayList; |
|
|
import java.util.ArrayList; |
|
|
|
|
|
import java.util.Scanner; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -105,14 +106,16 @@ public class Course { |
|
|
/** |
|
|
/** |
|
|
* Writes the information of the course to a file. |
|
|
* Writes the information of the course to a file. |
|
|
* |
|
|
* |
|
|
* @param filename The name of the file to write to. |
|
|
|
|
|
|
|
|
* @param courseName The name of the course. |
|
|
|
|
|
* @param courseCode The code of the course. |
|
|
|
|
|
* @param courseCredit The credit value of the course. |
|
|
|
|
|
* @param filename The name of the file to write to. |
|
|
*/ |
|
|
*/ |
|
|
public void writeToFile(String filename) { |
|
|
|
|
|
try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) { |
|
|
|
|
|
|
|
|
public void writeToFile(String courseName, String courseCode, int courseCredit, String filename) { |
|
|
|
|
|
try (PrintWriter writer = new PrintWriter(new FileWriter(filename, true))) { |
|
|
writer.println(courseName); |
|
|
writer.println(courseName); |
|
|
writer.println(courseCode); |
|
|
writer.println(courseCode); |
|
|
writer.println(courseCredit); |
|
|
writer.println(courseCredit); |
|
|
// Add any other fields you want to save |
|
|
|
|
|
} catch (IOException e) { |
|
|
} catch (IOException e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
} |
|
|
} |
|
@ -144,5 +147,113 @@ public class Course { |
|
|
return courses; |
|
|
return courses; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Adds a new course to the file with provided details. |
|
|
|
|
|
* |
|
|
|
|
|
* @param courseName The name of the course. |
|
|
|
|
|
* @param courseCode The code of the course. |
|
|
|
|
|
* @param courseCredit The credit value of the course. |
|
|
|
|
|
* @param filename The name of the file to write to. |
|
|
|
|
|
*/ |
|
|
|
|
|
public static void addCourse(String courseName, String courseCode, int courseCredit, String filename) { |
|
|
|
|
|
// Write course data to the file |
|
|
|
|
|
Course course = new Course(); |
|
|
|
|
|
course.writeToFile(courseName, courseCode, courseCredit, filename); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Checks if a course with the given name or code already exists in the file. |
|
|
|
|
|
* |
|
|
|
|
|
* @param filename The name of the file to read from. |
|
|
|
|
|
* @param name The name of the course. |
|
|
|
|
|
* @param code The code of the course. |
|
|
|
|
|
* @return True if a course with the same name or code exists, otherwise False. |
|
|
|
|
|
*/ |
|
|
|
|
|
private static boolean courseExists(String filename, String name, String code) { |
|
|
|
|
|
List<Course> courses = readFromFile(filename); |
|
|
|
|
|
for (Course course : courses) { |
|
|
|
|
|
if (course.getCourseName().equalsIgnoreCase(name) || course.getCourseCode().equalsIgnoreCase(code)) { |
|
|
|
|
|
return true; // Course with the same name or code already exists |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return false; // No course with the same name or code found |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Adds a new course to the file based on user input. |
|
|
|
|
|
* |
|
|
|
|
|
* @param filename The name of the file to write to. |
|
|
|
|
|
*/ |
|
|
|
|
|
public static void addCourseFromUserInput(String filename) { |
|
|
|
|
|
Scanner scanner = new Scanner(System.in); |
|
|
|
|
|
|
|
|
|
|
|
System.out.print("Enter the name of the course: "); |
|
|
|
|
|
String courseName = scanner.nextLine(); |
|
|
|
|
|
System.out.print("Enter the code of the course: "); |
|
|
|
|
|
String courseCode = scanner.nextLine(); |
|
|
|
|
|
|
|
|
|
|
|
// Check if the course already exists |
|
|
|
|
|
if (courseExists(filename, courseName, courseCode)) { |
|
|
|
|
|
System.out.println("Course with the same name or code already exists."); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
System.out.print("Enter the credit value of the course: "); |
|
|
|
|
|
int courseCredit = scanner.nextInt(); |
|
|
|
|
|
|
|
|
|
|
|
// Add the course using the provided data |
|
|
|
|
|
addCourse(courseName, courseCode, courseCredit, filename); |
|
|
|
|
|
|
|
|
|
|
|
System.out.println("Added Course successfully!"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Deletes a course from the file based on user input. |
|
|
|
|
|
* |
|
|
|
|
|
* @param filename The name of the file to write to. |
|
|
|
|
|
*/ |
|
|
|
|
|
public static void deleteCourse(String filename) { |
|
|
|
|
|
List<Course> courses = readFromFile(filename); |
|
|
|
|
|
|
|
|
|
|
|
if (courses.isEmpty()) { |
|
|
|
|
|
System.out.println("No courses found."); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Scanner scanner = new Scanner(System.in); |
|
|
|
|
|
|
|
|
|
|
|
while (true) { |
|
|
|
|
|
System.out.println("Select the course to delete (or 0 to go back to the menu):"); |
|
|
|
|
|
|
|
|
|
|
|
// Display available courses for selection |
|
|
|
|
|
for (int i = 0; i < courses.size(); i++) { |
|
|
|
|
|
System.out.println((i + 1) + ". " + courses.get(i).getCourseName()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int selection = scanner.nextInt(); |
|
|
|
|
|
|
|
|
|
|
|
if (selection == 0) { |
|
|
|
|
|
return; // Return to the menu |
|
|
|
|
|
} else if (selection < 1 || selection > courses.size()) { |
|
|
|
|
|
System.out.println("Invalid selection."); |
|
|
|
|
|
} else { |
|
|
|
|
|
Course courseToDelete = courses.get(selection - 1); |
|
|
|
|
|
courses.remove(courseToDelete); |
|
|
|
|
|
|
|
|
|
|
|
try (FileWriter writer = new FileWriter(filename)) { |
|
|
|
|
|
for (Course course : courses) { |
|
|
|
|
|
writer.write(course.getCourseName() + "\n"); |
|
|
|
|
|
writer.write(course.getCourseCode() + "\n"); |
|
|
|
|
|
writer.write(course.getCourseCredit() + "\n"); |
|
|
|
|
|
// Write any other fields you saved |
|
|
|
|
|
} |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
System.out.println("An error occurred while deleting the course."); |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
System.out.println("Course deleted successfully."); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |