From 5a9974781541717746e2a45856d2e9f3bda1f927 Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Wed, 7 Feb 2024 16:48:21 +0100 Subject: [PATCH 1/5] Added readFromFile() function in class Professor --- src/main/java/org/example/Professor.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/org/example/Professor.java b/src/main/java/org/example/Professor.java index 5388030..ad7b358 100644 --- a/src/main/java/org/example/Professor.java +++ b/src/main/java/org/example/Professor.java @@ -116,4 +116,20 @@ public class Professor { writer.newLine(); } } + + public static List readFromFile() throws IOException { + List professors = new ArrayList<>(); + try (BufferedReader reader = new BufferedReader(new FileReader("professorData.txt"))) { + String line; + while ((line = reader.readLine()) != null) { + // Assuming the toString format is exactly how we write to the file + String[] parts = line.replace("Professor{", "").replace("}", "").split(", "); + String professorID = parts[0].split("'")[1]; // Extracting professorID + String name = parts[1].split("'")[1]; // Extracting Name + String role = parts[2].split("'")[1]; // Extracting role + professors.add(new Professor(professorID, name, role)); + } + } + return professors; + } } From 8133f81a6f783980d2fa69d79654edfdecf1f123 Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Wed, 7 Feb 2024 16:51:04 +0100 Subject: [PATCH 2/5] Test: added testReadFromFile() for the professor Data --- src/test/java/org/example/ProfessorTest.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/test/java/org/example/ProfessorTest.java b/src/test/java/org/example/ProfessorTest.java index ab52603..002f461 100644 --- a/src/test/java/org/example/ProfessorTest.java +++ b/src/test/java/org/example/ProfessorTest.java @@ -1,4 +1,24 @@ package org.example; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.List; + +import static org.junit.Assert.assertEquals; + public class ProfessorTest { + + @Test + void testReadFromFile() throws IOException { + Professor professor = new Professor("P1002", "Jane Doe", "Professor"); + professor.writeToFile(); + + List professors = Professor.readFromFile(); + + assertEquals(1, professors.size()); + assertEquals("P1002", professors.get(0).getProfessorID()); + assertEquals("Jane Doe", professors.get(0).getName()); + assertEquals("Professor", professors.get(0).getRole()); + } } From 149fa2c5afaa060fa884e27361489cb55097791d Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Wed, 7 Feb 2024 16:54:37 +0100 Subject: [PATCH 3/5] Cleared to test the functions of reading professor data --- professorData.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/professorData.txt b/professorData.txt index e69de29..2ed6e43 100644 --- a/professorData.txt +++ b/professorData.txt @@ -0,0 +1 @@ +Professor{professorID='P1002', Name='Jane Doe', role='Professor'} From fe694511b0bcfeaf8980f4b1a6785b1aa6fc97b5 Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Wed, 7 Feb 2024 16:55:00 +0100 Subject: [PATCH 4/5] Cleared to test the functions of reading professor data --- professorData.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/professorData.txt b/professorData.txt index 2ed6e43..e69de29 100644 --- a/professorData.txt +++ b/professorData.txt @@ -1 +0,0 @@ -Professor{professorID='P1002', Name='Jane Doe', role='Professor'} From 533ffedbe4685355eca61f0c9cf2c6511cdc1683 Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Wed, 7 Feb 2024 16:59:08 +0100 Subject: [PATCH 5/5] Test: Added testWriteToFile() to write the professor data into professorData.txt --- src/test/java/org/example/ProfessorTest.java | 30 ++++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/example/ProfessorTest.java b/src/test/java/org/example/ProfessorTest.java index 002f461..65b4c2c 100644 --- a/src/test/java/org/example/ProfessorTest.java +++ b/src/test/java/org/example/ProfessorTest.java @@ -1,14 +1,38 @@ package org.example; +import org.example.Professor; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - -import java.io.IOException; +import static org.junit.jupiter.api.Assertions.*; +import java.io.*; import java.util.List; -import static org.junit.Assert.assertEquals; public class ProfessorTest { + @BeforeEach + void setup() throws IOException { + // Ensure the file is clean before each test + new PrintWriter("professorData.txt").close(); + } + + @AfterEach + void tearDown() { + // Clean up after tests + new File("professorData.txt").delete(); + } + + @Test + void testWriteToFile() throws IOException { + Professor professor = new Professor("P1001", "John Doe", "Professor"); + professor.writeToFile(); + + // Verify the file contains the expected data + try (BufferedReader reader = new BufferedReader(new FileReader("professorData.txt"))) { + assertEquals("Professor{professorID='P1001', Name='John Doe', role='Professor'}", reader.readLine()); + } + } @Test void testReadFromFile() throws IOException { Professor professor = new Professor("P1002", "Jane Doe", "Professor");