Browse Source

Finalizing documentation in Course class and Test file

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

11
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<Course> readFromFile(String filename) {
List<Course> courses = new ArrayList<>();

26
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()) {

Loading…
Cancel
Save