summaryrefslogtreecommitdiff
path: root/src/main/java/design/controller/userinput/menus/CourseSearch.java
diff options
context:
space:
mode:
authorTyler Ferrari <69283684+Sowgro@users.noreply.github.com>2025-10-09 08:14:57 -0400
committerGitHub <noreply@github.com>2025-10-09 08:14:57 -0400
commitf115308210fd98e6b6f83f8c091ca72dbfb666fb (patch)
treebb2199639272496e79e79e221513b5346f553084 /src/main/java/design/controller/userinput/menus/CourseSearch.java
parentbf6b01e5005618e6ccdfb4217311a8f94dd5a0dd (diff)
parentddcfcf82baf737e183ec7b00edeee26894516c58 (diff)
downloaddesignproject-design-6-f115308210fd98e6b6f83f8c091ca72dbfb666fb.tar.gz
designproject-design-6-f115308210fd98e6b6f83f8c091ca72dbfb666fb.tar.bz2
designproject-design-6-f115308210fd98e6b6f83f8c091ca72dbfb666fb.zip
Merge pull request #10 from RIT-SWEN-262/course-search
Course Search Menu
Diffstat (limited to 'src/main/java/design/controller/userinput/menus/CourseSearch.java')
-rw-r--r--src/main/java/design/controller/userinput/menus/CourseSearch.java104
1 files changed, 104 insertions, 0 deletions
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..019965c
--- /dev/null
+++ b/src/main/java/design/controller/userinput/menus/CourseSearch.java
@@ -0,0 +1,104 @@
+package design.controller.userinput.menus;
+
+import design.controller.userinput.Menu;
+import design.controller.userinput.MenuOption;
+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;
+import java.util.List;
+import java.util.Scanner;
+
+/*
+ * The actual SEARCH feature of course searching.
+ */
+public class CourseSearch extends Menu {
+ CurrentSearchQuery query = CurrentSearchQuery.INSTANCE;
+ PersonalDatabase GolferDB = PersonalDatabase.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<MenuOption> getMenuOptions() {
+ var l = new ArrayList<MenuOption>();
+ List<ICourse> 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()) {
+ 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<MenuOption> 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) {
+ 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 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 {
+ GolferDB.updateGolfer(currentGolfer);
+ } 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();
+ }
+ ));
+ }
+ // if not, we need to traverse another courselist
+ else if (icourse instanceof CourseList sublist) {
+ addCoursesRecursive(menuOptions, sublist);
+ }
+ }
+ }
+}