From 6ff78829acc70a51fdcd867528801034c81f4e31 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 22:48:18 +0100 Subject: [PATCH 1/7] Creating empty CourseTest file --- src/test/java/org/example/CourseTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/test/java/org/example/CourseTest.java diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java new file mode 100644 index 0000000..1c404db --- /dev/null +++ b/src/test/java/org/example/CourseTest.java @@ -0,0 +1,9 @@ +package org.example; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class CourseTest { + +} \ No newline at end of file From 96c6a8da43429c9e864f1acb59a1b841a37fa88c Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 22:58:10 +0100 Subject: [PATCH 2/7] test: printCourseInfo method in Course class --- src/test/java/org/example/CourseTest.java | 37 ++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 1c404db..5445a3d 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -3,7 +3,42 @@ package org.example; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import static org.junit.Assert.assertEquals; 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 expectedCourseCredits = "Course Credits: 3"; + + // Redirect System.out to capture printed output + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + PrintStream originalOut = System.out; + System.setOut(new PrintStream(outputStream)); + + // Call the method to be tested + course.printCourseInfo(); + + // Reset System.out + System.setOut(originalOut); + + // Get the printed output + String printedOutput = outputStream.toString().trim(); // trim to remove trailing newline + + // Split the printed output into lines + String[] outputLines = printedOutput.split("\\r?\\n"); + + // Assert that each line matches the expected output + assertEquals(expectedCourseName, outputLines[0].trim()); + assertEquals(expectedCourseCode, outputLines[1].trim()); + assertEquals(expectedCourseCredits, outputLines[2].trim()); + } } \ No newline at end of file From 2e1ade76ad3bf75d8841dc8f5d207c3f1f4fdab0 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 22:59:45 +0100 Subject: [PATCH 3/7] Creating empty courseData.txt file --- courseData.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 courseData.txt diff --git a/courseData.txt b/courseData.txt new file mode 100644 index 0000000..e69de29 From 46c2dc1b9eab4cc0e3c87004aa49c46ee606a247 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 23:02:21 +0100 Subject: [PATCH 4/7] Adding writeToFile method in Course class --- src/main/java/org/example/Course.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/org/example/Course.java b/src/main/java/org/example/Course.java index e32b098..b294177 100644 --- a/src/main/java/org/example/Course.java +++ b/src/main/java/org/example/Course.java @@ -1,4 +1,7 @@ package org.example; +import java.io.*; + + /** * The Course class represents a course in a curriculum. @@ -96,4 +99,16 @@ public class Course { System.out.println("Course Code: " + courseCode); System.out.println("Course Credits: " + courseCredit); } + + public void writeToFile(String filename) { + try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) { + writer.println(courseName); + writer.println(courseCode); + writer.println(courseCredit); + // Add any other fields you want to save + } catch (IOException e) { + e.printStackTrace(); + } + } + } From 4932036ebd853b9b075f974315bc4fecc028872b Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 23:10:47 +0100 Subject: [PATCH 5/7] Adding readFromFile method in Course class --- src/main/java/org/example/Course.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/java/org/example/Course.java b/src/main/java/org/example/Course.java index b294177..74a5f38 100644 --- a/src/main/java/org/example/Course.java +++ b/src/main/java/org/example/Course.java @@ -1,5 +1,7 @@ package org.example; import java.io.*; +import java.util.List; +import java.util.ArrayList; @@ -111,4 +113,25 @@ public class Course { } } + public static List readFromFile(String filename) { + List courses = new ArrayList<>(); + + try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { + String line; + while ((line = reader.readLine()) != null) { + String courseName = line; + String courseCode = reader.readLine(); + int credits = Integer.parseInt(reader.readLine()); + // Read any other fields you saved + Course course = new Course(courseName, courseCode, credits); + courses.add(course); + } + } catch (IOException e) { + e.printStackTrace(); + } + + return courses; + } + + } From 9a9e35153ba435d8d72668333c62122046462aac Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 23:14:25 +0100 Subject: [PATCH 6/7] test: writeToFile method in Course class --- src/test/java/org/example/CourseTest.java | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 5445a3d..0a7d241 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -6,6 +6,9 @@ import static org.junit.jupiter.api.Assertions.*; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import static org.junit.Assert.assertEquals; +import java.io.*; +import java.util.List; + class CourseTest { @@ -41,4 +44,32 @@ class CourseTest { assertEquals(expectedCourseCode, outputLines[1].trim()); assertEquals(expectedCourseCredits, outputLines[2].trim()); } + + @Test + void writeToFile() { + // Create a temporary file for testing + String filename = "test_course_data.txt"; + + // Create a new course + Course course = new Course("Math", "C101", 3); + + // Write course data to the temporary file + course.writeToFile(filename); + + // Read course data from the temporary file + List courses = Course.readFromFile(filename); + + // Assert that the read data matches the original course + assertEquals(1, courses.size()); + Course readCourse = courses.get(0); + assertEquals("Math", readCourse.getCourseName()); + assertEquals(3, readCourse.getCourseCredit()); + + // Delete the temporary file + File file = new File(filename); + if (file.exists()) { + file.delete(); + } + + } } \ No newline at end of file From 2a2caa891e856ac4a1cf2372908ac38e6d6889c1 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 23:19:26 +0100 Subject: [PATCH 7/7] test: readFromFile method in Course class --- src/test/java/org/example/CourseTest.java | 39 +++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 0a7d241..4c8a05d 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -1,7 +1,5 @@ package org.example; - import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.*; import java.io.ByteArrayOutputStream; import java.io.PrintStream; @@ -70,6 +68,43 @@ class CourseTest { if (file.exists()) { file.delete(); } + } + + @Test + void readFromFile() { + // Create a temporary file with sample course data + String filename = "test_course_data.txt"; + try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) { + writer.println("Math"); + writer.println("C101"); + writer.println(3); + writer.println("Science"); + writer.println("C201"); + writer.println(4); + } catch (IOException e) { + e.printStackTrace(); + } + + // Read course data from the temporary file + List courses = Course.readFromFile(filename); + + // Assert that the read data matches the original course data + assertEquals(2, courses.size()); + + Course course1 = courses.get(0); + assertEquals("Math", course1.getCourseName()); + assertEquals("C101", course1.getCourseCode()); + assertEquals(3, course1.getCourseCredit()); + Course course2 = courses.get(1); + assertEquals("Science", course2.getCourseName()); + assertEquals("C201", course2.getCourseCode()); + assertEquals(4, course2.getCourseCredit()); + + // Delete the temporary file + File file = new File(filename); + if (file.exists()) { + file.delete(); + } } } \ No newline at end of file