Browse Source

Merge branch 'fdai7921' into 'master'

Fdai7921

See merge request fdai7600/PWN_Alpha_Dogs!10
remotes/origin/fdai7780
fdai7921 11 months ago
parent
commit
2e1fe55a3a
  1. 4
      src/main/java/org/example/Administration.java
  2. 44
      src/main/java/org/example/Course.java
  3. 6
      src/main/java/org/example/Main.java
  4. 42
      src/test/java/org/example/CourseTest.java
  5. 14
      src/test/java/org/example/StudentTest.java

4
src/main/java/org/example/Administration.java

@ -43,7 +43,7 @@ public class Administration {
*/
public boolean deleteCourse(String courseID){
for(Course course : courses){
if(course.getCourseCode().equals(courseID)){
if(course.getCourseID().equals(courseID)){
courses.remove(course);
return true;
}
@ -59,7 +59,7 @@ public class Administration {
*/
public Course findCourseByID(String courseID){
for(Course course : courses){
if(course.getCourseCode().equals(courseID)){
if(course.getCourseID().equals(courseID)){
return course;
}
}

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

@ -16,10 +16,10 @@ public class Course {
private String courseName;
/** The code of the course. */
private String courseCode;
private String courseID;
/** The credit value of the course. */
private int courseCredit;
private int courseCredits;
/**
* Constructs a new Course object with default values.
@ -31,13 +31,13 @@ public class Course {
* Constructs a new Course object with specified values.
*
* @param courseName The name of the course.
* @param courseCode The code of the course.
* @param courseCredit The credit value of the course.
* @param courseID The code of the course.
* @param courseCredits The credit value of the course.
*/
public Course(String courseName, String courseCode, int courseCredit) {
public Course(String courseName, String courseID, int courseCredits) {
this.courseName = courseName;
this.courseCode = courseCode;
this.courseCredit = courseCredit;
this.courseID = courseID;
this.courseCredits = courseCredits;
}
/**
@ -63,17 +63,17 @@ public class Course {
*
* @return The code of the course.
*/
public String getCourseCode() {
return courseCode;
public String getCourseID() {
return courseID;
}
/**
* Sets the code of the course.
*
* @param courseCode The code of the course.
* @param courseID The code of the course.
*/
public void setCourseCode(String courseCode) {
this.courseCode = courseCode;
public void setCourseID(String courseID) {
this.courseID = courseID;
}
/**
@ -81,17 +81,17 @@ public class Course {
*
* @return The credit value of the course.
*/
public int getCourseCredit() {
return courseCredit;
public int getCourseCredits() {
return courseCredits;
}
/**
* Sets the credit value of the course.
*
* @param courseCredit The credit value of the course.
* @param courseCredits The credit value of the course.
*/
public void setCourseCredit(int courseCredit) {
this.courseCredit = courseCredit;
public void setCourseCredits(int courseCredits) {
this.courseCredits = courseCredits;
}
/**
@ -99,8 +99,8 @@ public class Course {
*/
public void printCourseInfo() {
System.out.println("Course Name: " + courseName);
System.out.println("Course Code: " + courseCode);
System.out.println("Course Credits: " + courseCredit);
System.out.println("Course Code: " + courseID);
System.out.println("Course Credits: " + courseCredits);
}
/**
@ -172,7 +172,7 @@ public class Course {
private static boolean courseExists(String filename, String name, String code) {
List<Course> courses = readFromFile(filename);
for (Course course : courses) {
if (course.getCourseName().equalsIgnoreCase(name) || course.getCourseCode().equalsIgnoreCase(code)) {
if (course.getCourseName().equalsIgnoreCase(name) || course.getCourseID().equalsIgnoreCase(code)) {
return true; // Course with the same name or code already exists
}
}
@ -244,8 +244,8 @@ public class Course {
try (FileWriter writer = new FileWriter(filename)) {
for (Course course : courses) {
writer.write(course.getCourseName() + "\n");
writer.write(course.getCourseCode() + "\n");
writer.write(course.getCourseCredit() + "\n");
writer.write(course.getCourseID() + "\n");
writer.write(course.getCourseCredits() + "\n");
// Write any other fields you saved
}
} catch (IOException e) {

6
src/main/java/org/example/Main.java

@ -2,6 +2,8 @@ package org.example;
import java.util.Scanner;
public class Main {
public static final String courseData_file = "courseData.txt";
public static void main(String[] args) {System.out.println("Hello world!");
Administration administration = new Administration();
@ -31,11 +33,11 @@ public class Main {
break;
case 3:
System.out.println("Add Course selected");
Course.addCourseFromUserInput("courseData.txt");
Course.addCourseFromUserInput(courseData_file);
break;
case 4:
System.out.println("Delete Course selected");
Course.deleteCourse("courseData.txt");
Course.deleteCourse(courseData_file);
break;
case 5:
System.out.println("Assign Grade selected");

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

@ -24,7 +24,7 @@ class CourseTest {
* Verifies that the method correctly prints course information to the console.
*/
@Test
void printCourseInfo() {
void testPrintCourseInfo() {
// Set up the expected output
String expectedCourseName = "Course Name: Math";
String expectedCourseCode = "Course Code: M101";
@ -58,12 +58,12 @@ class CourseTest {
* Verifies that course data is correctly written to a file and can be read back.
*/
@Test
void writeToFile() {
void testWriteToFile() {
// Create a new course
Course course = sampleCourse;
// Write course data to the temporary file
course.writeToFile(course.getCourseName(), course.getCourseCode(), course.getCourseCredit(), TEMP_FILE);
course.writeToFile(course.getCourseName(), course.getCourseID(), course.getCourseCredits(), TEMP_FILE);
// Read course data from the temporary file
List<Course> courses = Course.readFromFile(TEMP_FILE);
@ -72,8 +72,8 @@ class CourseTest {
assertEquals(1, courses.size());
Course readCourse = courses.get(0);
assertEquals("Math", readCourse.getCourseName());
assertEquals("M101", readCourse.getCourseCode());
assertEquals(3, readCourse.getCourseCredit());
assertEquals("M101", readCourse.getCourseID());
assertEquals(3, readCourse.getCourseCredits());
// Delete the temporary file
deleteTempFile();
@ -84,7 +84,7 @@ class CourseTest {
* Verifies that course data can be correctly read from a file.
*/
@Test
void readFromFile() {
void testReadFromFile() {
// Create a temporary file with sample course data
createTempFile("Math", "C101", "3", "Science", "C201", "4");
@ -96,24 +96,24 @@ class CourseTest {
Course course1 = courses.get(0);
assertEquals("Math", course1.getCourseName());
assertEquals("C101", course1.getCourseCode());
assertEquals(3, course1.getCourseCredit());
assertEquals("C101", course1.getCourseID());
assertEquals(3, course1.getCourseCredits());
Course course2 = courses.get(1);
assertEquals("Science", course2.getCourseName());
assertEquals("C201", course2.getCourseCode());
assertEquals(4, course2.getCourseCredit());
assertEquals("C201", course2.getCourseID());
assertEquals(4, course2.getCourseCredits());
// Delete the temporary file
deleteTempFile();
}
@Test
void addCourse() {
void testAddCourse() {
// Create a new course
String courseName = sampleCourse.getCourseName();
String courseCode = sampleCourse.getCourseCode();
int courseCredit = sampleCourse.getCourseCredit();
String courseCode = sampleCourse.getCourseID();
int courseCredit = sampleCourse.getCourseCredits();
// Write course data to the temporary file
Course.addCourse(courseName, courseCode, courseCredit, TEMP_FILE);
@ -125,8 +125,8 @@ class CourseTest {
assertEquals(1, courses.size());
Course addedCourse = courses.get(0);
assertEquals(courseName, addedCourse.getCourseName());
assertEquals(courseCode, addedCourse.getCourseCode());
assertEquals(courseCredit, addedCourse.getCourseCredit());
assertEquals(courseCode, addedCourse.getCourseID());
assertEquals(courseCredit, addedCourse.getCourseCredits());
// Delete the temporary file
deleteTempFile();
@ -137,14 +137,14 @@ class CourseTest {
// Write courses to the test file
PrintWriter writer = new PrintWriter(new FileWriter(TEMP_FILE));
writer.println(sampleCourse.getCourseName());
writer.println(sampleCourse.getCourseCode());
writer.println(sampleCourse.getCourseCredit());
writer.println(sampleCourse.getCourseID());
writer.println(sampleCourse.getCourseCredits());
writer.println(sampleCourse2.getCourseName());
writer.println(sampleCourse2.getCourseCode());
writer.println(sampleCourse2.getCourseCredit());
writer.println(sampleCourse2.getCourseID());
writer.println(sampleCourse2.getCourseCredits());
writer.println(sampleCourse3.getCourseName());
writer.println(sampleCourse3.getCourseCode());
writer.println(sampleCourse3.getCourseCredit());
writer.println(sampleCourse3.getCourseID());
writer.println(sampleCourse3.getCourseCredits());
writer.close();
// Simulate user input for course deletion

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

@ -10,9 +10,11 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class StudentTest {
private static final Student sampleStudent = new Student("Sebastian Reichard", "S1001", "Student");
@org.junit.jupiter.api.Test
void writeToFile() throws IOException {
Student student = new Student("Sebastian", "S1001", "Student") ;
Student student = sampleStudent;
String filename = "testStudentData.txt";
@ -20,7 +22,7 @@ class StudentTest {
Path path = Path.of(filename);
String content = Files.readString(path);
assertEquals("Sebastian\nS1001\nStudent", content);
assertEquals("Sebastian Reichard\nS1001\nStudent", content);
Files.delete(path);
@ -34,7 +36,7 @@ class StudentTest {
void readFromFile() throws IOException {
try (PrintWriter writer = new PrintWriter(new FileWriter("testStudentData.txt"))){
writer.println("Samuel");
writer.println("Sebastian Reichard");
writer.println("S1001");
writer.print("Student");
} catch (IOException e) {
@ -46,7 +48,7 @@ class StudentTest {
assertEquals(1, testStudents.size());
Student admin = testStudents.get(0);
assertEquals("Samuel", admin.getName());
assertEquals("Sebastian Reichard", admin.getName());
assertEquals("S1001", admin.getId());
assertEquals("Student", admin.getRole());
@ -57,7 +59,7 @@ class StudentTest {
@Test
void printStudentInfo() {
Student student = new Student("Sebastian", "S1001", "Student");
Student student = sampleStudent;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStream));
@ -66,7 +68,7 @@ class StudentTest {
String printedOutput = outputStream.toString().trim();
assertTrue(printedOutput.contains("Sebastian"));
assertTrue(printedOutput.contains("Sebastian Reichard"));
assertTrue(printedOutput.contains("S1001"));
assertTrue(printedOutput.contains("Student"));

Loading…
Cancel
Save