Browse Source

Improving writeToFile method in Course class

remotes/origin/fdai7780
fdai7921 11 months ago
parent
commit
a6dad8916d
  1. 10
      src/main/java/org/example/Course.java
  2. 2
      src/test/java/org/example/CourseTest.java

10
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();
}

2
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<Course> courses = Course.readFromFile(TEMP_FILE);

Loading…
Cancel
Save