Browse Source

Finalizing documentation in Course class and Test file

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

26
src/main/java/org/example/Course.java

@ -147,12 +147,28 @@ public class Course {
return courses;
}
/**
* Adds a new course to the file with provided details.
*
* @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 static void addCourse(String courseName, String courseCode, int courseCredit, String filename) {
// Write course data to the file
Course course = new Course();
course.writeToFile(courseName, courseCode, courseCredit, filename);
}
/**
* Checks if a course with the given name or code already exists in the file.
*
* @param filename The name of the file to read from.
* @param name The name of the course.
* @param code The code of the course.
* @return True if a course with the same name or code exists, otherwise False.
*/
private static boolean courseExists(String filename, String name, String code) {
List<Course> courses = readFromFile(filename);
for (Course course : courses) {
@ -163,6 +179,11 @@ public class Course {
return false; // No course with the same name or code found
}
/**
* Adds a new course to the file based on user input.
*
* @param filename The name of the file to write to.
*/
public static void addCourseFromUserInput(String filename) {
Scanner scanner = new Scanner(System.in);
@ -185,6 +206,11 @@ public class Course {
System.out.println("Added Course successfully!");
}
/**
* Deletes a course from the file based on user input.
*
* @param filename The name of the file to write to.
*/
public static void deleteCourse(String filename) {
List<Course> courses = readFromFile(filename);

8
src/test/java/org/example/CourseTest.java

@ -108,6 +108,10 @@ class CourseTest {
deleteTempFile();
}
/**
* Tests the addCourse method of the Course class.
* Verifies that a new course can be added to the file.
*/
@Test
void addCourse() {
// Create a new course
@ -132,6 +136,10 @@ class CourseTest {
deleteTempFile();
}
/**
* Tests the deleteCourse method of the Course class.
* Verifies that a course can be deleted from the file.
*/
@Test
public void testDeleteCourse() throws IOException {
// Prepare test data

Loading…
Cancel
Save