Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA: Define a movie database application that allows a user to look up informat

ID: 3775107 • Letter: J

Question

JAVA:

Define a movie database application that allows a user to look up information about a movie and provide a review for a movie. Your application should provide the following features:
•   List all movies that are rated a particular rating (PG, PG-13, R, etc) entered by the user.
•   All a user to post a review for a particular movie. A review consists of review text and a star rating with
o   5 stars = Excellent
o   4 stars = Good
o   3 stars = Neutral
o   2 stars = Poor
o   1 star = Very Poor
•   List all reviews for a movie and include the average star rating for the movie

Your solution should read the database connection information (jdbc driver and connection url) from a properties file called database.properties. You can create a console based application to implement this application or write a GUI interface to provide these movie database features described above. The only requirement is that the class containing the main method should be called MovieDBApp.
Here is some information about the database table schema you will be using:

A set of commands to create and populate the MOVIE and MOVIE_REVIEW tables are provided below.

CONNECT 'jdbc:derby:Lab11DB;create=true';

CREATE TABLE MOVIE (MOVIE_ID int not null primary key GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), MOVIE_NAME varchar(100), RATED
varchar(10));

CREATE TABLE MOVIE_REVIEW (REVIEW_ID int not null primary key GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), MOVIE_ID int, REVIEW varchar(500), STARS int);

INSERT   INTO   MOVIE(MOVIE_NAME,   RATED)   values('Divergent', 'PG-13');
INSERT   INTO   MOVIE(MOVIE_NAME,   RATED)   values('Muppets Most Wanted', 'PG');
INSERT   INTO   MOVIE(MOVIE_NAME,   RATED)   values('The LEGO Movie', 'PG');
INSERT   INTO   MOVIE(MOVIE_NAME,   RATED)   values('Saving Mr. Banks', 'PG-13');
INSERT   INTO   MOVIE(MOVIE_NAME,   RATED)   values('Her', 'R');

Notice that in the CONNECT statement has a portion of the connection URL highlighted. This statement assumes that you will want to have the database files located in your program's current directory in Lab11DB. If you would like the database files to be located somewhere else on your system, you will need to update this statement. Also, when the MOVIE and MOVIE_REVIEW tables are created the MOVIE_ID and REVIEW_ID are set to be automatically generated by the database. As a result, any SQL statements to INSERT INTO the MOVIE or MOVIE_REVIEW table will not need to specify and ID. Instead, the database will automatically generate these values for you.

Explanation / Answer

public class movieAverage {
public static void main(String args[]) {
FileIO reader = new FileIO();
String[] rawMovies = reader.load("C://Users/James/Desktop/Movies.csv");
String[] rawRatings = reader.load("C://Users/James/Desktop/Ratings.csv");

String delim = ",(?=([^"]*"[^"]*")*[^"]*$)";
int moviesRows = rawMovies.length;
int ratingRows = rawRatings.length;
int moviesColumns = rawMovies[0].split(delim).length;
int ratingsColumns = rawRatings[0].split(delim).length;

//saving the file to a 2d array
String[][] movies = new String[moviesRows][moviesColumns];
String[][] ratings = new String[ratingRows][ratingsColumns];

for(int i = 0; i < moviesRows; i++) {
String[] tokens = rawMovies[i].split(delim);
for(int j = 0; j < moviesColumns; j++) {
movies[i][j] = tokens[j];
}
}
for(int i = 0; i < ratingRows; i++) {
String[] tokens = rawRatings[i].split(",");
for(int j = 0; j < ratingsColumns; j++) {
ratings[i][j] = tokens[j];
}
}

//averaging the ratings
int[][] unsorted = new int[moviesRows][2]; // sum, size(amount of ratings)
// for(int i = 0; i < moviesRows; i++) {
// for(int j = 0; j < unsorted[0].length; j++) {
// unsorted[i][j] = 0;
// }
// }

int x = 0;
String s = "";
for(int i = 0; i < ratings.length; i++) {
x = Integer.parseInt(ratings[i][1]) - 1;
s = ratings[i][2];
String[] tok = s.split("[ ]+");
unsorted[x][0] += Integer.parseInt(tok[0]);
unsorted[x][1]++;
}

double[][] average = new double[unsorted.length][2]; // average, film#
for(int i = 0; i < unsorted.length; i++) {
if(unsorted[i][1] == 0) average[i][0] = 0.0;
else average[i][0] = (double)(unsorted[i][0]) / (unsorted[i][1]);
average[i][1] = i;
}

// selection sort to sort by averages
double TEMP = 0.0;
for(int i = 0; i < average.length - 1; i++) {
int max = i;
for(int j = i + 1; j < average.length; j++) {
if(average[j][0] > average[max][0]) max = j;
}
TEMP = average[i][0];
average[i][0] = average[max][0];
average[max][0] = TEMP;
TEMP = average[i][1];
average[i][1] = average[max][1];
average[max][1] = TEMP;
}

// bubble sort to sort by number of ratings with averages still in order
for(int i = average.length - 1; i >= 0; i--) {
for(int j = 0; j <= i - 1; j++) {
if(average[j][0] == average[j + 1][0] && unsorted[(int)average[j][1]][1] < unsorted[(int)average[j + 1][1]][1]) {
TEMP = average[j][1];
average[j][1] = average[j + 1][1];
average[j + 1][1] = TEMP;
}
}
}

String[][] sorted = new String[moviesRows][3];
for(int i = 0; i < moviesRows; i++) {
sorted[i][0] = String.valueOf(average[i][0]); // average rating
sorted[i][1] = String.valueOf(unsorted[(int)average[i][1]][1]); // number of ratings
sorted[i][2] = movies[(int)average[i][1]][1]; // movie name
}

try {
reader.save("C://Users/James/Desktop/sortedMovies.csv", sorted);
System.out.println("SAVED");
} catch(Exception e) {
System.out.println(e.getClass());
}
}
}