Commit 96b92982 authored by Daniel Kim's avatar Daniel Kim
Browse files

fixed ep and dp

parent b51b3dec
......@@ -87,9 +87,11 @@ public class ClientApplication {
break;
case "ep":
System.out.println("Edit person \"" + command.substring(3) + "\"");
editPerson(command.substring(3));
break;
case "dp":
System.out.println("Delete person \"" + command.substring(3) + "\"");
deletePerson(command.substring(3));
break;
default:
System.out.println("Invalid command. Type \"qt\" to quit.");
......@@ -793,7 +795,6 @@ public class ClientApplication {
String reasonOfDeath = "Reason of Death";
HashMap<String, String> values = new HashMap<String, String>();
// Person p = new Person();
String command = "";
boolean exitLoop = false;
......@@ -914,12 +915,10 @@ public class ClientApplication {
}
public static void editPerson(String entity) {
String searchPersonQueryPrefix = "select * from People where name like ";
try {
Statement st = c.createStatement();
String query = searchPersonQueryPrefix + "'" + entity + "%'";
ResultSet rs = st.executeQuery(query);
PreparedStatement query = c.prepareStatement("select * from People where name like ?");
query.setString(1, entity);
ResultSet rs = query.executeQuery();
if (rs.next()) {
String personID = rs.getString(1);
......@@ -928,32 +927,30 @@ public class ClientApplication {
int height = rs.getInt(4);
String biography = rs.getString(5);
String birthDetails = rs.getString(6);
String dateOfBirth = rs.getTimestamp(7).toString();
String dateOfBirth = rs.getTimestamp(7).toString() != "" ? rs.getTimestamp(7).toString().split(" ")[0].replace('-', '/') : "";
String deadPersonID = "";
String dateOfDeath = "";
String placeOfDeath = "";
String reasonOfDeath = "";
query = String.format("select * from DeadPeople where personID = '%s'" + personID);
ResultSet rs2 = st.executeQuery(query);
query = c.prepareStatement("select * from DeadPeople where personID = ?");
query.setString(1, personID);
ResultSet rs2 = query.executeQuery();
if (rs2.next()) {
deadPersonID = rs.getString(1);
dateOfDeath = rs.getTimestamp(2).toString();
placeOfDeath = rs.getString(3);
reasonOfDeath = rs.getString(4);
deadPersonID = rs2.getString(1);
dateOfDeath = rs2.getTimestamp(2).toString() != "" ? rs2.getTimestamp(2).toString().split(" ")[0].replace('-', '/') : "";
placeOfDeath = rs2.getString(3);
reasonOfDeath = rs2.getString(4);
}
HashSet<String> values = new HashSet<String>();
HashSet<String> deadValues = new HashSet<String>();
boolean exitLoop = false;
String command = "";
while (!exitLoop) {
System.out.println("Type the number at the beginning of the line to edit the property. Type 'c' to confirm your changes.");
System.out.println("Type the number at the beginning of the line to edit the property. Type 'c' to confirm your changes. Type 'x' to cancel.");
System.out.println("Required fields are marked with a '*'.");
System.out.println("1: Name * - " + name);
System.out.println("2: Birth Name - " + birthName);
System.out.println("3: Height - " + height);
System.out.println("4: Biography - " + biography);
System.out.println("4: Biography - " + (biography != "" ? biography.substring(0, Math.min(100, biography.length())) : "") + (biography.length() > 100 ? "..." : ""));
System.out.println("5: Birth Details - " + birthDetails);
System.out.println("6: Date of Birth - " + dateOfBirth);
System.out.println("7: Date of Death - " + dateOfDeath);
......@@ -970,80 +967,80 @@ public class ClientApplication {
}
else {
name = command;
values.add("name");
}
break;
case "2":
System.out.println("Enter a new value for Birth Name");
command = input.nextLine();
birthName = command;
values.add("birth_name");
break;
case "3":
System.out.println("Enter a new value for Height");
command = input.nextLine();
height = Integer.parseInt(command);
values.add("height");
break;
case "4":
System.out.println("Enter a new value for Biography");
command = input.nextLine();
biography = command;
values.add("bio");
break;
case "5":
System.out.println("Enter a new value for Birth Details");
command = input.nextLine();
birthDetails = command;
values.add("birthDetails");
break;
case "6":
System.out.println("Enter a new value for Date of Birth");
System.out.println("Enter a new value for Date of Birth (yyyy/mm/dd)");
command = input.nextLine();
dateOfBirth = command;
values.add("dateOfBirth");
break;
case "7":
System.out.println("Enter a new value for Date of Death");
System.out.println("Enter a new value for Date of Death (yyyy/mm/dd)");
command = input.nextLine();
dateOfDeath = command;
deadValues.add("dateOfDeath");
break;
case "8":
System.out.println("Enter a new value for Place of Death");
command = input.nextLine();
placeOfDeath = command;
deadValues.add("placeOfDeath");
break;
case "9":
System.out.println("Enter a new value for Reason of Death");
command = input.nextLine();
reasonOfDeath = command;
deadValues.add("reasonOfDeath");
break;
case "c":
exitLoop = true;
Statement st2 = c.createStatement();
query = "";
if (dateOfDeath.equals("") && placeOfDeath.equals("") && reasonOfDeath.equals("")) {
query = String.format("delete ignore from DeadPeople where personID = '%s';", personID);
st2.executeUpdate(query);
query = c.prepareStatement("delete ignore from DeadPeople where personID = ?");
query.setString(1, personID);
query.executeUpdate();
}
else {
if (deadPersonID.equals("")) {
query = String.format("insert into DeadPeople values ('%s', '%s', '%s', '%s')",
"nm" + personID,
deadValues.contains(dateOfDeath) ? new java.sql.Timestamp(format.parse(dateOfDeath + " 00:00:00").getTime()) : "null",
deadValues.contains(placeOfDeath) ? placeOfDeath : "",
deadValues.contains(reasonOfDeath) ? reasonOfDeath : ""
);
st2.executeUpdate(query);
query = c.prepareStatement("insert into DeadPeople values (?, ?, ?, ?)");
query.setString(1, personID);
query.setTimestamp(2, dateOfDeath != "" ? new java.sql.Timestamp(format.parse(dateOfDeath + " 00:00:00").getTime()) : null);
query.setString(3, placeOfDeath);
query.setString(4, reasonOfDeath);
query.executeUpdate();
}
}
query = String.format("update People set name = '%s', birth_name = '%s', height = %d, bio = '%s', birthDetails = '%s', dateOfBirth = '%s' where personID = '%s';",
name, birthName, height, biography, birthDetails, dateOfBirth != "" ? new java.sql.Timestamp(format.parse(dateOfBirth + " 00:00:00").getTime()) : "null",
personID);
st2.executeUpdate(query);
PreparedStatement query2 = c.prepareStatement("update People set name = ?, birthName = ?, height = ?, bio = ?, birthDetails = ?, dateOfBirth = ? where personID = ?");
query2.setString(1, name);
query2.setString(2, birthName);
query2.setInt(3, height);
query2.setString(4, biography);
query2.setString(5, birthDetails);
query2.setTimestamp(6, dateOfBirth != "" ? new java.sql.Timestamp(format.parse(dateOfBirth + " 00:00:00").getTime()) : null);
query2.setString(7, personID);
query2.executeUpdate();
System.out.println("Successfully updated person with personID = " + personID);
break;
case "x":
exitLoop = true;
break;
default:
break;
}
}
......@@ -1058,9 +1055,10 @@ public class ClientApplication {
public static void deletePerson(String q) {
try {
Statement st = c.createStatement();
String query = "select personID, name from People where name like '" + q + "%' limit 10";
ResultSet rs = st.executeQuery(query);
PreparedStatement query = c.prepareStatement("select personID, name from People where name like ?% limit 10");
query.setString(1, q);
ResultSet rs = query.executeQuery();
String people[] = new String[10];
System.out.println("Select a person to delete");
int i = 1;
......@@ -1074,10 +1072,18 @@ public class ClientApplication {
System.out.println("Are you sure you want to delete this person? Type y to confirm. Type any other character to cancel.");
command = input.nextLine().trim();
if (command.equals("y")) {
query = String.format("delete from People where personID = '%s';", personID);
st.executeUpdate(query);
query = String.format("delete ignore from DeadPeople where personID = '%s';", personID);
st.executeUpdate(query);
query = c.prepareStatement("delete ignore from DeadPeople where personID = ?");
query.setString(1, personID);
query.executeUpdate();
query = c.prepareStatement("delete ignore from PeopleInMovies where personID = ?");
query.setString(1, personID);
query.executeUpdate();
query = c.prepareStatement("delete from People where personID = ?");
query.setString(1, personID);
query.executeUpdate();
System.out.println("Person successfully deleted.");
} else {
System.out.println("Action cancelled.");
......
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