From a6dad8916d47fae331f6f0cd1f2196bdf9f74357 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 13:16:57 +0100 Subject: [PATCH] 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);