From 0377c71c525318bb01d4d9d2cbd46a17bcaa2d64 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 14:38:18 +0100 Subject: [PATCH 1/2] refactoring: more clarity for output messages of Course add and delete functions --- src/main/java/org/example/Course.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/example/Course.java b/src/main/java/org/example/Course.java index 10cb36b..1714e77 100644 --- a/src/main/java/org/example/Course.java +++ b/src/main/java/org/example/Course.java @@ -203,7 +203,7 @@ public class Course { // Add the course using the provided data addCourse(courseName, courseCode, courseCredit, filename); - System.out.println("Added Course successfully!"); + System.out.println(courseName + " added successfully!"); } /** @@ -231,6 +231,8 @@ public class Course { int selection = scanner.nextInt(); + String toDeleteCourse = courses.get(selection-1).getCourseName(); + if (selection == 0) { return; // Return to the menu } else if (selection < 1 || selection > courses.size()) { @@ -251,7 +253,7 @@ public class Course { e.printStackTrace(); } - System.out.println("Course deleted successfully."); + System.out.println(toDeleteCourse + " deleted successfully."); return; } } From b756faecc279fe7b908ac83ca8010cd062c6d022 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 15:08:48 +0100 Subject: [PATCH 2/2] refactoring: extract sample courses in CourseTest file --- src/test/java/org/example/CourseTest.java | 55 +++++++++-------------- 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index ec0f7c9..704edaa 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -15,6 +15,9 @@ import static org.junit.jupiter.api.Assertions.*; */ class CourseTest { private static final String TEMP_FILE = "test_course_data.txt"; + private static final Course sampleCourse = new Course("Math", "M101", 3); + private static final Course sampleCourse2 = new Course("Science", "S101", 4); + private static final Course sampleCourse3 = new Course("Java", "J101", 3); /** * Tests the printCourseInfo method of the Course class. @@ -22,12 +25,9 @@ class CourseTest { */ @Test void printCourseInfo() { - // Create a new Course object - Course course = new Course("Introduction to Programming", "CS101", 3); - // Set up the expected output - String expectedCourseName = "Course Name: Introduction to Programming"; - String expectedCourseCode = "Course Code: CS101"; + String expectedCourseName = "Course Name: Math"; + String expectedCourseCode = "Course Code: M101"; String expectedCourseCredits = "Course Credits: 3"; // Redirect System.out to capture printed output @@ -36,7 +36,7 @@ class CourseTest { System.setOut(new PrintStream(outputStream)); // Call the method to be tested - course.printCourseInfo(); + sampleCourse.printCourseInfo(); // Reset System.out System.setOut(originalOut); @@ -60,7 +60,7 @@ class CourseTest { @Test void writeToFile() { // Create a new course - Course course = new Course("Math", "C101", 3); + Course course = sampleCourse; // Write course data to the temporary file course.writeToFile(course.getCourseName(), course.getCourseCode(), course.getCourseCredit(), TEMP_FILE); @@ -72,7 +72,7 @@ class CourseTest { assertEquals(1, courses.size()); Course readCourse = courses.get(0); assertEquals("Math", readCourse.getCourseName()); - assertEquals("C101", readCourse.getCourseCode()); + assertEquals("M101", readCourse.getCourseCode()); assertEquals(3, readCourse.getCourseCredit()); // Delete the temporary file @@ -108,16 +108,12 @@ 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 - String courseName = "Math"; - String courseCode = "C101"; - int courseCredit = 3; + String courseName = sampleCourse.getCourseName(); + String courseCode = sampleCourse.getCourseCode(); + int courseCredit = sampleCourse.getCourseCredit(); // Write course data to the temporary file Course.addCourse(courseName, courseCode, courseCredit, TEMP_FILE); @@ -136,28 +132,19 @@ 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 - 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.println(sampleCourse.getCourseName()); + writer.println(sampleCourse.getCourseCode()); + writer.println(sampleCourse.getCourseCredit()); + writer.println(sampleCourse2.getCourseName()); + writer.println(sampleCourse2.getCourseCode()); + writer.println(sampleCourse2.getCourseCredit()); + writer.println(sampleCourse3.getCourseName()); + writer.println(sampleCourse3.getCourseCode()); + writer.println(sampleCourse3.getCourseCredit()); writer.close(); // Simulate user input for course deletion @@ -174,7 +161,7 @@ class CourseTest { assertEquals(2, coursesAfterDeletion.size()); // Check if the correct course was deleted - assertFalse(coursesAfterDeletion.contains(course1)); + assertFalse(coursesAfterDeletion.contains(sampleCourse)); deleteTempFile(); }