From a6dad8916d47fae331f6f0cd1f2196bdf9f74357 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 13:16:57 +0100 Subject: [PATCH 01/10] Improving writeToFile method in Course class --- src/main/java/org/example/Course.java | 10 ++++++---- src/test/java/org/example/CourseTest.java | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/example/Course.java b/src/main/java/org/example/Course.java index 7715ca3..ff8a721 100644 --- a/src/main/java/org/example/Course.java +++ b/src/main/java/org/example/Course.java @@ -105,14 +105,16 @@ public class Course { /** * 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(courseCode); writer.println(courseCredit); - // Add any other fields you want to save } catch (IOException e) { e.printStackTrace(); } diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index b7c7620..01bd09d 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -62,7 +62,7 @@ class CourseTest { Course course = new Course("Math", "C101", 3); // Write course data to the temporary file - course.writeToFile(TEMP_FILE); + course.writeToFile(course.getCourseName(), course.getCourseCode(), course.getCourseCredit(), TEMP_FILE); // Read course data from the temporary file List courses = Course.readFromFile(TEMP_FILE); From 2542ae7b9dadcc0e02d9714aafa572f540c4e234 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 13:21:26 +0100 Subject: [PATCH 02/10] Adding addCourse method in Course class --- src/main/java/org/example/Course.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/example/Course.java b/src/main/java/org/example/Course.java index ff8a721..9092951 100644 --- a/src/main/java/org/example/Course.java +++ b/src/main/java/org/example/Course.java @@ -146,5 +146,9 @@ public class Course { return courses; } - + 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); + } } From 91cd4439af8008ab9bd5d53f41ad35dc2e865d5a Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 13:24:00 +0100 Subject: [PATCH 03/10] test: addCourse method in Course class --- src/test/java/org/example/CourseTest.java | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 01bd09d..5438509 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -107,6 +107,31 @@ class CourseTest { deleteTempFile(); } + @Test + void addCourse() { + // Create a new course + String courseName = "Math"; + String courseCode = "C101"; + int courseCredit = 3; + + // Write course data to the temporary file + Course.addCourse(courseName, courseCode, courseCredit, TEMP_FILE); + + // Read course data from the temporary file + List courses = Course.readFromFile(TEMP_FILE); + + // Assert that the course is added successfully + assertEquals(1, courses.size()); + Course addedCourse = courses.get(0); + assertEquals(courseName, addedCourse.getCourseName()); + assertEquals(courseCode, addedCourse.getCourseCode()); + assertEquals(courseCredit, addedCourse.getCourseCredit()); + + // Delete the temporary file + deleteTempFile(); + } + + /** * Creates a temporary file with the provided data. * From ddeaa689cb60cc4c83e4642f626b226db52b2cd5 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 13:25:11 +0100 Subject: [PATCH 04/10] Adding courseExists method in Course class --- src/main/java/org/example/Course.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/org/example/Course.java b/src/main/java/org/example/Course.java index 9092951..6d0ae4e 100644 --- a/src/main/java/org/example/Course.java +++ b/src/main/java/org/example/Course.java @@ -151,4 +151,14 @@ public class Course { Course course = new Course(); course.writeToFile(courseName, courseCode, courseCredit, filename); } + + private static boolean courseExists(String filename, String name, String code) { + List 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 + } } From 46b6240589d9f5a3508ee3602de1681dbbaadcf8 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 13:26:36 +0100 Subject: [PATCH 05/10] Adding addCourseFromUserInput method in Course class --- src/main/java/org/example/Course.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/org/example/Course.java b/src/main/java/org/example/Course.java index 6d0ae4e..cc95d97 100644 --- a/src/main/java/org/example/Course.java +++ b/src/main/java/org/example/Course.java @@ -2,6 +2,7 @@ package org.example; import java.io.*; import java.util.List; import java.util.ArrayList; +import java.util.Scanner; @@ -161,4 +162,27 @@ public class Course { } return false; // No course with the same name or code found } + + 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!"); + } + } From 28b4d42f9c56824aa73f3b6dbaa1189785981084 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 13:27:54 +0100 Subject: [PATCH 06/10] Adding deleteCourse method in Course class --- src/main/java/org/example/Course.java | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/main/java/org/example/Course.java b/src/main/java/org/example/Course.java index cc95d97..d990da4 100644 --- a/src/main/java/org/example/Course.java +++ b/src/main/java/org/example/Course.java @@ -185,4 +185,49 @@ public class Course { System.out.println("Added Course successfully!"); } + public static void deleteCourse(String filename) { + List 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; + } + } + } } From ee86cf4325b42c0ed7c9b56d9f8e1dd253346b2d Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 13:30:01 +0100 Subject: [PATCH 07/10] test: deleteCourse method in Course class --- src/test/java/org/example/CourseTest.java | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 5438509..fa987a0 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -6,6 +6,7 @@ import java.io.PrintStream; import static org.junit.Assert.assertEquals; import java.io.*; import java.util.List; +import static org.junit.jupiter.api.Assertions.*; /** * The CourseTest class contains unit tests for the Course class. @@ -131,6 +132,45 @@ class CourseTest { deleteTempFile(); } + @Test + public void testDeleteCourse() throws IOException { + // Prepare test data + Course course1 = new Course("Math", "M101", 3); + Course course2 = new Course("Science", "S101", 4); + Course course3 = new Course("Java", "J101", 4); + + // Write courses to the test file + PrintWriter writer = new PrintWriter(new FileWriter(TEMP_FILE)); + writer.println(course1.getCourseName()); + writer.println(course1.getCourseCode()); + writer.println(course1.getCourseCredit()); + writer.println(course2.getCourseName()); + writer.println(course2.getCourseCode()); + writer.println(course2.getCourseCredit()); + writer.println(course3.getCourseName()); + writer.println(course3.getCourseCode()); + writer.println(course3.getCourseCredit()); + writer.close(); + + // Simulate user input for course deletion + ByteArrayInputStream inputStream = new ByteArrayInputStream("1\n0".getBytes()); + System.setIn(inputStream); + + // Delete a course + Course.deleteCourse(TEMP_FILE); + + // Read courses after deletion + List coursesAfterDeletion = Course.readFromFile(TEMP_FILE); + + // Ensure the correct number of courses remaining after deletion + assertEquals(2, coursesAfterDeletion.size()); + + // Check if the correct course was deleted + assertFalse(coursesAfterDeletion.contains(course1)); + + deleteTempFile(); + } + /** * Creates a temporary file with the provided data. From f0a47e1c1adab21663f3b4c9f0b3c07be8701fdb Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 14:10:30 +0100 Subject: [PATCH 08/10] Finalizing documentation in Course class and Test file --- src/main/java/org/example/Course.java | 26 +++++++++++++++++++++++ src/test/java/org/example/CourseTest.java | 8 +++++++ 2 files changed, 34 insertions(+) diff --git a/src/main/java/org/example/Course.java b/src/main/java/org/example/Course.java index d990da4..10cb36b 100644 --- a/src/main/java/org/example/Course.java +++ b/src/main/java/org/example/Course.java @@ -147,12 +147,28 @@ public class Course { 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 courses = readFromFile(filename); for (Course course : courses) { @@ -163,6 +179,11 @@ public class Course { 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); @@ -185,6 +206,11 @@ public class Course { 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 courses = readFromFile(filename); diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index fa987a0..ec0f7c9 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -108,6 +108,10 @@ class CourseTest { deleteTempFile(); } + /** + * Tests the addCourse method of the Course class. + * Verifies that a new course can be added to the file. + */ @Test void addCourse() { // Create a new course @@ -132,6 +136,10 @@ class CourseTest { deleteTempFile(); } + /** + * Tests the deleteCourse method of the Course class. + * Verifies that a course can be deleted from the file. + */ @Test public void testDeleteCourse() throws IOException { // Prepare test data From a9e13a28bf5133654c04c19a33821c21b4fbe862 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 14:12:09 +0100 Subject: [PATCH 09/10] Implementing Add Course function in main --- src/main/java/org/example/Main.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 3ba5d3a..0ea664a 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -31,6 +31,7 @@ public class Main { break; case 3: System.out.println("Add Course selected"); + Course.addCourseFromUserInput("courseData.txt"); break; case 4: System.out.println("Delete Course selected"); From 3e83c6ec3c237e537e735212c5583d2cfa7a693b Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 14:13:11 +0100 Subject: [PATCH 10/10] Implementing Delete course method in main --- src/main/java/org/example/Main.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 0ea664a..1a502ae 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -35,6 +35,7 @@ public class Main { break; case 4: System.out.println("Delete Course selected"); + Course.deleteCourse("courseData.txt"); break; case 5: System.out.println("Assign Grade selected");