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 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/main/java/design/controller/userinput/menus/CourseSearch.java (limited to 'src/main/java/design/controller/userinput/menus/CourseSearch.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); + } + } + } +} -- cgit v1.2.3 From 88564fc1e48ec1efbbfc91e99339b781146a9a75 Mon Sep 17 00:00:00 2001 From: WillemDalton Date: Wed, 8 Oct 2025 22:05:47 -0400 Subject: added the ability for users to save courses to their profile --- .../design/controller/userinput/menus/CourseSearch.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/main/java/design/controller/userinput/menus/CourseSearch.java') diff --git a/src/main/java/design/controller/userinput/menus/CourseSearch.java b/src/main/java/design/controller/userinput/menus/CourseSearch.java index 6bba2ef..7170b5d 100644 --- a/src/main/java/design/controller/userinput/menus/CourseSearch.java +++ b/src/main/java/design/controller/userinput/menus/CourseSearch.java @@ -5,7 +5,9 @@ import design.controller.userinput.MenuOption; import design.runtime.*; import design.model.*; import design.model.course_search.*; +import design.persistence.PersonalDatabase; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; @@ -15,6 +17,7 @@ import java.util.Scanner; */ public class CourseSearch extends Menu { CurrentSearchQuery query = CurrentSearchQuery.INSTANCE; + PersonalDatabase GolferDB = PersonalDatabase.INSTANCE; @Override public String getTitle() { @@ -81,8 +84,19 @@ public class CourseSearch extends Menu { new MainMenu().present(); } + // add the course, try to save to DB. currentGolfer.addCourse(c); + try + { + GolferDB.updateGolfer(currentGolfer); + } + catch(IOException e) + { + System.out.println(e); // not sure if we should format this prettier for the user if the DB fails. + } + System.out.println("\n Course added to profile. \n"); + new MainMenu().present(); } )); } -- cgit v1.2.3 From 7623a5c02f6da679016bf8f8671d4fb29c5fdfbb Mon Sep 17 00:00:00 2001 From: sowgro Date: Wed, 8 Oct 2025 23:45:07 -0400 Subject: update for action interface change and cleanup string --- src/main/java/design/controller/userinput/menus/CourseSearch.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/main/java/design/controller/userinput/menus/CourseSearch.java') diff --git a/src/main/java/design/controller/userinput/menus/CourseSearch.java b/src/main/java/design/controller/userinput/menus/CourseSearch.java index 7170b5d..b9e29bd 100644 --- a/src/main/java/design/controller/userinput/menus/CourseSearch.java +++ b/src/main/java/design/controller/userinput/menus/CourseSearch.java @@ -51,7 +51,7 @@ public class CourseSearch extends Menu { List queryResult = query.getQueryResult().getCourses(); // 0 - return to main menu - l.add(new MenuOption("return to main menu", (a) -> new MainMenu().present())); + l.add(new MenuOption("return to main menu", () -> new MainMenu().present())); // if we find no results, let the user know. if (queryResult.isEmpty()) @@ -74,8 +74,9 @@ public class CourseSearch extends Menu { // 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) -> { + var name = String.format("%s, %s, Difficulty: %s, %s holes, %s total par", + c.getName(), c.getLocation(), c.getDifficultyRating(), c.getHoleCount(), c.getTotalPar()); + menuOptions.add(new MenuOption(name, () -> { Golfer currentGolfer = Session.getCurrentGolfer(); if(currentGolfer == null) { -- cgit v1.2.3 From ddcfcf82baf737e183ec7b00edeee26894516c58 Mon Sep 17 00:00:00 2001 From: sowgro Date: Thu, 9 Oct 2025 08:06:56 -0400 Subject: fix formatting --- .../controller/userinput/menus/CourseSearch.java | 53 ++++++++++------------ 1 file changed, 23 insertions(+), 30 deletions(-) (limited to 'src/main/java/design/controller/userinput/menus/CourseSearch.java') diff --git a/src/main/java/design/controller/userinput/menus/CourseSearch.java b/src/main/java/design/controller/userinput/menus/CourseSearch.java index b9e29bd..019965c 100644 --- a/src/main/java/design/controller/userinput/menus/CourseSearch.java +++ b/src/main/java/design/controller/userinput/menus/CourseSearch.java @@ -2,10 +2,13 @@ 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 design.model.Course; +import design.model.Golfer; +import design.model.course_search.CourseList; +import design.model.course_search.CurrentSearchQuery; +import design.model.course_search.ICourse; import design.persistence.PersonalDatabase; +import design.runtime.Session; import java.io.IOException; import java.util.ArrayList; @@ -27,8 +30,7 @@ public class CourseSearch extends Menu { /* * Prompt for input and search. */ - public void search() - { + public void search() { System.out.print("Enter search term (blank for all): "); Scanner sc = new Scanner(System.in); String searchTerm = sc.nextLine(); @@ -45,21 +47,19 @@ public class CourseSearch extends Menu { * Display the results of our search. */ @Override - public List getMenuOptions() - { + 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", () -> new MainMenu().present())); - + // if we find no results, let the user know. - if (queryResult.isEmpty()) - { + if (queryResult.isEmpty()) { System.out.println("\nNo matching courses found.\n"); } - // traverse the course list tree and add menu options for each leaf (course) + // traverse the course list tree and add menu options for each leaf (course) addCoursesRecursive(l, query.getQueryResult()); return l; } @@ -67,43 +67,36 @@ public class CourseSearch extends Menu { // 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()) - { + 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) - { + if (icourse instanceof Course c) { var name = String.format("%s, %s, Difficulty: %s, %s holes, %s total par", c.getName(), c.getLocation(), c.getDifficultyRating(), c.getHoleCount(), c.getTotalPar()); menuOptions.add(new MenuOption(name, () -> { Golfer currentGolfer = Session.getCurrentGolfer(); - if(currentGolfer == null) - { + 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(); + return; } // add the course, try to save to DB. currentGolfer.addCourse(c); - try - { + try { GolferDB.updateGolfer(currentGolfer); - } - catch(IOException e) - { - System.out.println(e); // not sure if we should format this prettier for the user if the DB fails. + } catch (IOException e) { + e.printStackTrace(); // not sure if we should format this prettier for the user if the DB fails. } System.out.println("\n Course added to profile. \n"); - new MainMenu().present(); + new MainMenu().present(); } - )); - } + )); + } // if not, we need to traverse another courselist - else if (icourse instanceof CourseList sublist) - { + else if (icourse instanceof CourseList sublist) { addCoursesRecursive(menuOptions, sublist); } } -- cgit v1.2.3