diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 7504fa3..87616b2 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -49,7 +49,7 @@ public static void main(String[] args) { CourseCreationDsGateway course; try { course = new FileCourse("./courses.csv"); - } catch (IOException e) { + } catch (IOException | ClassNotFoundException e) { throw new RuntimeException("Could not create file."); } CourseCreationOutputBoundary presenter = new CourseCreationPresenter(); diff --git a/src/main/java/course_creation_use_case/CourseCreationInteractor.java b/src/main/java/course_creation_use_case/CourseCreationInteractor.java index d652715..ebc0ce2 100644 --- a/src/main/java/course_creation_use_case/CourseCreationInteractor.java +++ b/src/main/java/course_creation_use_case/CourseCreationInteractor.java @@ -3,6 +3,8 @@ // Use case layer import entities.*; +import read_write.CourseReadWrite; +import read_write.TaskReadWrite; import java.util.ArrayList; @@ -55,6 +57,10 @@ public CourseCreationResponseModel create(CourseCreationRequestModel requestMode // TaskMap.addTask(course.getCourseName() + courseTasks.indexOf(task), task); } + // save course to file +// CourseReadWrite crw = new CourseReadWrite("src/data/CourseMap"); +// CourseMap.saveToFile(trw); + // course sent to presenter CourseCreationResponseModel courseResponseModel = new CourseCreationResponseModel( course.getCourseID(), course.getTasks()); diff --git a/src/main/java/entities/Course.java b/src/main/java/entities/Course.java index 708126a..3313701 100644 --- a/src/main/java/entities/Course.java +++ b/src/main/java/entities/Course.java @@ -1,12 +1,13 @@ package entities; +import java.io.Serializable; import java.util.ArrayList; /** * entity layer */ -public class Course { +public class Course implements Serializable { /** * class for Course entity with private instance variables * using StudentUser, InstructorUser, and Task entities diff --git a/src/main/java/read_write/CourseReadWrite.java b/src/main/java/read_write/CourseReadWrite.java new file mode 100644 index 0000000..ff999fc --- /dev/null +++ b/src/main/java/read_write/CourseReadWrite.java @@ -0,0 +1,58 @@ +package read_write; + +import entities.*; + +import java.io.*; +import java.util.ArrayList; + +public class CourseReadWrite implements ReadWriter { + + String path; + + public CourseReadWrite(String path) throws IOException, ClassNotFoundException { + this.path = path; + + CourseMap courses = buildCourseMap(); + saveToFile(courses); + + CourseMap courseMap = readFromFile(); + System.out.println(courseMap); + System.out.println(courses); + } + + /** + * reads in the courseMap from a file (CourseMap.java) + * @return the courseMap that is read + */ + public CourseMap readFromFile() throws IOException, ClassNotFoundException { + FileInputStream fileReader = new FileInputStream("courses.ser"); + ObjectInputStream in = new ObjectInputStream(fileReader); + CourseMap courseMap = (CourseMap) in.readObject(); + return courseMap; + } + + /** + * saves the courseMap to a file (CourseMap.java) + * @param courseMap - object to be serialized + */ + public void saveToFile(Object courseMap) throws IOException { + FileOutputStream fileWriter = new FileOutputStream("courses.ser"); + ObjectOutputStream out = new ObjectOutputStream(fileWriter); + out.writeObject(courseMap); + out.close(); + fileWriter.close(); + } + + /** + * initialization + * @return the courseMap + */ + private static CourseMap buildCourseMap() { + ArrayList courseTasks = new ArrayList(); + courseTasks.add("task1"); + Course csc207 = new Course("csc207", "paulgries", courseTasks); + CourseMap courses = new CourseMap(); + courses.addCourse("csc207paulgries", csc207); + return courses; + } +} diff --git a/src/main/java/screens/FileCourse.java b/src/main/java/screens/FileCourse.java index 858b0ac..8e8da95 100644 --- a/src/main/java/screens/FileCourse.java +++ b/src/main/java/screens/FileCourse.java @@ -1,165 +1,66 @@ package screens; import course_creation_use_case.CourseCreationDsGateway; -//import course_creation_use_case.courseCreationDsRequestModel; +import entities.*; import course_creation_use_case.CourseCreationRequestModel; -/* - * Notes: - * I think for csv file? so columns is what each course entity is? im assuming? idek man - */ import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class FileCourse implements CourseCreationDsGateway { - private final File csvFile; - private final Map headers = new HashMap<>(); - private final Map courses = new HashMap<>(); - - public FileCourse(String csvPath) throws IOException { - csvFile = new File(csvPath); - - headers.put("course_name", 0); - headers.put("course_instructor", 1); - headers.put("tasks?", 2); - - if (csvFile.length() == 0) { - saveCourse(); - } else { - - BufferedReader reader = new BufferedReader(new FileReader(csvFile)); - reader.readLine(); // skip the header - - String row; - while ((row = reader.readLine()) != null) { - String[] col = row.split(","); - String courseName = String.valueOf(col[headers.get("courseName")]); - String courseInstructor = String.valueOf(col[headers.get("courseInstructor")]); - ArrayList tasks = new ArrayList(); // idk what im doing -// /* String.valueOf(col[headers.get("tasks?")]); */ - CourseCreationRequestModel course = new CourseCreationRequestModel(courseName, courseInstructor, tasks); - courses.put(courseName, course); - } - - reader.close(); + /** + * is this supposed to be String to Request Model OR String to Course + */ + private final HashMap courses; + private final String filePath; - } + /** + * reads the Course database file, stores its contents -- HashMap of course id to Course object + * @param path - + */ + public FileCourse(String path) throws IOException, ClassNotFoundException { + this.courses = readFromFile(); + this.filePath = path; + } + /** + * reads the courseMap from a file + * @return - + */ + public HashMap readFromFile() throws IOException, ClassNotFoundException { + FileInputStream fileReader = new FileInputStream(this.filePath); + ObjectInputStream in = new ObjectInputStream(fileReader); + HashMap courseMap = (HashMap) in.readObject(); + in.close(); + fileReader.close(); + return courseMap; } /** - * Add requestModel to storage - * @param requestModel the user information to save + * adds the request model to database + * @param requestModel the course info that is being saved */ @Override public void saveCourse(CourseCreationRequestModel requestModel) { - courses.put(requestModel.getCourseName() ,requestModel); - this.saveCourse(); + courses.put(requestModel.getCourseID(), requestModel); +// this.saveCourse(); } - - private void saveCourse() { - BufferedWriter writer; - try { - writer = new BufferedWriter(new FileWriter(csvFile)); - writer.write(String.join(",", headers.keySet())); - writer.newLine(); - - for (CourseCreationRequestModel course : courses.values()) { - String line = "%s,%s,%s".format( - course.getCourseName(), course.getCourseInstructor(), course.getTasks()); - writer.write(line); - writer.newLine(); - } - - writer.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - + private void saveCourse() throws IOException { + FileOutputStream fileWriter = new FileOutputStream(filePath); + ObjectOutputStream out = new ObjectOutputStream(fileWriter); + out.writeObject(courses); + out.close(); + fileWriter.close(); } @Override - public boolean existsByCourseID(String identifier) { - return courses.containsKey(identifier); + public boolean existsByCourseID(String courseIdentifier) { + return courses.containsKey(courseIdentifier); } -} - -/* - * with ds request but apparently not needed so - */ -//public class FileCourse implements courseCreationDsGateway { -// private final File csvFile; -// private final Map headers = new HashMap<>(); -// private final Map courses = new HashMap<>(); -// -// public FileCourse(String csvPath) throws IOException { -// csvFile = new File(csvPath); -// -// headers.put("courseName", 0); -// headers.put("course instructor", 1); -// headers.put("tasks?", 2); -// -// if (csvFile.length() == 0) { -// saveCourse(); -// } else { -// -// BufferedReader reader = new BufferedReader(new FileReader(csvFile)); -// reader.readLine(); // skip the header -// -// String row; -// while ((row = reader.readLine()) != null) { -// String[] col = row.split(","); -// String courseName = String.valueOf(col[headers.get("courseName")]); -// String courseInstructor = String.valueOf(col[headers.get("courseInstructor")]); -// ArrayList tasks = new ArrayList(); // idk what im doing -//// /* String.valueOf(col[headers.get("tasks?")]); */ -// courseCreationDsRequestModel course = new courseCreationDsRequestModel(courseName, courseInstructor, tasks); -// courses.put(courseName, course); -// } -// -// reader.close(); -// -// } -// -// } -// -// /** -// * Add requestModel to storage -// * @param requestModel the user information to save -// */ -// @Override -// public void saveCourse(courseCreationDsRequestModel requestModel) { -// courses.put(requestModel.getCourseName() ,requestModel); -// this.saveCourse(); -// } -// -// private void saveCourse() { -// BufferedWriter writer; -// try { -// writer = new BufferedWriter(new FileWriter(csvFile)); -// writer.write(String.join(",", headers.keySet())); -// writer.newLine(); -// -// for (courseCreationDsRequestModel course : courses.values()) { -// String line = "%s,%s,%s".format( -// course.getCourseName(), course.getCourseInstructor(), course.getTasks()); -// writer.write(line); -// writer.newLine(); -// } -// -// writer.close(); -// } catch (IOException e) { -// throw new RuntimeException(e); -// } -// -// } -// -// @Override -// public boolean existsByCourseID(String identifier) { -// return courses.containsKey(identifier); -// } -// -//} + public Map getCourses() { + return this.courses; + } +} \ No newline at end of file diff --git a/src/test/java/CourseCreationInteractorTest.java b/src/test/java/CourseCreationInteractorTest.java new file mode 100644 index 0000000..5b72159 --- /dev/null +++ b/src/test/java/CourseCreationInteractorTest.java @@ -0,0 +1,9 @@ +import org.junit.Test; + +public class CourseCreationInteractorTest { + + @Test + void create() { + + } +} diff --git a/src/test/java/CourseUnitTest.java b/src/test/java/CourseUnitTest.java index 0310aa3..2eb6dd7 100644 --- a/src/test/java/CourseUnitTest.java +++ b/src/test/java/CourseUnitTest.java @@ -8,8 +8,19 @@ public class CourseUnitTest { + + /** + * Tests needed: + * + * try catch exceptions: + * - name empty, instructor empty, task empty + * - course id already in course map + * - assertThrows(); + * or can just do assertFalse(course.____IsValid()); + */ @Test - void given123Password_whenPasswordIsNotValid_thenIsFalse() { + + void huh() { ArrayList task = new ArrayList(); task.add("task1"); Course course = new Course("csc207", "paulgries", task);