Browse Source

Merge branch 'fdai7921' into 'master'

Fdai7921

See merge request fdai7600/PWN_Alpha_Dogs!3
remotes/origin/fdai7780
fdai7921 11 months ago
parent
commit
7c0042942e
  1. 0
      courseData.txt
  2. 38
      src/main/java/org/example/Course.java
  3. 110
      src/test/java/org/example/CourseTest.java

0
courseData.txt

38
src/main/java/org/example/Course.java

@ -1,4 +1,9 @@
package org.example; package org.example;
import java.io.*;
import java.util.List;
import java.util.ArrayList;
/** /**
* The Course class represents a course in a curriculum. * The Course class represents a course in a curriculum.
@ -96,4 +101,37 @@ public class Course {
System.out.println("Course Code: " + courseCode); System.out.println("Course Code: " + courseCode);
System.out.println("Course Credits: " + courseCredit); System.out.println("Course Credits: " + courseCredit);
} }
public void writeToFile(String filename) {
try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) {
writer.println(courseName);
writer.println(courseCode);
writer.println(courseCredit);
// Add any other fields you want to save
} catch (IOException e) {
e.printStackTrace();
}
}
public static List<Course> readFromFile(String filename) {
List<Course> courses = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String courseName = line;
String courseCode = reader.readLine();
int credits = Integer.parseInt(reader.readLine());
// Read any other fields you saved
Course course = new Course(courseName, courseCode, credits);
courses.add(course);
}
} catch (IOException e) {
e.printStackTrace();
}
return courses;
}
} }

110
src/test/java/org/example/CourseTest.java

@ -0,0 +1,110 @@
package org.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.Assert.assertEquals;
import java.io.*;
import java.util.List;
class CourseTest {
@Test
void printCourseInfo() {
// Create a new Course object
Course course = new Course("Introduction to Programming", "CS101", 3);
// Set up the expected output
String expectedCourseName = "Course Name: Introduction to Programming";
String expectedCourseCode = "Course Code: CS101";
String expectedCourseCredits = "Course Credits: 3";
// Redirect System.out to capture printed output
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream originalOut = System.out;
System.setOut(new PrintStream(outputStream));
// Call the method to be tested
course.printCourseInfo();
// Reset System.out
System.setOut(originalOut);
// Get the printed output
String printedOutput = outputStream.toString().trim(); // trim to remove trailing newline
// Split the printed output into lines
String[] outputLines = printedOutput.split("\\r?\\n");
// Assert that each line matches the expected output
assertEquals(expectedCourseName, outputLines[0].trim());
assertEquals(expectedCourseCode, outputLines[1].trim());
assertEquals(expectedCourseCredits, outputLines[2].trim());
}
@Test
void writeToFile() {
// Create a temporary file for testing
String filename = "test_course_data.txt";
// Create a new course
Course course = new Course("Math", "C101", 3);
// Write course data to the temporary file
course.writeToFile(filename);
// Read course data from the temporary file
List<Course> courses = Course.readFromFile(filename);
// Assert that the read data matches the original course
assertEquals(1, courses.size());
Course readCourse = courses.get(0);
assertEquals("Math", readCourse.getCourseName());
assertEquals(3, readCourse.getCourseCredit());
// Delete the temporary file
File file = new File(filename);
if (file.exists()) {
file.delete();
}
}
@Test
void readFromFile() {
// Create a temporary file with sample course data
String filename = "test_course_data.txt";
try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) {
writer.println("Math");
writer.println("C101");
writer.println(3);
writer.println("Science");
writer.println("C201");
writer.println(4);
} catch (IOException e) {
e.printStackTrace();
}
// Read course data from the temporary file
List<Course> courses = Course.readFromFile(filename);
// Assert that the read data matches the original course data
assertEquals(2, courses.size());
Course course1 = courses.get(0);
assertEquals("Math", course1.getCourseName());
assertEquals("C101", course1.getCourseCode());
assertEquals(3, course1.getCourseCredit());
Course course2 = courses.get(1);
assertEquals("Science", course2.getCourseName());
assertEquals("C201", course2.getCourseCode());
assertEquals(4, course2.getCourseCredit());
// Delete the temporary file
File file = new File(filename);
if (file.exists()) {
file.delete();
}
}
}
Loading…
Cancel
Save