diff --git a/src/main/java/org/example/Course.java b/src/main/java/org/example/Course.java index 74a5f38..7715ca3 100644 --- a/src/main/java/org/example/Course.java +++ b/src/main/java/org/example/Course.java @@ -102,6 +102,11 @@ public class Course { System.out.println("Course Credits: " + courseCredit); } + /** + * Writes the information of the course to a file. + * + * @param filename The name of the file to write to. + */ public void writeToFile(String filename) { try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) { writer.println(courseName); @@ -113,6 +118,12 @@ public class Course { } } + /** + * Reads course information from a file and returns a list of courses. + * + * @param filename The name of the file to read from. + * @return A list of Course objects read from the file. + */ public static List readFromFile(String filename) { List courses = new ArrayList<>(); diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 0050875..b7c7620 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -7,10 +7,18 @@ import static org.junit.Assert.assertEquals; import java.io.*; import java.util.List; - +/** + * The CourseTest class contains unit tests for the Course class. + * It tests methods for printing course information, writing course data to a file, + * and reading course data from a file. + */ class CourseTest { private static final String TEMP_FILE = "test_course_data.txt"; + /** + * Tests the printCourseInfo method of the Course class. + * Verifies that the method correctly prints course information to the console. + */ @Test void printCourseInfo() { // Create a new Course object @@ -44,6 +52,10 @@ class CourseTest { assertEquals(expectedCourseCredits, outputLines[2].trim()); } + /** + * Tests the writeToFile method of the Course class. + * Verifies that course data is correctly written to a file and can be read back. + */ @Test void writeToFile() { // Create a new course @@ -66,6 +78,10 @@ class CourseTest { deleteTempFile(); } + /** + * Tests the readFromFile method of the Course class. + * Verifies that course data can be correctly read from a file. + */ @Test void readFromFile() { // Create a temporary file with sample course data @@ -91,6 +107,11 @@ class CourseTest { deleteTempFile(); } + /** + * Creates a temporary file with the provided data. + * + * @param data The data to write to the file. + */ private void createTempFile(String... data) { try (PrintWriter writer = new PrintWriter(new FileWriter(TEMP_FILE))) { for (String line : data) { @@ -101,6 +122,9 @@ class CourseTest { } } + /** + * Deletes the temporary file used for testing. + */ private void deleteTempFile() { File file = new File(TEMP_FILE); if (file.exists()) {