Browse Source

Merge branch 'fdai7887' into 'master'

Fdai7887

See merge request fdai7600/PWN_Alpha_Dogs!4
remotes/origin/fdai7780
fdai7887 11 months ago
parent
commit
88f4aec488
  1. 37
      src/main/java/org/example/Student.java
  2. 77
      src/test/java/org/example/StudentTest.java
  3. 0
      studentData.txt

37
src/main/java/org/example/Student.java

@ -1,4 +1,9 @@
package org.example;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Represents an student user in the system.
*/
@ -61,4 +66,36 @@ public class Student {
System.out.println("Role: " + getRole());
}
public void writeToFile(Student student, String filename) throws IOException {
try(BufferedWriter writer = new BufferedWriter(new FileWriter(filename))){
String attributes = student.getName() + "\n" + student.getId() + "\n" + student.getRole();
writer.write(attributes);
}
}
public static List<Student> readFromFile(String filename)
{
List<Student> students = 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();
Student student = new Student(name, id, role);
students.add(student);
}
} catch (IOException e) {
e.printStackTrace();
}
return students;
}
}

77
src/test/java/org/example/StudentTest.java

@ -0,0 +1,77 @@
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 StudentTest {
@org.junit.jupiter.api.Test
void writeToFile() throws IOException {
Student student = new Student("Sebastian", "S1001", "Student") ;
String filename = "testStudentData.txt";
student.writeToFile(student, filename);
Path path = Path.of(filename);
String content = Files.readString(path);
assertEquals("Sebastian\nS1001\nStudent", content);
Files.delete(path);
}
@Test
void readFromFile() throws IOException {
try (PrintWriter writer = new PrintWriter(new FileWriter("testStudentData.txt"))){
writer.println("Samuel");
writer.println("S1001");
writer.print("Student");
} catch (IOException e) {
e.printStackTrace();
}
String filename = "testStudentData.txt";
List<Student> testStudents = Student.readFromFile(filename);
assertEquals(1, testStudents.size());
Student admin = testStudents.get(0);
assertEquals("Samuel", admin.getName());
assertEquals("S1001", admin.getId());
assertEquals("Student", admin.getRole());
Files.delete(Path.of(filename));
}
@Test
void printStudentInfo() {
Student student = new Student("Sebastian", "S1001", "Student");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStream));
student.printStudentInfo();
String printedOutput = outputStream.toString().trim();
assertTrue(printedOutput.contains("Sebastian"));
assertTrue(printedOutput.contains("S1001"));
assertTrue(printedOutput.contains("Student"));
}
}

0
studentData.txt

Loading…
Cancel
Save