Commit 8bcc0983 authored by RAYMOND XIA's avatar RAYMOND XIA
Browse files

merge new search

parents 1278389a 5ba58c17
import java.util.HashMap;
import java.util.Random;
import java.util.ArrayList;
import java.util.Scanner;
import java.rmi.server.SocketSecurityException;
import java.sql.*;
import java.text.SimpleDateFormat;
public class ClientApplication {
final static Scanner input = new Scanner(System.in);
static HashMap<String, Command> commands = new HashMap<String, Command>();
static Connection c;
static String User;
static SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
public static void main(String[] args) {
authDatabase();
switchUser();
......@@ -19,7 +22,6 @@ public class ClientApplication {
if (command.length() < 2 || !commands.containsKey(command.substring(0, 2))) {
System.out.println("Invalid command. Type \"qt\" to quit.");
}
else if (commands.containsKey(command.substring(0, 2))) {
switch (command.substring(0, 2)) {
case "se":
......@@ -68,7 +70,7 @@ public class ClientApplication {
System.exit(0);
case "am":
System.out.println("Add movie to database");
// Required fields: movie title, year, director
addMovie();
break;
case "em":
System.out.println("Edit movie \"" + command.substring(3) + "\"");
......@@ -78,6 +80,7 @@ public class ClientApplication {
break;
case "ap":
System.out.println("Add person to database");
addPerson();
break;
case "ep":
System.out.println("Edit person \"" + command.substring(3) + "\"");
......@@ -85,14 +88,14 @@ public class ClientApplication {
case "dp":
System.out.println("Delete person \"" + command.substring(3) + "\"");
break;
default:
System.out.println("Invalid command. Type \"qt\" to quit.");
break;
}
}
}
System.out.println("Program exit.");
input.close();
default:
System.out.println("Invalid command. Type \"qt\" to quit.");
break;
}
}
}
System.out.println("Program exit.");
input.close();
System.exit(0);
}
......@@ -353,34 +356,219 @@ public class ClientApplication {
}
public static void addMovie() {
System.out.println("Use the following form to add a movie entry");
System.out.println("Type the number at the beginning of the line to edit the property.");
System.out.println("Required fields are marked with a '*'.");
System.out.println("1: Name *");
System.out.println("2: Director");
System.out.println("3: Writer");
System.out.println("4: Production Company");
System.out.println("5: Description");
System.out.println("6: Actors");
System.out.println("7: Genre");
System.out.println("8: Duration");
System.out.println("9: Country");
System.out.println("0: Language");
// User fills in everything and confirms values
ArrayList<String> genres = new ArrayList<>();
ArrayList<String> languages = new ArrayList<>();
ArrayList<String> countries = new ArrayList<>();
HashMap<String, String> people = new HashMap<String, String>();
HashMap<String, String> values = new HashMap<String, String>();
String command = "";
boolean exitLoop = false;
while (!exitLoop) {
System.out.println("Use the following form to add a movie entry");
System.out.println("Type the number at the beginning of the line to edit the property.");
System.out.println("Required fields are marked with a '*'.");
System.out.println("1: Title *");
System.out.println("2: Description");
System.out.println("3: Release date");
System.out.println("4: Genre(s)");
System.out.println("5: Cast member(s)");
System.out.println("6: Duration");
System.out.println("7: Language(s)");
System.out.println("8: Country(ies)");
System.out.println("9: Production company(ies)");
System.out.println("10: Budget");
System.out.println("11: Domestic Income");
System.out.println("12: Worldwide Income");
command = input.nextLine().trim();
switch(command) {
case "1":
System.out.println("Enter a title");
command = input.nextLine().trim();
if (command.equals("")) {
System.out.println("This field is required.");
} else {
values.put("title", command);
}
break;
case "2":
System.out.println("Enter a description");
command = input.nextLine().trim();
values.put("description", command);
break;
case "3":
System.out.println("Enter release date (yyyy/mm/dd)");
command = input.nextLine().trim();
values.put("releaseDate", command);
break;
case "4":
System.out.println("Search for a genre");
try {
genres.add(searchCriteria("Genres", "genre"));
} catch (Exception e) {
System.out.println(e);
}
break;
case "5":
System.out.println("Search for a cast member");
command = input.nextLine().trim();
try {
Statement st = c.createStatement();
String query = "select personID, firstName from People where firstName like '" + command + "%' limit 10";
ResultSet rs = st.executeQuery(query);
String results[] = new String[10];
System.out.println("Select an option");
int i = 1;
while (rs.next()) {
String columnValue = rs.getString(1);
results[i - 1] = columnValue;
System.out.println(i + ": " + rs.getString(2));
i++;
}
command = input.nextLine().trim();
System.out.println("Enter their role");
String role = input.nextLine().trim();
people.put(results[Integer.parseInt(command) - 1], role);
} catch (Exception e) {
System.out.println(e);
}
try {
Statement st = c.createStatement();
String query = "";
ResultSet rs = st.executeQuery(query);
break;
case "6":
System.out.println("Enter a duration (min)");
command = input.nextLine().trim();
values.put("duration", command);
break;
case "7":
System.out.println("Search for a language");
try {
languages.add(searchCriteria("Languages", "language"));
} catch (Exception e) {
System.out.println(e);
}
break;
case "8":
System.out.println("Search for a country");
try {
countries.add(searchCriteria("Countries", "country"));
} catch (Exception e) {
System.out.println(e);
}
break;
case "9":
System.out.println("Enter a production company");
try {
values.put("production company", searchCriteria("ProductionCompanies", "productionCompany"));
} catch (Exception e) {
System.out.println(e);
}
break;
case "10":
System.out.println("Enter a budget");
command = input.nextLine().trim();
values.put("budget", command);
break;
case "11":
System.out.println("Enter domestic income");
command = input.nextLine().trim();
values.put("incomeDomestic", command);
break;
case "12":
System.out.println("Enter worldwide income");
command = input.nextLine().trim();
values.put("incomeWorldwide", command);
break;
case "c":
exitLoop = true;
try {
Statement st = c.createStatement();
ResultSet rs = st.executeQuery("select entityNum from MaxIDs where entityName = 'movie'");
int maxId = 0;
while (rs.next()) {
maxId = Integer.parseInt(rs.getString(1));
}
maxId++;
st = c.createStatement();
String query = String.format("insert into Movies (movieID, title, description, releaseDate, duration) " +
"values ('%s', '%s', '%s', %s, %s)",
"tt" + maxId,
values.get("title"),
values.containsKey("description") ? values.get("description") : "",
values.containsKey("releaseDate") ? "'" + new java.sql.Timestamp(format.parse(values.get("releaseDate")).getTime()) + "'" : "null",
values.containsKey("duration") ? values.get("duration") : "0"
);
st.executeUpdate(query);
if (!genres.isEmpty()) {
st = c.createStatement();
query = "insert into GenresInMovies (movieID, genre) values ('tt" + maxId + "','";
query += String.join("'),('tt" + maxId + "','", genres);
query += "');";
st.executeUpdate(query);
}
System.out.println("Movie successfully added, imdbID = some id");
if (!languages.isEmpty()) {
st = c.createStatement();
query = "insert into LanguagesInMovies (movieID, language) values ('tt" + maxId + "','";
query += String.join("'),('tt" + maxId + "','", languages);
query += "');";
st.executeUpdate(query);
}
} catch (Exception e) {
System.out.println(e);
if (!countries.isEmpty()) {
st = c.createStatement();
query = "insert into CountriesInMovies (movieID, country) values ('tt" + maxId + "','";
query += String.join("'),('tt" + maxId + "','", countries);
query += "');";
st.executeUpdate(query);
}
if (!people.isEmpty()) {
st = c.createStatement();
query = "insert into PeopleInMovies (movieID, personID, role) values ";
for (HashMap.Entry<String, String> entry : people.entrySet()) {
String personID = entry.getKey();
String role = entry.getValue();
query += String.format("('%s','%s','%s'),",
"tt" + maxId,
personID,
role
);
}
query = query.substring(0, query.length() - 1) + ";";
st.executeUpdate(query);
}
st = c.createStatement();
query = "update MaxIDs set entityNum = " + maxId + " where entityName = 'movie'";
st.executeUpdate(query);
} catch (Exception e) {
System.out.println(e);
}
break;
}
}
}
public static String searchCriteria(String table, String criteria) throws Exception {
String command = input.nextLine().trim();
Statement st = c.createStatement();
String query = "select " + criteria + " from " + table + " where " + criteria + " like '" + command + "%' limit 10";
ResultSet rs = st.executeQuery(query);
String results[] = new String[10];
System.out.println("Select an option");
int i = 1;
while (rs.next()) {
String columnValue = rs.getString(1);
results[i - 1] = columnValue;
System.out.println(i + ": " + columnValue);
i++;
}
command = input.nextLine().trim();
return results[Integer.parseInt(command) - 1];
}
public static void editMovie() {
try {
Statement st = c.createStatement();
......@@ -420,15 +608,19 @@ public class ClientApplication {
String height = "Height";
String biography = "Biography";
String birthDetails = "Birth Details";
System.out.println("Use the following form to add a person entry.");
System.out.println("Type the number at the beginning of the line to edit the property.");
System.out.println("Required fields are marked with a '*'.");
System.out.println("1: Name *");
System.out.println("2: Original Name");
System.out.println("3: Height");
System.out.println("4: Biography");
System.out.println("5: Birth Details");
String dateOfBirth = "Date of Birth";
String dateOfDeath = "Date of Death";
String placeOfDeath = "Place of Death";
String reasonOfDeath = "Reason of Death";
// System.out.println("Use the following form to add a person entry.");
// System.out.println("Type the number at the beginning of the line to edit the property.");
// System.out.println("Required fields are marked with a '*'.");
// System.out.println("1: Name *");
// System.out.println("2: Original Name");
// System.out.println("3: Height");
// System.out.println("4: Biography");
// System.out.println("5: Birth Details");
HashMap<String, String> values = new HashMap<String, String>();
// Person p = new Person();
......@@ -444,6 +636,10 @@ public class ClientApplication {
System.out.println("3: Height");
System.out.println("4: Biography");
System.out.println("5: Birth Details");
System.out.println("6: Date of Birth");
System.out.println("7: Date of Death");
System.out.println("8: Place of Death");
System.out.println("9: Reason of Death");
command = input.nextLine();
......@@ -478,47 +674,88 @@ public class ClientApplication {
command = input.nextLine();
values.put(birthDetails, command);
break;
case "6":
System.out.println("Enter a value for Date of Birth (yyyy/mm/dd)");
command = input.nextLine();
values.put(dateOfBirth, command);
break;
case "7":
System.out.println("Enter a value for Date of Death (yyyy/mm/dd)");
command = input.nextLine();
values.put(dateOfDeath, command);
break;
case "8":
System.out.println("Enter a value for Place of Death");
command = input.nextLine();
values.put(placeOfDeath, command);
break;
case "9":
System.out.println("Enter a value for Reason of Death");
command = input.nextLine();
values.put(reasonOfDeath, command);
break;
case "c":
exitLoop = true;
try {
Random r = new Random();
r.nextInt();
Statement idst = c.createStatement();
String idQuery = "select * from MaxIDs where entityName = 'person'";
ResultSet idrs = idst.executeQuery(idQuery);
int maxId = 0;
while (idrs.next()) {
maxId = Integer.parseInt(idrs.getString(2));
}
maxId++;
Statement updateId = c.createStatement();
String updateIdQuery = "update MaxIDs set entityNum = " + maxId + " where entityName = 'person'";
updateId.execute(updateIdQuery);
Statement st = c.createStatement();
String query = String.format("insert into People values ('%s', '%s', '%s', '%s', '%s')",
values.get(name),
String query = String.format("insert into People values ('%s', '%s', '%s', '%s', '%s', '%s', '%s')",
"nm" + maxId,
values.get(name),
values.get(originalName),
values.get(height),
values.get(biography),
values.get(birthDetails)
values.get(birthDetails),
new java.sql.Timestamp(format.parse(values.get(dateOfBirth) + " 00:00:00").getTime())
);
ResultSet rs = st.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
System.out.println(query);
for (int i = 1; i <= columnsNumber; i++) {
System.out.print(rsmd.getColumnName(i) + " ");
}
System.out.println();
while (rs.next()) {
for (int i = 1; i <= columnsNumber; i++) {
if (i > 1) System.out.print(", ");
String columnValue = rs.getString(i);
System.out.print(columnValue);
}
System.out.println();
st.execute(query);
if (values.containsKey(dateOfDeath) || values.containsKey(placeOfDeath) || values.containsKey(reasonOfDeath)) {
Statement st2 = c.createStatement();
String deathQuery = String.format("insert into DeadPeople values ('%s', '%s', '%s', '%s')",
"nm" + maxId,
new java.sql.Timestamp(format.parse(values.get(dateOfDeath) + " 00:00:00").getTime()),
values.get(placeOfDeath),
values.get(reasonOfDeath)
);
st2.execute(deathQuery);
}
System.out.println("Person successfully added, nameID = some id");
} catch (Exception e) {
System.out.println(e);
}
break;
default:
System.out.println("Invalid command " + command);
break;
}
}
}
public static void editPerson(Person p) {
// String name = "Name";
// String originalName = "Original Name";
// String height = "Height";
// String biography = "Biography";
// String birthDetails = "Birth Details";
// String dateOfBirth = "Date of Birth";
// String dateOfDeath = "Date of Death";
// String placeOfDeath = "Place of Death";
// String reasonOfDeath = "Reason of Death";
// HashMap<String, String> values = new HashMap<String, String>();
boolean exitLoop = false;
String command = "";
while (!exitLoop) {
......@@ -529,6 +766,10 @@ public class ClientApplication {
System.out.println("3: Height - " + p.getHeight());
System.out.println("4: Biography - " + p.getBiography());
System.out.println("5: Birth Details - " + p.getBirthDetails());
System.out.println("6: Date of Birth - " + p.getDateOfBirth());
System.out.println("7: Date of Death - " + p.getDateOfDeath());
System.out.println("8: Place of Death - " + p.getPlaceOfDeath());
System.out.println("9: Reason of Death - " + p.getReasonOfDeath());
command = input.nextLine();
switch(command) {
......@@ -545,18 +786,41 @@ public class ClientApplication {
case "2":
System.out.println("Enter a new value for Original Name");
command = input.nextLine();
p.setOriginalName(command);
// values.put(originalName, command);
break;
case "3":
System.out.println("Enter a new value for Height");
command = input.nextLine();
p.setHeight(Integer.parseInt(command));
break;
case "4":
System.out.println("Enter a new value for Biography");
command = input.nextLine();
p.setBiography(command);
break;
case "5":
System.out.println("Enter a new value for Birth Details");
command = input.nextLine();
p.setBirthDetails(command);
break;
case "6":
System.out.println("Enter a new value for Date of Birth");
command = input.nextLine();
break;
case "7":
System.out.println("Enter a new value for Date of Death");
command = input.nextLine();
break;
case "8":
System.out.println("Enter a new value for Place of Death");
command = input.nextLine();
p.setPlaceOfDeath(command);
break;
case "9":
System.out.println("Enter a new value for Reason of Death");
command = input.nextLine();
p.setReasonOfDeath(command);
break;
case "c":
exitLoop = true;
......
......@@ -12,8 +12,17 @@ public class Movie {
private int totalRating;
private float metascore;
public Movie(String movieID, String title, String originalTItle, int year, Date releaseDate, int duration, String description, float averageRating, int totalRating, float metascore) {
public Movie(String movieID, String title, String originalTitle, int year, Date releaseDate, int duration, String description, float averageRating, int totalRating, float metascore) {
this.movieID = movieID;
this.title = title;
this.originalTitle = originalTitle;
this.year = year;
this.releaseDate = releaseDate;
this.duration = duration;
this.description = description;
this.averageRating = averageRating;
this.totalRating = totalRating;
this.metascore = metascore;
}
public String getMovieID() {
......
......@@ -5,14 +5,22 @@ public class Person {
private String originalName;
private int height;
private String biography;
private Date birthDetails;
private String birthDetails;
private Date dateOfBirth;
private Date dateOfDeath;
private String placeOfDeath;
private String reasonOfDeath;
public Person(String name, String originalName, int height, String biography, Date birthDetails) {
public Person(String name, String originalName, int height, String biography, String birthDetails, Date dateOfBirth, Date dateOfDeath, String placeOfDeath, String reasonOfDeath) {
this.name = name;
this.originalName = originalName;
this.height = height;
this.biography = biography;
this.birthDetails = birthDetails;
this.dateOfBirth = dateOfBirth;
this.dateOfDeath = dateOfDeath;
this.placeOfDeath = placeOfDeath;
this.reasonOfDeath = reasonOfDeath;
}
public String getName() {
......@@ -47,11 +55,43 @@ public class Person {
this.biography = biography;
}
public Date getBirthDetails() {
public String getBirthDetails() {
return this.birthDetails;
}
public void setBirthDetails(Date birthDetails) {
public void setBirthDetails(String birthDetails) {
this.birthDetails = birthDetails;
}
public Date getDateOfBirth() {
return this.dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public Date getDateOfDeath() {
return this.dateOfDeath;
}
public void setDateOfDeath(Date dateOfDeath) {
this.dateOfDeath = dateOfDeath;
}
public String getPlaceOfDeath() {
return this.placeOfDeath;
}
public void setPlaceOfDeath(String placeOfDeath) {
this.placeOfDeath = placeOfDeath;
}
public String getReasonOfDeath() {
return this.reasonOfDeath;
}
public void setReasonOfDeath(String reasonOfDeath) {
this.reasonOfDeath = reasonOfDeath;
}
}
......@@ -77,8 +77,8 @@ create table People (
dateOfBirth datetime
);
alter table People add primary key (personID);
replace into People (personID, firstName, lastName, bio, birthDetails, dateOfBirth)
select personID, firstName, lastName, bio, birthDetails, dateOfBirth from ImdbNames;
replace into People (personID, firstName, lastName, height, bio, birthDetails, dateOfBirth)
select personID, firstName, lastName, height, bio, birthDetails, dateOfBirth from ImdbNames;