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 f1764a8f52d48d8ce011d700a1024cf6fb86c147
parent dcde1fddf28b01b4f9015a7cc82f2ed353d0217e
Author: jamisonv93 <47033011+jamisonv93@users.noreply.github.com>
Date:   Mon, 16 Mar 2020 20:07:30 -0400

Jamison (#39)

* rebuild DB with included Fall 2020 files

* Change default size of primary view; addLoadSelectedCourses() to switchSemester() in CoursesController

* fix addSelectedCourse & getSelectedCourses; delete User Model

* minor changes, style guide, comments

* add Semester model

* small change in Semester class

* small change in Semester class

* use models in controller methods to handle data; add constraint to prevent selected given class more than once

* reset Course Combo Box after selecting cousre or switching combo box

* Fix trailing whitespace issue Admin.generateCourseList

* remove unnecessary occurences of trim(); fix removeCourse bug

* refactor Semester class
Diffstat:
MScheduleCreator/Admin.java | 4+++-
MScheduleCreator/App.java | 10++--------
MScheduleCreator/Translator.java | 53++++++++++++++++++++++++++++++++---------------------
MScheduleCreator/controllers/CoursesController.java | 58+++++++++++++++++++++++++++++++++++++++++-----------------
MScheduleCreator/controllers/PrimaryController.java | 53++++++++++++++++++++++++++---------------------------
MScheduleCreator/models/Course.java | 13+++++++++----
AScheduleCreator/models/Semester.java | 135+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 248 insertions(+), 78 deletions(-)

diff --git a/ScheduleCreator/Admin.java b/ScheduleCreator/Admin.java @@ -20,6 +20,8 @@ import java.util.regex.Pattern; * pull out hardcoded paths - * * @author Nick Econopouly, Jamison Valentine, Ilyass Sfar + * + * Last Updated: 3/16/2020 */ public class Admin { @@ -152,7 +154,7 @@ public class Admin { if (m.matches()) { course = m.group(1); if (!allCourses.contains(m.group(1))) { - allCourses.add(m.group(3) + " - " + m.group(1)); + allCourses.add(m.group(3) + " - " + m.group(1).trim()); } } } diff --git a/ScheduleCreator/App.java b/ScheduleCreator/App.java @@ -5,21 +5,15 @@ package ScheduleCreator; * * @author Jamison Valentine * - * Last Updated: 2/17/2020 + * Last Updated: 3/16/2020 */ + import java.io.IOException; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.application.Application; -import javafx.event.ActionEvent; -import javafx.event.EventHandler; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; -import javafx.scene.control.Button; -import javafx.scene.layout.StackPane; import javafx.stage.Stage; - public class App extends Application { /** diff --git a/ScheduleCreator/Translator.java b/ScheduleCreator/Translator.java @@ -7,6 +7,7 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -25,7 +26,7 @@ import java.util.regex.Pattern; public class Translator { // this is in the working directory, not the .jar - protected static File selectedCourseFile = new File("user_selected_courses.txt"); + protected static File selectedCourseFile; /** * the type of data requested about a section @@ -69,7 +70,7 @@ public class Translator { * a leading /) * @return the fulltext as a String */ - + protected static String getFullText(String _resourceName) throws FileNotFoundException, IOException { String path = "resources/" + _resourceName; String content; @@ -172,42 +173,50 @@ public class Translator { * @param _course * @throws Exception */ - public static void saveCourse(String _course, String _semester) throws Exception { + + public static void saveCourse(String _course, String _semester) { + //Adds new selected course to new line. + selectedCourseFile = new File(_semester + "_selected_courses.txt"); try ( //Open file to add new classes. FileWriter output = new FileWriter(new File(_semester + "_selected_courses.txt"), true)) { //Adds new selected course to new line. output.append(_course + "\n"); } + catch (Exception ex) { + System.out.println("Course was not saved succesfully"); + } } /** * Removes the selected course from the database. * - * @param _course + * @param _course, _semester * @throws Exception */ - public static void removeCourse(String _course) throws Exception { + public static void removeCourse(String _course, String _semester) { - Scanner input = new Scanner(selectedCourseFile); - StringBuilder newContents = new StringBuilder(); - String line = ""; + selectedCourseFile = new File(_semester + "_selected_courses.txt"); - /** - * Gets all of the courses except the selected one and appends to a new - * file to be saved. * - */ - while (input.hasNext()) { - line = input.nextLine(); - - if (!line.contains(_course)) { - newContents.append(_course).append('\n'); + try { + List<String> courses = Files.readAllLines(selectedCourseFile.toPath()); + for (String course : courses) { + if (course.equals(_course)) { + courses.remove(course); + break; + } } + try ( FileWriter writer = new FileWriter(selectedCourseFile)) { + for (String course : courses) { + writer.append(course + "\n"); + } + writer.close(); + } + catch (Exception ex) {} } - try ( FileWriter writer = new FileWriter(selectedCourseFile)) { - writer.append(newContents.toString()); + catch (IOException ex) { } } @@ -227,11 +236,13 @@ public class Translator { String line; while (input.hasNext()) { line = input.nextLine(); - selectedCourses.add(line.trim()); + selectedCourses.add(line); } } catch (FileNotFoundException ex) { - System.out.println(_semester + "user_selected_courses.txt file does not exist:"); + + System.out.println(_semester + "user_selected_courses.txt file does not exist yet (no selected courses for semester)"); + } finally { return selectedCourses; diff --git a/ScheduleCreator/controllers/CoursesController.java b/ScheduleCreator/controllers/CoursesController.java @@ -1,6 +1,7 @@ package ScheduleCreator.controllers; import ScheduleCreator.Translator; +import ScheduleCreator.models.Semester; import java.io.IOException; import java.net.URL; import java.util.ArrayList; @@ -37,7 +38,12 @@ public class CoursesController implements Initializable { protected Button courseButton; @FXML protected Button removeCourseButton; - protected String currentSemester; + + protected Semester currentSemester; + protected Semester spring2020 = new Semester("spring2020"); + protected Semester summer2020 = new Semester("summer2020"); + protected Semester fall2020 = new Semester("fall2020"); + @Override public void initialize(URL url, ResourceBundle rb) { @@ -49,23 +55,36 @@ public class CoursesController implements Initializable { } public void addSelectedCourse(ActionEvent _event) throws Exception { - String choice = this.courseComboBox.getValue(); - - //Displays course to be added in console - System.out.println("Course selected: " + choice); - List<String> courseList = new ArrayList(); - courseList.add(choice); - this.selectedCourses.getItems().add(choice); - Translator.saveCourse(choice, formatSemester(this.currentSemester)); + + String selectedCourse = this.courseComboBox.getValue(); + this.courseComboBox.setValue("-"); + + if (selectedCourse != null && selectedCourse != "-") { + + if (currentSemester.addCourse(selectedCourse)) this.selectedCourses.getItems().add(selectedCourse); + } } public void switchSemester(ActionEvent _event) throws Exception { - this.currentSemester = semesterComboBox.getValue(); - clearCalendar(); - clearSectionList(); + String currentSemesterString = semesterComboBox.getValue(); + this.courseComboBox.setValue("-"); + + switch (formatSemester(currentSemesterString)) { - loadAllCourses(this.currentSemester); - loadSelectedCourses(formatSemester(this.currentSemester)); + case "spring2020": + this.currentSemester = spring2020; + break; + case "summer2020": + this.currentSemester = summer2020; + break; + case "fall2020": + this.currentSemester = fall2020; + + break; + } + + loadAllCourses(currentSemesterString); + loadSelectedCourses(this.currentSemester.getName()); } @@ -79,9 +98,12 @@ public class CoursesController implements Initializable { public void removeSelectedCourse(ActionEvent _event) throws Exception { Object itemToRemove = this.selectedCourses.getSelectionModel().getSelectedItem(); - String courseToDelete = (String) itemToRemove; + this.selectedCourses.getItems().remove(itemToRemove); - Translator.removeCourse(courseToDelete); + + String courseToDelete = (String) itemToRemove; + this.currentSemester.removeCourse(courseToDelete.trim()); + } public void loadAllCourses(String _semester) throws Exception { @@ -102,7 +124,9 @@ public class CoursesController implements Initializable { public String formatSemester(String _semester) { //Format current semester to pass as argument in appropriate Translator methods - String[] temp = this.currentSemester.split(" "); + + String[] temp = _semester.split(" "); + String formattedSemester = temp[0].toLowerCase() + temp[1]; return formattedSemester; diff --git a/ScheduleCreator/controllers/PrimaryController.java b/ScheduleCreator/controllers/PrimaryController.java @@ -7,7 +7,6 @@ package ScheduleCreator.controllers; * * Last Updated: 3/16/2020 */ - import javafx.event.ActionEvent; import javafx.fxml.FXMLLoader; import javafx.scene.Node; @@ -20,40 +19,40 @@ import javafx.fxml.Initializable; public class PrimaryController implements Initializable { - public void changeToSelectClasses(ActionEvent _event) throws Exception { + public void changeToSelectClasses(ActionEvent _event) throws Exception { - //new FXML loader and scene for new screen - Parent root = FXMLLoader.load(getClass().getResource("/ScheduleCreator/resources/views/select_courses.fxml")); - Scene classViewScene = new Scene(root); + //new FXML loader and scene for new screen + Parent root = FXMLLoader.load(getClass().getResource("/ScheduleCreator/resources/views/select_courses.fxml")); + Scene classViewScene = new Scene(root); - //Get information from primary stage - Stage window = (Stage)((Node)_event.getSource()).getScene().getWindow(); - window.setScene(classViewScene); - window.setMinHeight(600); - window.setMinWidth(1000); - window.show(); - } + //Get information from primary stage + Stage window = (Stage) ((Node) _event.getSource()).getScene().getWindow(); + window.setScene(classViewScene); + window.setMinHeight(600); + window.setMinWidth(1000); + window.show(); + } - public void backToPrimary(ActionEvent _event) throws Exception { + public void backToPrimary(ActionEvent _event) throws Exception { - Parent root = FXMLLoader.load(getClass().getResource("/ScheduleCreator/resources/views/primary.fxml")); - Scene primaryScene = new Scene(root); + Parent root = FXMLLoader.load(getClass().getResource("/ScheduleCreator/resources/views/primary.fxml")); + Scene primaryScene = new Scene(root); - //Get information from primary stage - Stage window = (Stage)((Node)_event.getSource()).getScene().getWindow(); - window.setScene(primaryScene); - window.show(); - } + //Get information from primary stage + Stage window = (Stage) ((Node) _event.getSource()).getScene().getWindow(); + window.setScene(primaryScene); + window.show(); + } - public void changeToRegistrationScreen(ActionEvent _event) throws Exception { + public void changeToRegistrationScreen(ActionEvent _event) throws Exception { - Parent root = FXMLLoader.load(getClass().getResource("/ScheduleCreator/resources/views/registration_screen.fxml")); - Scene scene = new Scene(root); + Parent root = FXMLLoader.load(getClass().getResource("/ScheduleCreator/resources/views/registration_screen.fxml")); + Scene scene = new Scene(root); - Stage stage = (Stage)((Node)_event.getSource()).getScene().getWindow(); - stage.setScene(scene); - stage.show(); - } + Stage stage = (Stage) ((Node) _event.getSource()).getScene().getWindow(); + stage.setScene(scene); + stage.show(); + } @Override public void initialize(URL url, ResourceBundle rb) { diff --git a/ScheduleCreator/models/Course.java b/ScheduleCreator/models/Course.java @@ -12,12 +12,17 @@ import java.util.ArrayList; public class Course { -protected String abbreviation; -protected String courseNumber; -protected ArrayList<String> sections; + protected final String name; + protected String abbreviation; + protected String courseNumber; + protected ArrayList<String> sections; - public Course() { + public Course(String _name) { + this.name = _name; + } + public String getName() { + return this.name; } public String getabbreviation() { diff --git a/ScheduleCreator/models/Semester.java b/ScheduleCreator/models/Semester.java @@ -0,0 +1,134 @@ +package ScheduleCreator.models; + +import ScheduleCreator.Translator; +import java.util.ArrayList; +import java.util.TreeMap; +import ScheduleCreator.models.Section; +import java.util.List; + +/** + * This class models a semester, which is a collection of sections. + * + * Can be a literal real-world semester, or another collection of sections (like courses the user saves for later). + * + * @author Nick Econopouly, Jamison Valentine + * + * Last Updated: 3/16/2020 + */ + +public class Semester { + + protected final String name; + protected ArrayList<Course> selectedCourses; + protected ArrayList<Section> selectedSections; + protected TreeMap<String,Semester> courseList; + protected Schedule schedule; + + /** + * Methods to implement + * clearCalendar(); + * clearSelectedSection(); + */ + + + /** + * + * @param _name + */ + public Semester(String _name) { + this.name = _name; + setSelectedCourses(); + } + + //WORK IN PROGRESS + public void addSelectedSection(Section _section) { + this.selectedSections.add(_section); + } + + public void generateCourseList() { + // generate the courseList by iterating through the sections. + // this should only be performed on an actual semester (fall 2020, etc.) + // and only after all sections have been imported + } + + public Boolean addCourse(String _course) { + Boolean contains = false; + + for (Course course: this.selectedCourses) { + if (course.getName().equalsIgnoreCase(_course)) { + contains = true; + break; + } + } + + if (!contains) { + this.selectedCourses.add(new Course(_course)); + Translator.saveCourse(_course, this.name); + printCourseNames(); + return true; + } else { + return false; + } + + } + +// ============================== Getters ============================ + + public String getName() { + return this.name; + } + + //WORK IN PROGRESS + public List<Section> getAvailableSections(Course _course) { + ArrayList<Section> list = new ArrayList(); + return list; + } + + public void setSelectedCourses() { + List<String> list = Translator.getSelectedCourses(this.name); + + this.selectedCourses = new ArrayList(); + if (!list.isEmpty()) { + for (String courseName: list) { + this.selectedCourses.add(new Course(courseName)); + } + } + + } + + public void removeCourse(String _course) throws Exception { + Course courseToRemove; + + for (Course course: this.selectedCourses) { + + if (_course.equalsIgnoreCase(course.getName())) { + courseToRemove = course; + this.selectedCourses.remove(courseToRemove); + break; + } + } + + Translator.removeCourse(_course, this.name); + printCourseNames(); + + } + + public List<Course> getSelectedCourses() { + return this.selectedCourses; + } + + public TreeMap<String, Semester> getCourseList() { + return this.courseList; + } + + public void printCourseNames() { + System.out.println(this.name); + StringBuilder content = new StringBuilder(); + + for (Course course: this.selectedCourses) { + System.out.println(course.getName()); + } + System.out.println("\n\n"); + + } +}+ \ No newline at end of file