Browse Source

Added readFromFile() function in class Professor

remotes/origin/fdai7780
fdai7600 11 months ago
parent
commit
5a99747815
  1. 16
      src/main/java/org/example/Professor.java

16
src/main/java/org/example/Professor.java

@ -116,4 +116,20 @@ public class Professor {
writer.newLine(); writer.newLine();
} }
} }
public static List<Professor> readFromFile() throws IOException {
List<Professor> 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;
}
} }
Loading…
Cancel
Save