summaryrefslogtreecommitdiff
path: root/src/main/java/design/model/course_search/CurrentSearchQuery.java
blob: 0a492b33510891dad96210a3c22d61860dc25369 (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
package design.model.course_search;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import design.persistence.MasterDatabase;

/* 
 * Represents the state of our current search.
 */
public class CurrentSearchQuery {
    public static final CurrentSearchQuery INSTANCE = new CurrentSearchQuery();
    

    MasterDatabase db = MasterDatabase.INSTANCE;
    private CourseList query = db.getCourseList();
    private final List<CourseSorter> filters = new ArrayList<CourseSorter>();

    // add a new filter
    public void addFilter(CourseSorter filter)
    {
        filters.add(filter);
    }

    // clear the filters
    public void clearFilters()
    {
        filters.clear();
    }

    // print out the filters ( this is for the model checking used filters )
    public String printFilters()
    {
        String filterResult = "";

        // no filters? let the user know.
        if(filters.size() == 0)
        {
            return "nothing";
        }

        for( CourseSorter f : filters)
        {
            filterResult += f.toString() + " --> ";
        }

        // very silly way of removing the last arrow from our filter list. it's kind of dumb but it works fine.
        filterResult = filterResult.substring(0, filterResult.length() - 5); 
        filterResult += "\n";

        return filterResult;
    }

    // get all the filters
    public List<CourseSorter> getFilters()
    {
        return filters;
    }

    // get our current query.
    public CourseList getQueryResult()
    {
        return query;
    }

    public void search(String searchQuery)
    {
        List<ICourse> courses;

        // compare to our search query.
        courses = query.getCourses().stream()
                            .filter(s -> s != null && s.toString().toLowerCase().contains(searchQuery))
                            .collect(Collectors.toList());

        // TO DO: Filter this based on our current filters, then return it!
        
        query.setCourses(courses);
    }
}