From 5a9974781541717746e2a45856d2e9f3bda1f927 Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Wed, 7 Feb 2024 16:48:21 +0100 Subject: [PATCH] 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; + } }