Commit b51b3dec authored by RAYMOND XIA's avatar RAYMOND XIA
Browse files

Merge branch 'master' of https://git.uwaterloo.ca/db4kim/356-project into master

parents 900291ca 4fcd0b40
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Scanner;
import java.rmi.server.SocketSecurityException;
......@@ -782,7 +783,7 @@ public class ClientApplication {
public static void addPerson() {
String name = "Name";
String originalName = "Original Name";
String birthName = "Birth Name";
String height = "Height";
String biography = "Biography";
String birthDetails = "Birth Details";
......@@ -791,15 +792,6 @@ public class ClientApplication {
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();
......@@ -810,7 +802,7 @@ public class ClientApplication {
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("2: Birth Name");
System.out.println("3: Height");
System.out.println("4: Biography");
System.out.println("5: Birth Details");
......@@ -833,9 +825,9 @@ public class ClientApplication {
}
break;
case "2":
System.out.println("Enter a value for Original Name");
System.out.println("Enter a value for Birth Name");
command = input.nextLine();
values.put(originalName, command);
values.put(birthName, command);
break;
case "3":
System.out.println("Enter a value for Height");
......@@ -892,7 +884,7 @@ public class ClientApplication {
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(birthName),
values.get(height),
values.get(biography),
values.get(birthDetails),
......@@ -921,118 +913,178 @@ public class ClientApplication {
}
}
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";
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);
// HashMap<String, String> values = new HashMap<String, String>();
if (rs.next()) {
String personID = rs.getString(1);
String name = rs.getString(2);
String birthName = rs.getString(3);
int height = rs.getInt(4);
String biography = rs.getString(5);
String birthDetails = rs.getString(6);
String dateOfBirth = rs.getTimestamp(7).toString();
String deadPersonID = "";
String dateOfDeath = "";
String placeOfDeath = "";
String reasonOfDeath = "";
query = String.format("select * from DeadPeople where personID = '%s'" + personID);
ResultSet rs2 = st.executeQuery(query);
if (rs2.next()) {
deadPersonID = rs.getString(1);
dateOfDeath = rs.getTimestamp(2).toString();
placeOfDeath = rs.getString(3);
reasonOfDeath = rs.getString(4);
}
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("Required fields are marked with a '*'.");
System.out.println("1: Name * - " + p.getName());
System.out.println("2: Original Name - " + p.getOriginalName());
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();
HashSet<String> values = new HashSet<String>();
HashSet<String> deadValues = new HashSet<String>();
switch(command) {
case "1":
System.out.println("Enter a new value for Name");
command = input.nextLine();
if (command.trim().equals("")) {
System.out.println("This field is required.");
}
else {
p.setName(command);
}
break;
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");
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("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("5: Birth Details - " + birthDetails);
System.out.println("6: Date of Birth - " + dateOfBirth);
System.out.println("7: Date of Death - " + dateOfDeath);
System.out.println("8: Place of Death - " + placeOfDeath);
System.out.println("9: Reason of Death - " + reasonOfDeath);
command = input.nextLine();
p.setReasonOfDeath(command);
break;
case "c":
exitLoop = true;
try {
Statement st = c.createStatement();
String query = "";
ResultSet rs = st.executeQuery(query);
} catch (Exception e) {
System.out.println(e);
switch(command) {
case "1":
System.out.println("Enter a new value for Name");
command = input.nextLine();
if (command.trim().equals("")) {
System.out.println("This field is required.");
}
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");
command = input.nextLine();
dateOfBirth = command;
values.add("dateOfBirth");
break;
case "7":
System.out.println("Enter a new value for Date of Death");
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);
}
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 = 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);
break;
}
break;
}
}
else {
System.out.println("A person with this name does not exist.");
}
} catch (Exception e) {
System.out.println(e);
}
}
public static void deletePerson() {
System.out.println("Are you sure you want to delete this person? Type y to confirm. Type any other character to cancel.");
String command = "";
command = input.nextLine();
if (command.equals("y")) {
try {
Statement st = c.createStatement();
String query = "";
ResultSet rs = st.executeQuery(query);
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);
String people[] = new String[10];
System.out.println("Select a person to delete");
int i = 1;
while (rs.next()) {
people[i - 1] = rs.getString(1);
System.out.println(i + ": " + rs.getString(2));
i++;
}
String command = input.nextLine().trim();
String personID = people[Integer.parseInt(command) - 1];
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);
System.out.println("Person successfully deleted.");
} catch (Exception e) {
System.out.println(e);
} else {
System.out.println("Action cancelled.");
}
} catch (Exception e) {
System.out.println(e);
}
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