-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
129 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import org.junit.Test; | ||
|
||
public class CourseCreationInteractorTest { | ||
|
||
@Test | ||
void create() { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters