Browse Source

Added readFromFile Method in Admin Class

remotes/origin/fdai7780
Tobias Herbert 11 months ago
parent
commit
25e58e4413
  1. 30
      src/main/java/org/example/Admin.java

30
src/main/java/org/example/Admin.java

@ -1,8 +1,8 @@
package org.example; package org.example;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/** /**
* Represents an administrative user in the system. * Represents an administrative user in the system.
@ -60,9 +60,31 @@ public class Admin {
public void writeToFile(Admin admin, String filename) throws IOException{ public void writeToFile(Admin admin, String filename) throws IOException{
try(BufferedWriter writer = new BufferedWriter(new FileWriter(filename))){ try(BufferedWriter writer = new BufferedWriter(new FileWriter(filename))){
String attributes = admin.getId() + "\n" + admin.getName() + "\n" + admin.getRole();
String attributes = admin.getName() + "\n" + admin.getId() + "\n" + admin.getRole();
writer.write(attributes); writer.write(attributes);
} }
} }
public static List<Admin> readFromFile(String filename)
{
List<Admin> admins = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String name = line;
String id = reader.readLine();
String role = reader.readLine();
// Read any other fields you saved
Admin admin = new Admin(name, id, role);
admins.add(admin);
}
} catch (IOException e) {
e.printStackTrace();
}
return admins;
}
} }
Loading…
Cancel
Save