Schedule-Creator

Java GUI to create schedules for UNCG students
git clone git://git.wrycode.com/wrycode/archive/Schedule-Creator.git
Log | Files | Refs | README

commit 2bb5b0d7828e0803c79d5fa5237c9d846a7befa9
parent 5c950330db4c02b9d5c8c6031f674f3b8d72c1b1
Author: toldge <47599909+toldge@users.noreply.github.com>
Date:   Tue,  7 Apr 2020 18:23:50 -0400

Toldge adapter (#55)

* add some methods for Section and Schedule

* Add Adapter and change all calls of Translator to Adapter

* fix a small catch block error I made

Co-authored-by: Toldge <ntolodziecki@gmail.com>
Diffstat:
AScheduleCreator/Adapter.java | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MScheduleCreator/Tests.java | 13+++++++------
MScheduleCreator/Translator.java | 48++++++++++++++++++++++++++----------------------
AScheduleCreator/TranslatorInterface.java | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MScheduleCreator/controllers/CoursesController.java | 8+++++---
MScheduleCreator/models/Course.java | 5+++--
MScheduleCreator/models/Schedule.java | 3++-
MScheduleCreator/models/Section.java | 3++-
MScheduleCreator/models/Semester.java | 13+++++++------
9 files changed, 181 insertions(+), 41 deletions(-)

diff --git a/ScheduleCreator/Adapter.java b/ScheduleCreator/Adapter.java @@ -0,0 +1,60 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package ScheduleCreator; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.List; + +/** + * + * @author Nathan Tolodziecki <Nathan Tolodziecki> + */ +public class Adapter implements TranslatorInterface { + + final protected TranslatorInterface translator = new Translator(); + + @Override + public List<String> getSections(String _courseNumber, String _semester) { + return this.translator.getSections(_courseNumber, _semester); + } + + @Override + public String getFullText(String _resourceName) throws FileNotFoundException, IOException { + return this.translator.getFullText(_resourceName); + } + + @Override + public List<String> getCourses(String _semesterName) { + return this.translator.getCourses(_semesterName); + } + + @Override + public String getSectionInfo(Translator.choice _choice, String _semesterName, String _section) throws FileNotFoundException, IOException { + return this.translator.getSectionInfo(_choice, _semesterName, _section); + } + + @Override + public void saveCourse(String _course, String _semester) { + this.translator.saveCourse(_course, _semester); + } + + @Override + public void removeCourse(String _course, String _semester) { + this.translator.removeCourse(_course, _semester); + } + + @Override + public List<String> getSelectedCourses(String _semester) { + return this.translator.getSelectedCourses(_semester); + } + + @Override + public List<String> getSemesters() throws FileNotFoundException, IOException { + return this.translator.getSemesters(); + } + +} diff --git a/ScheduleCreator/Tests.java b/ScheduleCreator/Tests.java @@ -12,9 +12,10 @@ import com.mailjet.client.errors.MailjetException; import com.mailjet.client.errors.MailjetSocketTimeoutException; import java.io.IOException; import java.util.List; -import java.util.logging.Logger; public class Tests { + + protected static Adapter adapter = new Adapter(); public static void main(String[] args) throws IOException, MailjetException, MailjetSocketTimeoutException { @@ -29,7 +30,7 @@ public class Tests { public static void testSemester() throws IOException { // Example usage of DBAdapter - List<String> semesters = Translator.getSemesters(); + List<String> semesters = new Translator().getSemesters(); System.out.println("Current Semesters are:"); for (int i = 0; i < semesters.size(); i++) { @@ -40,7 +41,7 @@ public class Tests { String semester = semesters.get(0); // get courses - List<String> courses = Translator.getCourses(semester); + List<String> courses = Tests.adapter.getCourses(semester); // example course from the semester String exampleCourse = courses.get(20); @@ -48,15 +49,15 @@ public class Tests { System.out.println("Example course is: " + exampleCourse); // dummy method - we still need to implement this (I think?) - List<String> sections = Translator.getSections(exampleCourse, semester); + List<String> sections = new Translator().getSections(exampleCourse, semester); String section = sections.get(0); //should return real info for CSC 250 - 01 System.out.println("Building for " + section + " is: "); - System.out.println(Translator.getSectionInfo(Translator.choice.BUILDING, semester, section)); + System.out.println(new Translator().getSectionInfo(Translator.choice.BUILDING, semester, section)); System.out.println("CRN for " + section + " is: "); - System.out.println(Translator.getSectionInfo(Translator.choice.CRN, semester, section)); + System.out.println(new Translator().getSectionInfo(Translator.choice.CRN, semester, section)); } } diff --git a/ScheduleCreator/Translator.java b/ScheduleCreator/Translator.java @@ -23,7 +23,7 @@ import java.util.regex.Pattern; * * Last Updated: 3/17/2020 */ -public class Translator { +public class Translator implements TranslatorInterface { // this is in the working directory, not the .jar protected static File selectedCourseFile; @@ -45,24 +45,24 @@ public class Translator { * in all other DBAdapter methods * @throws java.io.FileNotFoundException */ - public static List<String> getSemesters() throws FileNotFoundException, IOException { + public List<String> getSemesters() throws FileNotFoundException, IOException { String path = "DB/semester_list"; - String contents = Translator.getFullText(path); + String contents = new Translator().getFullText(path); List<String> semesters = Arrays.asList(contents.split("\n")); return semesters; } - // DUMMY - public static List<String> getSections(String _courseNumber, String _semester) { + @Override + public List<String> getSections(String _courseNumber, String _semester) { String path = "DB/" + _semester + "/all_info"; ArrayList<String> sections = new ArrayList(); try { - String content = Translator.getFullText(path); + String content = new Translator().getFullText(path); Scanner input = new Scanner(content).useDelimiter("\n"); String line = ""; @@ -86,8 +86,8 @@ public class Translator { * a leading /) * @return the fulltext as a String */ - - protected static String getFullText(String _resourceName) throws FileNotFoundException, IOException { + @Override + public String getFullText(String _resourceName) throws FileNotFoundException, IOException { String path = "resources/" + _resourceName; String content; try ( InputStream stream = Translator.class.getResourceAsStream(path); InputStreamReader reader = new InputStreamReader(stream)) { @@ -110,14 +110,15 @@ public class Translator { * @return a List of courses (as Strings) available for the semester * */ - public static List<String> getCourses(String _semesterName) { + @Override + public List<String> getCourses(String _semesterName) { List<String> courses = new ArrayList(); try { - String contents = Translator.getFullText("DB/" + _semesterName + "/courses"); + String contents = new Translator().getFullText("DB/" + _semesterName + "/courses"); courses = Arrays.asList(contents.split("\n")); } - catch (Exception ex) {} + catch (IOException ex) {} return courses; } @@ -131,7 +132,8 @@ public class Translator { * @return requested info as a String * @throws java.io.FileNotFoundException */ - protected static String getSectionInfo(Translator.choice _choice, String _semesterName, String _section) throws FileNotFoundException, IOException { + @Override + public String getSectionInfo(Translator.choice _choice, String _semesterName, String _section) throws FileNotFoundException, IOException { String regex = null; String dataFileType = "all_info"; @@ -157,7 +159,7 @@ public class Translator { } // get fulltext of the data we need - String text = Translator.getFullText("DB/" + _semesterName + "/" + dataFileType); + String text = new Translator().getFullText("DB/" + _semesterName + "/" + dataFileType); // create a List<String> of the lines List<String> lines = Arrays.asList(text.split("\n")); @@ -193,10 +195,10 @@ public class Translator { * database. * * @param _course - * @throws Exception + * @param _semester */ - - public static void saveCourse(String _course, String _semester) { + @Override + public void saveCourse(String _course, String _semester) { //Adds new selected course to new line. selectedCourseFile = new File(_semester + "_selected_courses.txt"); @@ -213,10 +215,11 @@ public class Translator { /** * Removes the selected course from the database. * - * @param _course, _semester - * @throws Exception + * @param _course + * @param _semester */ - public static void removeCourse(String _course, String _semester) { + @Override + public void removeCourse(String _course, String _semester) { selectedCourseFile = new File(_semester + "_selected_courses.txt"); @@ -246,10 +249,11 @@ public class Translator { /** * Returns a list of the selected courses. * - * @return - * @throws Exception + * @param _semester + * @return A List (of Strings) of the selected courses. */ - public static List<String> getSelectedCourses(String _semester) { + @Override + public List<String> getSelectedCourses(String _semester) { ArrayList<String> selectedCourses = new ArrayList(); //Load courses from text file to be returned as a list. diff --git a/ScheduleCreator/TranslatorInterface.java b/ScheduleCreator/TranslatorInterface.java @@ -0,0 +1,69 @@ +package ScheduleCreator; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.List; + +/** + * An interface to allow easier modularity of the adapter. + * + * @author Jamison Valentine, Ilyass Sfar, Nick Econopouly, Nathan Tolodzieki + * Last updated 04/02/2020 + */ +public interface TranslatorInterface { + /** + * + * @return a list of semester names (Strings) that can be used as arguments + * in all other DBAdapter methods + * @throws java.io.FileNotFoundException + */ + abstract List<String> getSemesters() throws FileNotFoundException, IOException; + + abstract List<String> getSections(String _courseNumber, String _semester); + + /** + * utility method to return the full text of the file using classpath + * resource inside the jar. + * + * @param _resourceName name of the file in the resources directory (without + * a leading /) + * @return the fulltext as a String + * @throws java.io.FileNotFoundException + * @throws java.io.IOException + */ + abstract String getFullText(String _resourceName) throws FileNotFoundException, IOException; + + /** + * + * @param _semesterName + * @return a List of courses (as Strings) available for the semester + */ + abstract List<String> getCourses(String _semesterName); + + /** + * Get specific information about a section + * + * @param _choice type of info wanted about section + * @param _semesterName semester name from getSemesters() + * @param _section section name e.g. "CSC 250 - 01" + * @return requested info as a String + * @throws java.io.FileNotFoundException + */ + abstract String getSectionInfo(Translator.choice _choice, String _semesterName, String _section) throws FileNotFoundException, IOException; + + abstract void saveCourse(String _course, String _semester); + + /** + * Removes the selected course from the database. + * + * @param _course, _semester + */ + abstract void removeCourse(String _course, String _semester); + + /** + * Returns a list of the selected courses. + * + * @return A List (of Strings) of the selected courses. + */ + abstract List<String> getSelectedCourses(String _semester); +} diff --git a/ScheduleCreator/controllers/CoursesController.java b/ScheduleCreator/controllers/CoursesController.java @@ -3,10 +3,11 @@ package ScheduleCreator.controllers; /** * This class controls interactions in the Courses View. * - * @author Jamison Valentine, Ilyass Sfar, Nick Econopouly, Nathan Tolodzieki + * @author Jamison Valentine, Ilyass Sfar, Nick Econopouly, Nathan Tolodziecki * * Last Updated: 4/6/2020 */ +import ScheduleCreator.Adapter; import java.io.IOException; import java.net.URL; import java.util.ArrayList; @@ -84,6 +85,7 @@ public class CoursesController implements Initializable { protected Semester currentSemester; protected Course focusedCourse; protected Course currentCourse; + protected Adapter adapter = new Adapter(); protected int NUM_ROWS; protected int NUM_COLS; @@ -101,7 +103,7 @@ public class CoursesController implements Initializable { grid = new BorderPane[NUM_ROWS][NUM_COLS]; this.drawGrid(); this.CRNPane.toFront(); - } catch (IOException ex) { + } catch (Exception ex) { Logger.getLogger(CoursesController.class.getName()).log(Level.SEVERE, null, ex); } } @@ -345,7 +347,7 @@ public class CoursesController implements Initializable { } public void loadSemesters() throws IOException { - List<String> semesters = Translator.getSemesters(); + List<String> semesters = adapter.getSemesters(); List<String> newList = new ArrayList(); Pattern p = Pattern.compile("([a-z]*)([0-9]{4})"); diff --git a/ScheduleCreator/models/Course.java b/ScheduleCreator/models/Course.java @@ -8,7 +8,7 @@ package ScheduleCreator.models; * Last Updated: 3/27/2020 */ -import ScheduleCreator.Translator; +import ScheduleCreator.Adapter; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -21,6 +21,7 @@ public class Course { protected final String abbreviation; protected final String courseNumber; protected List<Section> sections; + protected Adapter adapter = new Adapter(); public Course(String _name, String _semester) { this.fullText = _name; @@ -53,7 +54,7 @@ public class Course { } private void loadSectionsFromFile(String _semester) { - List<String> sectionStrings = Translator.getSections(this.id, _semester); + List<String> sectionStrings = this.adapter.getSections(this.id, _semester); this.sections = new ArrayList(); Pattern p = Pattern.compile(".*([0-9]{5}).*- ([0-9]{2})\\s*(\\S* [ap]m - \\S* [ap]m)\\s*(\\S*)(.*)=(.*)"); diff --git a/ScheduleCreator/models/Schedule.java b/ScheduleCreator/models/Schedule.java @@ -65,4 +65,4 @@ public class Schedule { } return string.toString(); } -} +}+ \ No newline at end of file diff --git a/ScheduleCreator/models/Section.java b/ScheduleCreator/models/Section.java @@ -129,4 +129,4 @@ public class Section { this.endTime = end; } -} +}+ \ No newline at end of file diff --git a/ScheduleCreator/models/Semester.java b/ScheduleCreator/models/Semester.java @@ -1,6 +1,6 @@ package ScheduleCreator.models; -import ScheduleCreator.Translator; +import ScheduleCreator.Adapter; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; @@ -18,10 +18,11 @@ public class Semester { protected final List<String> allCourses; protected List<Schedule> schedules; protected LinkedHashMap<Course, List<Section>> selectedSections; + protected Adapter adapter = new Adapter(); public Semester(String _name) { this.name = _name; - this.allCourses = Translator.getCourses(this.name); + this.allCourses = this.adapter.getCourses(this.name); this.schedules = new ArrayList(); this.selectedSections = new LinkedHashMap(); this.loadSelectedCoursesFromFile(); @@ -49,7 +50,7 @@ public class Semester { public Boolean addCourse(Course _course) { if (!this.selectedSections.keySet().contains(_course)) { this.selectedSections.put(_course, _course.getSections()); - Translator.saveCourse(_course.getFullText(), this.name); + this.adapter.saveCourse(_course.getFullText(), this.name); return true; } return false; @@ -107,7 +108,7 @@ public class Semester { private void loadSelectedCoursesFromFile() { - List<String> list = Translator.getSelectedCourses(this.name); + List<String> list = this.adapter.getSelectedCourses(this.name); if (!list.isEmpty()) { for (String courseName : list) { Course course = new Course(courseName, this.name); @@ -124,7 +125,7 @@ public class Semester { break; } } - Translator.removeCourse(_course, this.name); + this.adapter.removeCourse(_course, this.name); } @Override @@ -151,7 +152,7 @@ public class Semester { } public List<String> getSelectedCourseStrings() { - return Translator.getSelectedCourses(this.name); + return this.adapter.getSelectedCourses(this.name); } public List<Schedule> getSchedules() {