Browse Source

Merge remote-tracking branch 'origin/master'

remotes/origin/fdai7780
fdai7600 11 months ago
parent
commit
eea1faa652
  1. 6
      src/main/java/org/example/Course.java
  2. 55
      src/test/java/org/example/CourseTest.java

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

@ -203,7 +203,7 @@ public class Course {
// Add the course using the provided data // Add the course using the provided data
addCourse(courseName, courseCode, courseCredit, filename); addCourse(courseName, courseCode, courseCredit, filename);
System.out.println("Added Course successfully!");
System.out.println(courseName + " added successfully!");
} }
/** /**
@ -231,6 +231,8 @@ public class Course {
int selection = scanner.nextInt(); int selection = scanner.nextInt();
String toDeleteCourse = courses.get(selection-1).getCourseName();
if (selection == 0) { if (selection == 0) {
return; // Return to the menu return; // Return to the menu
} else if (selection < 1 || selection > courses.size()) { } else if (selection < 1 || selection > courses.size()) {
@ -251,7 +253,7 @@ public class Course {
e.printStackTrace(); e.printStackTrace();
} }
System.out.println("Course deleted successfully.");
System.out.println(toDeleteCourse + " deleted successfully.");
return; return;
} }
} }

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

@ -15,6 +15,9 @@ import static org.junit.jupiter.api.Assertions.*;
*/ */
class CourseTest { class CourseTest {
private static final String TEMP_FILE = "test_course_data.txt"; private static final String TEMP_FILE = "test_course_data.txt";
private static final Course sampleCourse = new Course("Math", "M101", 3);
private static final Course sampleCourse2 = new Course("Science", "S101", 4);
private static final Course sampleCourse3 = new Course("Java", "J101", 3);
/** /**
* Tests the printCourseInfo method of the Course class. * Tests the printCourseInfo method of the Course class.
@ -22,12 +25,9 @@ class CourseTest {
*/ */
@Test @Test
void printCourseInfo() { void printCourseInfo() {
// Create a new Course object
Course course = new Course("Introduction to Programming", "CS101", 3);
// Set up the expected output // Set up the expected output
String expectedCourseName = "Course Name: Introduction to Programming";
String expectedCourseCode = "Course Code: CS101";
String expectedCourseName = "Course Name: Math";
String expectedCourseCode = "Course Code: M101";
String expectedCourseCredits = "Course Credits: 3"; String expectedCourseCredits = "Course Credits: 3";
// Redirect System.out to capture printed output // Redirect System.out to capture printed output
@ -36,7 +36,7 @@ class CourseTest {
System.setOut(new PrintStream(outputStream)); System.setOut(new PrintStream(outputStream));
// Call the method to be tested // Call the method to be tested
course.printCourseInfo();
sampleCourse.printCourseInfo();
// Reset System.out // Reset System.out
System.setOut(originalOut); System.setOut(originalOut);
@ -60,7 +60,7 @@ class CourseTest {
@Test @Test
void writeToFile() { void writeToFile() {
// Create a new course // Create a new course
Course course = new Course("Math", "C101", 3);
Course course = sampleCourse;
// Write course data to the temporary file // Write course data to the temporary file
course.writeToFile(course.getCourseName(), course.getCourseCode(), course.getCourseCredit(), TEMP_FILE); course.writeToFile(course.getCourseName(), course.getCourseCode(), course.getCourseCredit(), TEMP_FILE);
@ -72,7 +72,7 @@ class CourseTest {
assertEquals(1, courses.size()); assertEquals(1, courses.size());
Course readCourse = courses.get(0); Course readCourse = courses.get(0);
assertEquals("Math", readCourse.getCourseName()); assertEquals("Math", readCourse.getCourseName());
assertEquals("C101", readCourse.getCourseCode());
assertEquals("M101", readCourse.getCourseCode());
assertEquals(3, readCourse.getCourseCredit()); assertEquals(3, readCourse.getCourseCredit());
// Delete the temporary file // Delete the temporary file
@ -108,16 +108,12 @@ class CourseTest {
deleteTempFile(); deleteTempFile();
} }
/**
* Tests the addCourse method of the Course class.
* Verifies that a new course can be added to the file.
*/
@Test @Test
void addCourse() { void addCourse() {
// Create a new course // Create a new course
String courseName = "Math";
String courseCode = "C101";
int courseCredit = 3;
String courseName = sampleCourse.getCourseName();
String courseCode = sampleCourse.getCourseCode();
int courseCredit = sampleCourse.getCourseCredit();
// Write course data to the temporary file // Write course data to the temporary file
Course.addCourse(courseName, courseCode, courseCredit, TEMP_FILE); Course.addCourse(courseName, courseCode, courseCredit, TEMP_FILE);
@ -136,28 +132,19 @@ class CourseTest {
deleteTempFile(); deleteTempFile();
} }
/**
* Tests the deleteCourse method of the Course class.
* Verifies that a course can be deleted from the file.
*/
@Test @Test
public void testDeleteCourse() throws IOException { public void testDeleteCourse() throws IOException {
// Prepare test data
Course course1 = new Course("Math", "M101", 3);
Course course2 = new Course("Science", "S101", 4);
Course course3 = new Course("Java", "J101", 4);
// Write courses to the test file // Write courses to the test file
PrintWriter writer = new PrintWriter(new FileWriter(TEMP_FILE)); PrintWriter writer = new PrintWriter(new FileWriter(TEMP_FILE));
writer.println(course1.getCourseName());
writer.println(course1.getCourseCode());
writer.println(course1.getCourseCredit());
writer.println(course2.getCourseName());
writer.println(course2.getCourseCode());
writer.println(course2.getCourseCredit());
writer.println(course3.getCourseName());
writer.println(course3.getCourseCode());
writer.println(course3.getCourseCredit());
writer.println(sampleCourse.getCourseName());
writer.println(sampleCourse.getCourseCode());
writer.println(sampleCourse.getCourseCredit());
writer.println(sampleCourse2.getCourseName());
writer.println(sampleCourse2.getCourseCode());
writer.println(sampleCourse2.getCourseCredit());
writer.println(sampleCourse3.getCourseName());
writer.println(sampleCourse3.getCourseCode());
writer.println(sampleCourse3.getCourseCredit());
writer.close(); writer.close();
// Simulate user input for course deletion // Simulate user input for course deletion
@ -174,7 +161,7 @@ class CourseTest {
assertEquals(2, coursesAfterDeletion.size()); assertEquals(2, coursesAfterDeletion.size());
// Check if the correct course was deleted // Check if the correct course was deleted
assertFalse(coursesAfterDeletion.contains(course1));
assertFalse(coursesAfterDeletion.contains(sampleCourse));
deleteTempFile(); deleteTempFile();
} }

Loading…
Cancel
Save