From 2ef38d86d8b207bc0dad6e3c6b7780abc6f6e82b Mon Sep 17 00:00:00 2001 From: WillemDalton Date: Wed, 8 Oct 2025 21:49:21 -0400 Subject: finished multi filtering --- .../controller/userinput/menus/CourseSearch.java | 96 ++++++++++++++++++++++ .../controller/userinput/menus/FiltersMenu.java | 4 +- .../controller/userinput/menus/SearchMenu.java | 5 +- .../controller/userinput/menus/SelectCourse.java | 91 -------------------- .../model/course_search/CurrentSearchQuery.java | 77 ++++++++++++++++- 5 files changed, 177 insertions(+), 96 deletions(-) create mode 100644 src/main/java/design/controller/userinput/menus/CourseSearch.java delete mode 100644 src/main/java/design/controller/userinput/menus/SelectCourse.java diff --git a/src/main/java/design/controller/userinput/menus/CourseSearch.java b/src/main/java/design/controller/userinput/menus/CourseSearch.java new file mode 100644 index 0000000..6bba2ef --- /dev/null +++ b/src/main/java/design/controller/userinput/menus/CourseSearch.java @@ -0,0 +1,96 @@ +package design.controller.userinput.menus; + +import design.controller.userinput.Menu; +import design.controller.userinput.MenuOption; +import design.runtime.*; +import design.model.*; +import design.model.course_search.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +/* + * The actual SEARCH feature of course searching. + */ +public class CourseSearch extends Menu { + CurrentSearchQuery query = CurrentSearchQuery.INSTANCE; + + @Override + public String getTitle() { + return "select course"; + } + + /* + * Prompt for input and search. + */ + public void search() + { + System.out.print("Enter search term (blank for all): "); + Scanner sc = new Scanner(System.in); + String searchTerm = sc.nextLine(); + + // search and present + query.search(searchTerm); + this.present(); + + // reset the query after we're done. + query.reset(); + } + + /* + * Display the results of our search. + */ + @Override + public List getMenuOptions() + { + var l = new ArrayList(); + List queryResult = query.getQueryResult().getCourses(); + + // 0 - return to main menu + l.add(new MenuOption("return to main menu", (a) -> new MainMenu().present())); + + // if we find no results, let the user know. + if (queryResult.isEmpty()) + { + System.out.println("\nNo matching courses found.\n"); + } + + // traverse the course list tree and add menu options for each leaf (course) + addCoursesRecursive(l, query.getQueryResult()); + return l; + } + + + // recursively go through tree structure of courselist to make menu options. + // this is all for displaying the menu options, not the actual sorting. + private void addCoursesRecursive(List menuOptions, CourseList list) + { + for (ICourse icourse : list.getCourses()) + { + // if we find a leaf (course), display it as a menu option + if (icourse instanceof Course c) + { + menuOptions.add(new MenuOption( c.getName() + ", " + c.getLocation() + ", Difficulty: " + c.getDifficultyRating() + ", " + c.getHoleCount() + " holes, " + c.getTotalPar() + " total par", + (a) -> { + Golfer currentGolfer = Session.getCurrentGolfer(); + if(currentGolfer == null) + { + // if we aren't logged in, notify the user. + System.out.println("\n\n !!! log into a golfer account to add courses to your profile. !!! \n\n"); + new MainMenu().present(); + } + + currentGolfer.addCourse(c); + System.out.println("\n Course added to profile. \n"); + } + )); + } + // if not, we need to traverse another courselist + else if (icourse instanceof CourseList sublist) + { + addCoursesRecursive(menuOptions, sublist); + } + } + } +} diff --git a/src/main/java/design/controller/userinput/menus/FiltersMenu.java b/src/main/java/design/controller/userinput/menus/FiltersMenu.java index 3a2463f..724f6a3 100644 --- a/src/main/java/design/controller/userinput/menus/FiltersMenu.java +++ b/src/main/java/design/controller/userinput/menus/FiltersMenu.java @@ -8,8 +8,10 @@ import java.util.List; import design.model.course_search.CurrentSearchQuery; +/* + * Tracks the user's current filters on their search. + */ public class FiltersMenu extends Menu { - CurrentSearchQuery query = CurrentSearchQuery.INSTANCE; @Override diff --git a/src/main/java/design/controller/userinput/menus/SearchMenu.java b/src/main/java/design/controller/userinput/menus/SearchMenu.java index 9411b02..198e254 100644 --- a/src/main/java/design/controller/userinput/menus/SearchMenu.java +++ b/src/main/java/design/controller/userinput/menus/SearchMenu.java @@ -9,6 +9,9 @@ import design.model.course_search.SortByPar; import java.util.List; +/* + * The main control panel for course searching. + */ public class SearchMenu extends Menu { @Override @@ -20,7 +23,7 @@ public class SearchMenu extends Menu { public List getMenuOptions() { return List.of( new MenuOption("return to main menu", (a) -> new MainMenu().present()), - new MenuOption("search...", (a) -> new SelectCourse().search()), + new MenuOption("search...", (a) -> new CourseSearch().search()), new MenuOption("add difficulty filter...", (a) -> new FiltersMenu().addFilter(new SortByDifficulty())), new MenuOption("add hole count filter...", (a) -> new FiltersMenu().addFilter(new SortByHoles())), new MenuOption("add location filter...", (a) -> new FiltersMenu().addFilter(new SortByLocation())), diff --git a/src/main/java/design/controller/userinput/menus/SelectCourse.java b/src/main/java/design/controller/userinput/menus/SelectCourse.java deleted file mode 100644 index 0a57ab6..0000000 --- a/src/main/java/design/controller/userinput/menus/SelectCourse.java +++ /dev/null @@ -1,91 +0,0 @@ -package design.controller.userinput.menus; - -import design.controller.userinput.Menu; -import design.controller.userinput.MenuOption; -import design.runtime.*; -import design.model.*; -import design.model.course_search.*; - -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - -public class SelectCourse extends Menu { - CurrentSearchQuery query = CurrentSearchQuery.INSTANCE; - - @Override - public String getTitle() { - return "select course"; - } - - /* - * Prompt for input and search. - */ - public void search() - { - System.out.print("Enter search term (blank for all): "); - Scanner sc = new Scanner(System.in); - String searchTerm = sc.nextLine(); - - // reset the query so we aren't trying to search on an already searched query - query.reset(); - - // search and present - query.search(searchTerm); - this.present(); - } - - /* - * Display the results of our search. - */ - @Override - public List getMenuOptions() - { - var l = new ArrayList(); - List queryResult = query.getQueryResult().getCourses(); - - // 0 - return to main menu - l.add(new MenuOption("return to main menu", (a) -> new MainMenu().present())); - - // if we find no results, let the user know. - if (queryResult.isEmpty()) - { - System.out.println("\nNo matching courses found.\n"); - } - - // traverse the course list tree and add menu options for each leaf (course) - addCoursesRecursive(l, query.getQueryResult()); - return l; - } - - - // recursively go through tree structure of courselist to make menu options - private void addCoursesRecursive(List menuOptions, CourseList list) - { - for (ICourse icourse : list.getCourses()) - { - // if we find a leaf (course), display it as a menu option - if (icourse instanceof Course c) - { - menuOptions.add(new MenuOption( c.getName() + ", " + c.getLocation() + ", Difficulty: " + c.getDifficultyRating() + ", " + c.getHoleCount() + " holes", - (a) -> { - Golfer currentGolfer = Session.getCurrentGolfer(); - if(currentGolfer == null) - { - System.out.println("\n\n !!! log into a golfer account to add courses to your profile. !!! \n\n"); - new MainMenu().present(); - } - - currentGolfer.addCourse(c); - System.out.println("\n Course added to profile. \n"); - } - )); - } - // if not, we need to traverse another courselist - else if (icourse instanceof CourseList sublist) - { - addCoursesRecursive(menuOptions, sublist); - } - } - } -} diff --git a/src/main/java/design/model/course_search/CurrentSearchQuery.java b/src/main/java/design/model/course_search/CurrentSearchQuery.java index c00eb86..e1f52ce 100644 --- a/src/main/java/design/model/course_search/CurrentSearchQuery.java +++ b/src/main/java/design/model/course_search/CurrentSearchQuery.java @@ -17,12 +17,10 @@ public class CurrentSearchQuery { private CourseList query = db.getCourseList(); private final List filters = new ArrayList(); - // reset the query public void reset() { query = db.getCourseList(); - clearFilters(); } // add a new filter @@ -79,6 +77,79 @@ public class CurrentSearchQuery { .filter(s -> s.toString().toLowerCase().contains(searchQuery.toLowerCase())) .collect(Collectors.toList()); - query.setCourses(courses); + // and now, we filter it! + CourseList filtered = applyFilters(filters, courses); + query.setCourses(filtered.getCourses()); + } + + // to apply our filters we need to traverse the tree and make new grouping as we go. + public CourseList applyFilters(List filters, List coursesToFilter) + { + CourseList root = new CourseList(); + root.setCourses(applyFiltersRecursive(coursesToFilter, filters, 0)); + return root; + } + + // the actual recursive part. Level is how many filters deep we are. + private List applyFiltersRecursive(List courses, List filters, int level) + { + // base case. we have gone past all filters or theres only one course in this list. already sorted! + if (level >= filters.size() || courses.size() <= 1) + { + return courses; + } + + // grab out current sorting strategy for this level, and sort the courselist. + CourseSorter sorter = filters.get(level); + sorter.sortCourses(courses); + + // the resulting sorted list, with new groupings + List result = new ArrayList<>(); + + // courses with an equal value. + List currentGroup = new ArrayList<>(); + + ICourse prev = null; + + // run through the courses, if + for (ICourse c : courses) + { + /* always add the first course to a new group. when iterating through courses, if we have to values that are equal, we need to add them into a group together. + * think about it this way. If we have [ 1, 2, 2, 2, 3, 4 ]. 1 is first, so its in it's own group. 2 /= 1, so 2 gets its own group. + * now do 2 again. we add it to the existing group 2. Same with the next 2. Now 3. 3 /= 2, so we put it in it's own group. + */ + if (prev == null || !sorter.isEqual(prev, c)) + { + // already a course in that group? we now have two equal values and so + if (!currentGroup.isEmpty()) + { + result.add(makeGroup(currentGroup, filters, level)); + currentGroup = new ArrayList<>(); + } + } + + currentGroup.add(c); + prev = c; + } + + // handle the last group. + if (!currentGroup.isEmpty()) + { + result.add(makeGroup(currentGroup, filters, level)); + } + + return result; } + + // make a CourseList group, a sublist of a group, and filter it. + private ICourse makeGroup(List group, List filters, int level) + { + // base case, group only has one course in it (already sorted) + if (group.size() == 1) return group.get(0); + + // group has more than 1 course in it, it needs to be sorted more if possible. + CourseList subList = new CourseList(); + subList.setCourses(applyFiltersRecursive(group, filters, level + 1)); + return subList; + } } -- cgit v1.2.3