-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovieDataBase.java
More file actions
178 lines (147 loc) · 6.04 KB
/
Copy pathMovieDataBase.java
File metadata and controls
178 lines (147 loc) · 6.04 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
public class MovieDatabase {
static ArrayList<Movie> movies = new ArrayList<Movie>();
static ArrayList<Movie> sortedByTitle;
static ArrayList<Movie> sortedByAvgRating;
static ArrayList<Movie> sortedByNumRatings;
static String[] allGenres = {"Action", "Comedy", "Drama", "Horror", "Romance",
"Sci-Fi", "Thriller", "Animation", "Crime", "Documentary"};
static String[] tiers = {"Poor", "Below Avg", "Average", "Good", "Excellent"};
public static void main(String[] args) throws IOException {
try {
// TODO: Add your CSV reading code here
}
catch (Exception e){
e.printStackTrace();
}
System.out.printf("Loaded %d movies%n",movies.size());
sortedByTitle = new ArrayList<Movie>(movies);
sortedByTitle.sort((a, b) -> a.getTitle().compareTo(b.getTitle()));
sortedByAvgRating = new ArrayList<Movie>(movies);
sortByAvgRating(sortedByAvgRating);
sortedByNumRatings = new ArrayList<Movie>(movies);
sortedByNumRatings.sort((a, b) -> b.getNumRatings() - a.getNumRatings());
//some test code here
//System.out.println(getMostPopular(5));
//System.out.println(getTopRated(5,"Action"));
//System.out.println(getAverageRating("Crime"));
//System.out.println(searchByTitle("Lord").getTitle());
//int[][] matrix = buildGenreMatrix();
//printGenreMatrix(matrix);
}
/**
* Sort the given list in descending order by avgRating using selection sort or insertion sort.
* @param movies movies the list of movies to sort
*/
public static void sortByAvgRating(ArrayList<Movie> movies){
}
/**
* Returns an ArrayList<Movie> containing the top n movies of the given genre by average rating.
* @param n number of movies to return
* @param genre the genre to filter by
* @return ArrayList of the top n movies in the given genre sorted by average rating descending
*/
public static ArrayList<Movie> getTopRated(int n, String genre){
ArrayList<Movie> topMovies = new ArrayList<Movie>();
return topMovies;
}
/**
* Returns an ArrayList<Movie> containing all movies that belong to the given genre.
* @param genre the genre to filter by
* @return ArrayList<Movie> containing all movies that belong to the given genre
*/
public static ArrayList<Movie> filterByGenre(String genre){
ArrayList<Movie> filtered = new ArrayList<Movie>();
return filtered;
}
/**
* Returns the average avgRating of all movies belonging to the given genre.
* @param genre the genre to filter by
* @return the average avgRating of all movies belonging to the given genre
*/
public static double getAverageRating(String genre) {
return 0.0;
}
/**
* Returns an ArrayList containing the n movies with the highest number of ratings
* @param n number of movies to return
* @return ArrayList<Movie> containing the n movies with the highest numRatings
*/
public static ArrayList<Movie> getMostPopular(int n){
ArrayList<Movie> mostPopular = new ArrayList<Movie>();
return mostPopular;
}
/**
* Returns the first movie whose title contains the query string (case-insensitive) using a linear search.
* Return null if no match is found.
* @param query movie name, can be partial
* @return first movie that contains query or null if not found
*/
public static Movie searchByTitle(String query){
return null;
}
/**
* A binary search that returns the index of the movie with the matching title
* in the given list, or -1 if not found
* @param movieList list of movies to search
* @param title title of the movie
* @param low lower index
* @param high upper index
* @return index of title or -1 if not found
*/
public static int binarySearchByTitle(ArrayList<Movie> movieList,String title,int low,int high){
return -1;
}
/**
* Helper function for buildGenreMatrix that returns an integer that corresponds to the
* column of a specific rating as show in the README
* 0 is Poor, 1 is Below Average, 2 is Average, 3 is Good, 4 is Excellent
* @param rating average rating of a movie
* @return column number of the rating category
*/
private static int getRatingTier(double rating) {
if (rating < 2.5) return 0;
else if (rating < 3.0) return 1;
else if (rating < 3.5) return 2;
else if (rating < 4.0) return 3;
else return 4;
}
/**
* Creates a matrix of the counts of each rating for a movie in every genre.
* Poor is below 2.5, below average is between 2.5 (inclusive) and 3, average is
* between 3(inclusive) and 3.5, good is between 3.5 (inclusive) and 4, and excellent
* is greater than or equal to 4.
*
* @return a matrix of integer counts for ratings in each category
*/
public static int[][] buildGenreMatrix(){
int numRows = allGenres.length;
int numCols = tiers.length;
int ratings[][] = new int[numRows][numCols];
return ratings;
}
/**
* Prints a formatted table for the provided matrix of tiers by genre
* @param matrix 2D array of genre tier counts
*/
public static void printGenreMatrix(int[][] matrix) {
// print header
System.out.printf("%-15s", "Genre");
for (String tier : tiers) {
System.out.printf("%-12s", tier);
}
System.out.println();
System.out.println("-".repeat(75));
// print each row
for (int i = 0; i < allGenres.length; i++) {
System.out.printf("%-15s", allGenres[i]);
for (int j = 0; j < tiers.length; j++) {
System.out.printf("%-12d", matrix[i][j]);
}
System.out.println();
}
}
}