Skip to content

Commit

Permalink
serialization? serialization ;/ ...
Browse files Browse the repository at this point in the history
  • Loading branch information
jltng committed Nov 27, 2022
1 parent 55e8b10 commit 28c055a
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 143 deletions.
2 changes: 1 addition & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Use case layer

import entities.*;
import read_write.CourseReadWrite;
import read_write.TaskReadWrite;

import java.util.ArrayList;

Expand Down Expand Up @@ -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());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/entities/Course.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/read_write/CourseReadWrite.java
Original file line number Diff line number Diff line change
@@ -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<String> courseTasks = new ArrayList<String>();
courseTasks.add("task1");
Course csc207 = new Course("csc207", "paulgries", courseTasks);
CourseMap courses = new CourseMap();
courses.addCourse("csc207paulgries", csc207);
return courses;
}
}
181 changes: 41 additions & 140 deletions src/main/java/screens/FileCourse.java
Original file line number Diff line number Diff line change
@@ -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<String, Integer> headers = new HashMap<>();
private final Map<String, CourseCreationRequestModel> 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<String> tasks = new ArrayList<String>(); // 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<String, CourseCreationRequestModel> 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<String, CourseCreationRequestModel> readFromFile() throws IOException, ClassNotFoundException {
FileInputStream fileReader = new FileInputStream(this.filePath);
ObjectInputStream in = new ObjectInputStream(fileReader);
HashMap<String, CourseCreationRequestModel> courseMap = (HashMap<String, CourseCreationRequestModel>) 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<String, Integer> headers = new HashMap<>();
// private final Map<String, courseCreationDsRequestModel> 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<String> tasks = new ArrayList<String>(); // 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<String, CourseCreationRequestModel> getCourses() {
return this.courses;
}
}
9 changes: 9 additions & 0 deletions src/test/java/CourseCreationInteractorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import org.junit.Test;

public class CourseCreationInteractorTest {

@Test
void create() {

}
}
13 changes: 12 additions & 1 deletion src/test/java/CourseUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> task = new ArrayList<String>();
task.add("task1");
Course course = new Course("csc207", "paulgries", task);
Expand Down

0 comments on commit 28c055a

Please sign in to comment.