From f94d4419b92f9b6aa8117b84582057f459b814c2 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 16:57:58 +0100 Subject: [PATCH 1/4] extracting sample student in StudentTest file --- src/test/java/org/example/StudentTest.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/example/StudentTest.java b/src/test/java/org/example/StudentTest.java index 256b017..2bac26a 100644 --- a/src/test/java/org/example/StudentTest.java +++ b/src/test/java/org/example/StudentTest.java @@ -10,9 +10,11 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.*; class StudentTest { + private static final Student sampleStudent = new Student("Sebastian Reichard", "S1001", "Student"); + @org.junit.jupiter.api.Test void writeToFile() throws IOException { - Student student = new Student("Sebastian", "S1001", "Student") ; + Student student = sampleStudent; String filename = "testStudentData.txt"; @@ -20,7 +22,7 @@ class StudentTest { Path path = Path.of(filename); String content = Files.readString(path); - assertEquals("Sebastian\nS1001\nStudent", content); + assertEquals("Sebastian Reichard\nS1001\nStudent", content); Files.delete(path); @@ -34,7 +36,7 @@ class StudentTest { void readFromFile() throws IOException { try (PrintWriter writer = new PrintWriter(new FileWriter("testStudentData.txt"))){ - writer.println("Samuel"); + writer.println("Sebastian Reichard"); writer.println("S1001"); writer.print("Student"); } catch (IOException e) { @@ -46,7 +48,7 @@ class StudentTest { assertEquals(1, testStudents.size()); Student admin = testStudents.get(0); - assertEquals("Samuel", admin.getName()); + assertEquals("Sebastian Reichard", admin.getName()); assertEquals("S1001", admin.getId()); assertEquals("Student", admin.getRole()); @@ -57,7 +59,7 @@ class StudentTest { @Test void printStudentInfo() { - Student student = new Student("Sebastian", "S1001", "Student"); + Student student = sampleStudent; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(outputStream)); @@ -66,7 +68,7 @@ class StudentTest { String printedOutput = outputStream.toString().trim(); - assertTrue(printedOutput.contains("Sebastian")); + assertTrue(printedOutput.contains("Sebastian Reichard")); assertTrue(printedOutput.contains("S1001")); assertTrue(printedOutput.contains("Student")); From 1ef262140bb5392c121455299366bbb4c58076e7 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 17:08:37 +0100 Subject: [PATCH 2/4] refactoring: adding String for 'courseData.txt' file --- src/main/java/org/example/Main.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index e6cf202..f796f26 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -2,6 +2,8 @@ package org.example; import java.util.Scanner; public class Main { + + public static final String courseData_file = "courseData.txt"; public static void main(String[] args) {System.out.println("Hello world!"); Administration administration = new Administration(); @@ -31,11 +33,11 @@ public class Main { break; case 3: System.out.println("Add Course selected"); - Course.addCourseFromUserInput("courseData.txt"); + Course.addCourseFromUserInput(courseData_file); break; case 4: System.out.println("Delete Course selected"); - Course.deleteCourse("courseData.txt"); + Course.deleteCourse(courseData_file); break; case 5: System.out.println("Assign Grade selected"); From 98f1a6aa844f63cb6f2945540247b225839320d2 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 17:19:04 +0100 Subject: [PATCH 3/4] refactoring: adjusting variable names of Course class --- src/main/java/org/example/Administration.java | 4 +- src/main/java/org/example/Course.java | 44 +++++++++---------- src/test/java/org/example/CourseTest.java | 34 +++++++------- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index f760a50..4b8a8db 100644 --- a/src/main/java/org/example/Administration.java +++ b/src/main/java/org/example/Administration.java @@ -43,7 +43,7 @@ public class Administration { */ public boolean deleteCourse(String courseID){ for(Course course : courses){ - if(course.getCourseCode().equals(courseID)){ + if(course.getCourseID().equals(courseID)){ courses.remove(course); return true; } @@ -59,7 +59,7 @@ public class Administration { */ public Course findCourseByID(String courseID){ for(Course course : courses){ - if(course.getCourseCode().equals(courseID)){ + if(course.getCourseID().equals(courseID)){ return course; } } diff --git a/src/main/java/org/example/Course.java b/src/main/java/org/example/Course.java index 1714e77..ae4f2fe 100644 --- a/src/main/java/org/example/Course.java +++ b/src/main/java/org/example/Course.java @@ -16,10 +16,10 @@ public class Course { private String courseName; /** The code of the course. */ - private String courseCode; + private String courseID; /** The credit value of the course. */ - private int courseCredit; + private int courseCredits; /** * Constructs a new Course object with default values. @@ -31,13 +31,13 @@ public class Course { * Constructs a new Course object with specified values. * * @param courseName The name of the course. - * @param courseCode The code of the course. - * @param courseCredit The credit value of the course. + * @param courseID The code of the course. + * @param courseCredits The credit value of the course. */ - public Course(String courseName, String courseCode, int courseCredit) { + public Course(String courseName, String courseID, int courseCredits) { this.courseName = courseName; - this.courseCode = courseCode; - this.courseCredit = courseCredit; + this.courseID = courseID; + this.courseCredits = courseCredits; } /** @@ -63,17 +63,17 @@ public class Course { * * @return The code of the course. */ - public String getCourseCode() { - return courseCode; + public String getCourseID() { + return courseID; } /** * Sets the code of the course. * - * @param courseCode The code of the course. + * @param courseID The code of the course. */ - public void setCourseCode(String courseCode) { - this.courseCode = courseCode; + public void setCourseID(String courseID) { + this.courseID = courseID; } /** @@ -81,17 +81,17 @@ public class Course { * * @return The credit value of the course. */ - public int getCourseCredit() { - return courseCredit; + public int getCourseCredits() { + return courseCredits; } /** * Sets the credit value of the course. * - * @param courseCredit The credit value of the course. + * @param courseCredits The credit value of the course. */ - public void setCourseCredit(int courseCredit) { - this.courseCredit = courseCredit; + public void setCourseCredits(int courseCredits) { + this.courseCredits = courseCredits; } /** @@ -99,8 +99,8 @@ public class Course { */ public void printCourseInfo() { System.out.println("Course Name: " + courseName); - System.out.println("Course Code: " + courseCode); - System.out.println("Course Credits: " + courseCredit); + System.out.println("Course Code: " + courseID); + System.out.println("Course Credits: " + courseCredits); } /** @@ -172,7 +172,7 @@ public class Course { private static boolean courseExists(String filename, String name, String code) { List courses = readFromFile(filename); for (Course course : courses) { - if (course.getCourseName().equalsIgnoreCase(name) || course.getCourseCode().equalsIgnoreCase(code)) { + if (course.getCourseName().equalsIgnoreCase(name) || course.getCourseID().equalsIgnoreCase(code)) { return true; // Course with the same name or code already exists } } @@ -244,8 +244,8 @@ public class Course { try (FileWriter writer = new FileWriter(filename)) { for (Course course : courses) { writer.write(course.getCourseName() + "\n"); - writer.write(course.getCourseCode() + "\n"); - writer.write(course.getCourseCredit() + "\n"); + writer.write(course.getCourseID() + "\n"); + writer.write(course.getCourseCredits() + "\n"); // Write any other fields you saved } } catch (IOException e) { diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 704edaa..33510b6 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -63,7 +63,7 @@ class CourseTest { Course course = sampleCourse; // Write course data to the temporary file - course.writeToFile(course.getCourseName(), course.getCourseCode(), course.getCourseCredit(), TEMP_FILE); + course.writeToFile(course.getCourseName(), course.getCourseID(), course.getCourseCredits(), TEMP_FILE); // Read course data from the temporary file List courses = Course.readFromFile(TEMP_FILE); @@ -72,8 +72,8 @@ class CourseTest { assertEquals(1, courses.size()); Course readCourse = courses.get(0); assertEquals("Math", readCourse.getCourseName()); - assertEquals("M101", readCourse.getCourseCode()); - assertEquals(3, readCourse.getCourseCredit()); + assertEquals("M101", readCourse.getCourseID()); + assertEquals(3, readCourse.getCourseCredits()); // Delete the temporary file deleteTempFile(); @@ -96,13 +96,13 @@ class CourseTest { Course course1 = courses.get(0); assertEquals("Math", course1.getCourseName()); - assertEquals("C101", course1.getCourseCode()); - assertEquals(3, course1.getCourseCredit()); + assertEquals("C101", course1.getCourseID()); + assertEquals(3, course1.getCourseCredits()); Course course2 = courses.get(1); assertEquals("Science", course2.getCourseName()); - assertEquals("C201", course2.getCourseCode()); - assertEquals(4, course2.getCourseCredit()); + assertEquals("C201", course2.getCourseID()); + assertEquals(4, course2.getCourseCredits()); // Delete the temporary file deleteTempFile(); @@ -112,8 +112,8 @@ class CourseTest { void addCourse() { // Create a new course String courseName = sampleCourse.getCourseName(); - String courseCode = sampleCourse.getCourseCode(); - int courseCredit = sampleCourse.getCourseCredit(); + String courseCode = sampleCourse.getCourseID(); + int courseCredit = sampleCourse.getCourseCredits(); // Write course data to the temporary file Course.addCourse(courseName, courseCode, courseCredit, TEMP_FILE); @@ -125,8 +125,8 @@ class CourseTest { assertEquals(1, courses.size()); Course addedCourse = courses.get(0); assertEquals(courseName, addedCourse.getCourseName()); - assertEquals(courseCode, addedCourse.getCourseCode()); - assertEquals(courseCredit, addedCourse.getCourseCredit()); + assertEquals(courseCode, addedCourse.getCourseID()); + assertEquals(courseCredit, addedCourse.getCourseCredits()); // Delete the temporary file deleteTempFile(); @@ -137,14 +137,14 @@ class CourseTest { // Write courses to the test file PrintWriter writer = new PrintWriter(new FileWriter(TEMP_FILE)); writer.println(sampleCourse.getCourseName()); - writer.println(sampleCourse.getCourseCode()); - writer.println(sampleCourse.getCourseCredit()); + writer.println(sampleCourse.getCourseID()); + writer.println(sampleCourse.getCourseCredits()); writer.println(sampleCourse2.getCourseName()); - writer.println(sampleCourse2.getCourseCode()); - writer.println(sampleCourse2.getCourseCredit()); + writer.println(sampleCourse2.getCourseID()); + writer.println(sampleCourse2.getCourseCredits()); writer.println(sampleCourse3.getCourseName()); - writer.println(sampleCourse3.getCourseCode()); - writer.println(sampleCourse3.getCourseCredit()); + writer.println(sampleCourse3.getCourseID()); + writer.println(sampleCourse3.getCourseCredits()); writer.close(); // Simulate user input for course deletion From 910461e1ad9986ff25279cce1aec2e900043a64b Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 17:22:30 +0100 Subject: [PATCH 4/4] refactoring: adjusting Test functions to the naming convention agreed on --- src/test/java/org/example/CourseTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 33510b6..5e9dabc 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -24,7 +24,7 @@ class CourseTest { * Verifies that the method correctly prints course information to the console. */ @Test - void printCourseInfo() { + void testPrintCourseInfo() { // Set up the expected output String expectedCourseName = "Course Name: Math"; String expectedCourseCode = "Course Code: M101"; @@ -58,7 +58,7 @@ class CourseTest { * Verifies that course data is correctly written to a file and can be read back. */ @Test - void writeToFile() { + void testWriteToFile() { // Create a new course Course course = sampleCourse; @@ -84,7 +84,7 @@ class CourseTest { * Verifies that course data can be correctly read from a file. */ @Test - void readFromFile() { + void testReadFromFile() { // Create a temporary file with sample course data createTempFile("Math", "C101", "3", "Science", "C201", "4"); @@ -109,7 +109,7 @@ class CourseTest { } @Test - void addCourse() { + void testAddCourse() { // Create a new course String courseName = sampleCourse.getCourseName(); String courseCode = sampleCourse.getCourseID();