Commit 2b391d9e authored by RAYMOND XIA's avatar RAYMOND XIA
Browse files

proper auth fail handling and command length checking

parent f1b7b034
......@@ -11,10 +11,13 @@ public class ClientApplication {
static String User;
static SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
public static void main(String[] args) {
authDatabase();
switchUser();
String command = "";
boolean exitLoop = false;
if (authDatabase()) {
switchUser();
} else {
exitLoop = true;
}
String command = "";
while (!exitLoop) {
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();
......@@ -24,15 +27,29 @@ public class ClientApplication {
else if (commands.containsKey(command.substring(0, 2))) {
switch (command.substring(0, 2)) {
case "se":
String params = command.substring(3);
if (params.length() < 6) {
System.out.println("Invalid command. Type \"qt\" to quit.");
if (command.length() < 4) {
System.out.println("Usage: " + commands.get("se").getUsage());
break;
}
String params = command.substring(3);
if (params.length() < 7) {
System.out.println("Usage: " + commands.get("se").getUsage());
break;
}
int splitIndex = params.indexOf(" ");
String type = params.substring(0, splitIndex);
boolean invalid = false;
if (!type.equals("movie") && !type.equals("genre") && !type.equals("person")) {
System.out.println("Invalid command. Type \"qt\" to quit.");
invalid = true;
}
if ((type.equals("movie") || type.equals("genre")) && params.length() < 7) {
invalid = true;
}
if (type.equals("person") && params.length() < 8) {
invalid = true;
}
if (invalid) {
System.out.println("Usage: " + commands.get("se").getUsage());
break;
}
String keywords = params.substring(splitIndex+1);
......@@ -40,14 +57,26 @@ public class ClientApplication {
search(type, keywords);
break;
case "ar":
if (command.length() < 4) {
System.out.println("Usage: " + commands.get("ar").getUsage());
break;
}
System.out.println("Add review to \"" + command.substring(3) + "\"");
addReview(command.substring(3));
break;
case "er":
if (command.length() < 4) {
System.out.println("Usage: " + commands.get("er").getUsage());
break;
}
System.out.println("Edit review on \"" + command.substring(3) + "\"");
editReview(command.substring(3));
break;
case "dr":
if (command.length() < 4) {
System.out.println("Usage: " + commands.get("dr").getUsage());
break;
}
System.out.println("Delete review from \"" + command.substring(3) + "\"");
deleteReview(command.substring(3));
break;
......@@ -72,10 +101,18 @@ public class ClientApplication {
addMovie();
break;
case "em":
if (command.length() < 4) {
System.out.println("Usage: " + commands.get("em").getUsage());
break;
}
System.out.println("Edit movie \"" + command.substring(3) + "\"");
editMovie(command.substring(3));
break;
case "dm":
if (command.length() < 4) {
System.out.println("Usage: " + commands.get("dm").getUsage());
break;
}
System.out.println("Delete movie \"" + command.substring(3) + "\"");
deleteMovie(command.substring(3));
break;
......@@ -84,18 +121,34 @@ public class ClientApplication {
addPerson();
break;
case "ep":
if (command.length() < 4) {
System.out.println("Usage: " + commands.get("ep").getUsage());
break;
}
System.out.println("Edit person \"" + command.substring(3) + "\"");
editPerson(command.substring(3));
break;
case "dp":
if (command.length() < 4) {
System.out.println("Usage: " + commands.get("dp").getUsage());
break;
}
System.out.println("Delete person \"" + command.substring(3) + "\"");
deletePerson(command.substring(3));
break;
case "af":
if (command.length() < 4) {
System.out.println("Usage: " + commands.get("af").getUsage());
break;
}
System.out.println("Add to your favourites \"" + command.substring(3) + "\"");
addFavourite(command.substring(3));
break;
case "df":
if (command.length() < 4) {
System.out.println("Usage: " + commands.get("df").getUsage());
break;
}
System.out.println("Delete from your favourites \"" + command.substring(3) + "\"");
deleteFavourite(command.substring(3));
break;
......@@ -447,7 +500,7 @@ public class ClientApplication {
}
}
public static void authDatabase() {
public static boolean authDatabase() {
String pwd, db;
System.out.print("Enter username for the SQL server: ");
User = input.nextLine();
......@@ -461,6 +514,7 @@ public class ClientApplication {
db = input.nextLine();
try {
c = DriverManager.getConnection(Constants.db_url, User, pwd);
if (c.isClosed()) System.out.print("yoooo im closed");
Statement st = c.createStatement();
String query = "use " + db;
st.execute(query);
......@@ -473,7 +527,9 @@ public class ClientApplication {
}
} catch (Exception e) {
System.out.println(e);
return false;
}
return true;
}
public static void addMovie() {
......
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