summaryrefslogtreecommitdiff
path: root/src/main/java/design/controller/userinput/menus/CourseSearch.java
blob: 7170b5da49fd62f10b963eb563e9f2a3368a185c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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.persistence.PersonalDatabase;

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", (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<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) 
            {
                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();
                    }

                    // 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(); 
                }
            ));
            } 
            // if not, we need to traverse another courselist
            else if (icourse instanceof CourseList sublist) 
            {
                addCoursesRecursive(menuOptions, sublist);
            }
        }
    }
}