Commit 1278389a authored by RAYMOND XIA's avatar RAYMOND XIA
Browse files

search by genre and person

parent 22e71def
......@@ -14,7 +14,7 @@ public class ClientApplication {
String command = "";
boolean exitLoop = false;
while (!exitLoop) {
System.out.println("Type \"se <movie/genre/person>\" to search movies. Type \"hp\" to see other commands. Type \"qt\" to quit.");
System.out.println("Type \"se <movie/genre/person> <keywords>\" to search movies by title, genre, or person. Type \"hp\" to see other commands. Type \"qt\" to quit.");
command = input.nextLine();
if (command.length() < 2 || !commands.containsKey(command.substring(0, 2))) {
System.out.println("Invalid command. Type \"qt\" to quit.");
......@@ -23,8 +23,20 @@ public class ClientApplication {
else if (commands.containsKey(command.substring(0, 2))) {
switch (command.substring(0, 2)) {
case "se":
System.out.println("Search for \"" + command.substring(3) + "\"");
search(command.substring(3));
String params = command.substring(3);
if (params.length() < 6) {
System.out.println("Invalid command. Type \"qt\" to quit.");
break;
}
int splitIndex = params.indexOf(" ");
String type = params.substring(0, splitIndex);
if (!type.equals("movie") && !type.equals("genre") && !type.equals("person")) {
System.out.println("Invalid command. Type \"qt\" to quit.");
break;
}
String keywords = params.substring(splitIndex+1);
System.out.println("Search for " + type + " \"" + keywords + "\"");
search(type, keywords);
break;
case "ar":
System.out.println("Add review to \"" + command.substring(3) + "\"");
......@@ -88,7 +100,7 @@ public class ClientApplication {
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
for (int i = 1; i <= columnsNumber; i++) {
System.out.print(rsmd.getColumnName(i) + " ");
System.out.print(rsmd.getColumnName(i) + ", ");
}
System.out.println();
while (rs.next()) {
......@@ -101,12 +113,25 @@ public class ClientApplication {
}
}
public static void search(String entity) {
String searchMovieQueryString = "select * from Movies where title like '%s%%' limit 10";
public static void search(String type, String keywords) {
String searchQueryString = "";
switch (type) {
case "movie":
searchQueryString = "select title, releaseDate, description from Movies where title like '%%%s%%' limit 10";
break;
case "genre":
searchQueryString = "select title, releaseDate, genre, description from ImdbMovies where genre like '%%%s%%' limit 10";
break;
case "person":
searchQueryString = "select title, releaseDate, actors, description from ImdbMovies where actors like '%%%s%%' limit 10";
break;
default: break;
}
try {
Statement st = c.createStatement();
String query = String.format(searchMovieQueryString, entity);
String query = String.format(searchQueryString, keywords);
ResultSet rs = st.executeQuery(query);
printQueryResults(rs);
// Possible queries
......@@ -302,6 +327,7 @@ public class ClientApplication {
System.out.print("Enter username for the SQL server: ");
User = input.nextLine();
pwd = new String(System.console().readPassword("Enter password for the SQL server: "));
System.out.print("Enter database for the SQL server: ");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment