Browse Source

Merge branch 'fdai7780'

remotes/origin/fdai7780
Tobias Herbert 11 months ago
parent
commit
d05b9ded12
  1. 0
      adminData.txt
  2. 14
      pom.xml
  3. 45
      src/main/java/org/example/Admin.java
  4. 70
      src/test/java/org/example/AdminTest.java

0
adminData.txt

14
pom.xml

@ -13,5 +13,19 @@
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

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

@ -1,5 +1,9 @@
package org.example;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Represents an administrative user in the system.
*/
@ -53,4 +57,45 @@ public class Admin {
System.out.println("ID: " + getId());
System.out.println("Role: " + getRole());
}
/**
*
* @param admin the data of this admin is being written to the file
* @param filename the name of the file to write the data to
*/
public void writeToFile(Admin admin, String filename) throws IOException{
try(BufferedWriter writer = new BufferedWriter(new FileWriter(filename))){
String attributes = admin.getName() + "\n" + admin.getId() + "\n" + admin.getRole();
writer.write(attributes);
}
}
/**
*
* @param filename the name of the file to read the data from
* @return the list of admins that have been read from the file
*/
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;
}
}

70
src/test/java/org/example/AdminTest.java

@ -0,0 +1,70 @@
package org.example;
import org.junit.jupiter.api.Test;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class AdminTest {
@org.junit.jupiter.api.Test
void printAdminInfo() {
Admin admin = new Admin("Aaron", "A1001", "Admin");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStream));
admin.printAdminInfo();
String printedOutput = outputStream.toString().trim();
assertTrue(printedOutput.contains("Aaron"));
assertTrue(printedOutput.contains("A1001"));
assertTrue(printedOutput.contains("Admin"));
}
@Test
void writeToFile() throws IOException {
Admin admin = new Admin("Simon", "A1001", "Admin") ;
String filename = "testAdminData.txt";
admin.writeToFile(admin, filename);
Path path = Path.of(filename);
String content = Files.readString(path);
assertEquals("Simon\nA1001\nAdmin", content);
Files.delete(path);
}
@Test
void readFromFile() throws IOException {
try (PrintWriter writer = new PrintWriter(new FileWriter("testAdminData.txt"))){
writer.println("Thomas");
writer.println("A1001");
writer.print("Admin");
} catch (IOException e) {
e.printStackTrace();
}
String filename = "testAdminData.txt";
List<Admin> testAdmins = Admin.readFromFile(filename);
assertEquals(1, testAdmins.size());
Admin admin = testAdmins.get(0);
assertEquals("Thomas", admin.getName());
assertEquals("A1001", admin.getId());
assertEquals("Admin", admin.getRole());
Files.delete(Path.of(filename));
}
}
Loading…
Cancel
Save