Browse Source

Merge branch 'fdai7921' into 'master'

Fdai7921

See merge request fdai7600/PWN_Alpha_Dogs!5
remotes/origin/fdai7780
fdai7921 11 months ago
parent
commit
500d4e4480
  1. 119
      src/main/java/org/example/Course.java
  2. 2
      src/main/java/org/example/Main.java
  3. 75
      src/test/java/org/example/CourseTest.java

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

@ -2,6 +2,7 @@ package org.example;
import java.io.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
@ -105,14 +106,16 @@ public class Course {
/**
* Writes the information of the course to a file.
*
* @param filename The name of the file to write to.
* @param courseName The name of the course.
* @param courseCode The code of the course.
* @param courseCredit The credit value of the course.
* @param filename The name of the file to write to.
*/
public void writeToFile(String filename) {
try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) {
public void writeToFile(String courseName, String courseCode, int courseCredit, String filename) {
try (PrintWriter writer = new PrintWriter(new FileWriter(filename, true))) {
writer.println(courseName);
writer.println(courseCode);
writer.println(courseCredit);
// Add any other fields you want to save
} catch (IOException e) {
e.printStackTrace();
}
@ -144,5 +147,113 @@ public class Course {
return courses;
}
/**
* Adds a new course to the file with provided details.
*
* @param courseName The name of the course.
* @param courseCode The code of the course.
* @param courseCredit The credit value of the course.
* @param filename The name of the file to write to.
*/
public static void addCourse(String courseName, String courseCode, int courseCredit, String filename) {
// Write course data to the file
Course course = new Course();
course.writeToFile(courseName, courseCode, courseCredit, filename);
}
/**
* Checks if a course with the given name or code already exists in the file.
*
* @param filename The name of the file to read from.
* @param name The name of the course.
* @param code The code of the course.
* @return True if a course with the same name or code exists, otherwise False.
*/
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)) {
return true; // Course with the same name or code already exists
}
}
return false; // No course with the same name or code found
}
/**
* Adds a new course to the file based on user input.
*
* @param filename The name of the file to write to.
*/
public static void addCourseFromUserInput(String filename) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the name of the course: ");
String courseName = scanner.nextLine();
System.out.print("Enter the code of the course: ");
String courseCode = scanner.nextLine();
// Check if the course already exists
if (courseExists(filename, courseName, courseCode)) {
System.out.println("Course with the same name or code already exists.");
return;
}
System.out.print("Enter the credit value of the course: ");
int courseCredit = scanner.nextInt();
// Add the course using the provided data
addCourse(courseName, courseCode, courseCredit, filename);
System.out.println("Added Course successfully!");
}
/**
* Deletes a course from the file based on user input.
*
* @param filename The name of the file to write to.
*/
public static void deleteCourse(String filename) {
List<Course> courses = readFromFile(filename);
if (courses.isEmpty()) {
System.out.println("No courses found.");
return;
}
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Select the course to delete (or 0 to go back to the menu):");
// Display available courses for selection
for (int i = 0; i < courses.size(); i++) {
System.out.println((i + 1) + ". " + courses.get(i).getCourseName());
}
int selection = scanner.nextInt();
if (selection == 0) {
return; // Return to the menu
} else if (selection < 1 || selection > courses.size()) {
System.out.println("Invalid selection.");
} else {
Course courseToDelete = courses.get(selection - 1);
courses.remove(courseToDelete);
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");
// Write any other fields you saved
}
} catch (IOException e) {
System.out.println("An error occurred while deleting the course.");
e.printStackTrace();
}
System.out.println("Course deleted successfully.");
return;
}
}
}
}

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

@ -31,9 +31,11 @@ public class Main {
break;
case 3:
System.out.println("Add Course selected");
Course.addCourseFromUserInput("courseData.txt");
break;
case 4:
System.out.println("Delete Course selected");
Course.deleteCourse("courseData.txt");
break;
case 5:
System.out.println("Assign Grade selected");

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

@ -6,6 +6,7 @@ import java.io.PrintStream;
import static org.junit.Assert.assertEquals;
import java.io.*;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/**
* The CourseTest class contains unit tests for the Course class.
@ -62,7 +63,7 @@ class CourseTest {
Course course = new Course("Math", "C101", 3);
// Write course data to the temporary file
course.writeToFile(TEMP_FILE);
course.writeToFile(course.getCourseName(), course.getCourseCode(), course.getCourseCredit(), TEMP_FILE);
// Read course data from the temporary file
List<Course> courses = Course.readFromFile(TEMP_FILE);
@ -107,6 +108,78 @@ class CourseTest {
deleteTempFile();
}
/**
* Tests the addCourse method of the Course class.
* Verifies that a new course can be added to the file.
*/
@Test
void addCourse() {
// Create a new course
String courseName = "Math";
String courseCode = "C101";
int courseCredit = 3;
// Write course data to the temporary file
Course.addCourse(courseName, courseCode, courseCredit, TEMP_FILE);
// Read course data from the temporary file
List<Course> courses = Course.readFromFile(TEMP_FILE);
// Assert that the course is added successfully
assertEquals(1, courses.size());
Course addedCourse = courses.get(0);
assertEquals(courseName, addedCourse.getCourseName());
assertEquals(courseCode, addedCourse.getCourseCode());
assertEquals(courseCredit, addedCourse.getCourseCredit());
// Delete the temporary file
deleteTempFile();
}
/**
* Tests the deleteCourse method of the Course class.
* Verifies that a course can be deleted from the file.
*/
@Test
public void testDeleteCourse() throws IOException {
// Prepare test data
Course course1 = new Course("Math", "M101", 3);
Course course2 = new Course("Science", "S101", 4);
Course course3 = new Course("Java", "J101", 4);
// Write courses to the test file
PrintWriter writer = new PrintWriter(new FileWriter(TEMP_FILE));
writer.println(course1.getCourseName());
writer.println(course1.getCourseCode());
writer.println(course1.getCourseCredit());
writer.println(course2.getCourseName());
writer.println(course2.getCourseCode());
writer.println(course2.getCourseCredit());
writer.println(course3.getCourseName());
writer.println(course3.getCourseCode());
writer.println(course3.getCourseCredit());
writer.close();
// Simulate user input for course deletion
ByteArrayInputStream inputStream = new ByteArrayInputStream("1\n0".getBytes());
System.setIn(inputStream);
// Delete a course
Course.deleteCourse(TEMP_FILE);
// Read courses after deletion
List<Course> coursesAfterDeletion = Course.readFromFile(TEMP_FILE);
// Ensure the correct number of courses remaining after deletion
assertEquals(2, coursesAfterDeletion.size());
// Check if the correct course was deleted
assertFalse(coursesAfterDeletion.contains(course1));
deleteTempFile();
}
/**
* Creates a temporary file with the provided data.
*

Loading…
Cancel
Save