Reformatted the codebase
Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
parent
6e7c1ed8bd
commit
9366ab384e
|
@ -11,7 +11,8 @@ class FormUtil {
|
||||||
/**
|
/**
|
||||||
* Reads a string from the console.
|
* Reads a string from the console.
|
||||||
*
|
*
|
||||||
* @param label Label that is shown before the input
|
* @param label
|
||||||
|
* Label that is shown before the input
|
||||||
* @return read string
|
* @return read string
|
||||||
*/
|
*/
|
||||||
static String readString(String label) {
|
static String readString(String label) {
|
||||||
|
@ -19,81 +20,87 @@ class FormUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a string from the console.
|
* Reads a string from the console.
|
||||||
*
|
*
|
||||||
* @param label Label that is shown before the input
|
* @param label
|
||||||
* @param defaultValue the default value in case an empty input is provided
|
* Label that is shown before the input
|
||||||
* @return read string
|
* @param defaultValue
|
||||||
*/
|
* the default value in case an empty input is provided
|
||||||
static String readString(String label, String defaultValue) {
|
* @return read string
|
||||||
String ret = null;
|
*/
|
||||||
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
|
static String readString(String label, String defaultValue) {
|
||||||
|
String ret = null;
|
||||||
try {
|
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
|
||||||
System.out.print(label + (!defaultValue.isEmpty() ? "[" + defaultValue + "]" : "") + ": ");
|
|
||||||
ret = stdin.readLine();
|
try {
|
||||||
ret = ret.isEmpty() ? defaultValue : ret;
|
System.out.print(label + (!defaultValue.isEmpty() ? "[" + defaultValue + "]" : "") + ": ");
|
||||||
} catch (IOException e) {
|
ret = stdin.readLine();
|
||||||
e.printStackTrace();
|
ret = ret.isEmpty() ? defaultValue : ret;
|
||||||
}
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
return ret;
|
}
|
||||||
}
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a password from the console.
|
* Reads a password from the console.
|
||||||
*
|
*
|
||||||
* @return the entered password
|
* @return the entered password
|
||||||
*/
|
*/
|
||||||
static String readPassword() {
|
static String readPassword() {
|
||||||
String password;
|
String password;
|
||||||
System.out.print("Password: ");
|
System.out.print("Password: ");
|
||||||
password = String.valueOf(System.console().readPassword());
|
password = String.valueOf(System.console().readPassword());
|
||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
|
/**
|
||||||
* Reads a password from the console.
|
* Reads a password from the console.
|
||||||
*
|
*
|
||||||
* @return the entered password
|
* @return the entered password
|
||||||
*/
|
*/
|
||||||
static String readPassword(String oldPassword) {
|
static String readPassword(String oldPassword) {
|
||||||
String password = readPassword();
|
String password = readPassword();
|
||||||
return password.isEmpty() ? oldPassword : password;
|
return password.isEmpty() ? oldPassword : password;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads an integer from the console.
|
* Reads an integer from the console.
|
||||||
*
|
*
|
||||||
* @param label Label that is shown before the input
|
* @param label
|
||||||
|
* Label that is shown before the input
|
||||||
* @return read integer
|
* @return read integer
|
||||||
*/
|
*/
|
||||||
static int readInt(String label) {
|
static int readInt(String label) {
|
||||||
return readInt(label, -1);
|
return readInt(label, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads an integer from the console.
|
* Reads an integer from the console.
|
||||||
*
|
*
|
||||||
* @param label Label that is shown before the input
|
* @param label
|
||||||
* @param defaultValue the default value
|
* Label that is shown before the input
|
||||||
* @return read integer
|
* @param defaultValue
|
||||||
*/
|
* the default value
|
||||||
static int readInt(String label, int defaultValue) {
|
* @return read integer
|
||||||
int ret = 0;
|
*/
|
||||||
boolean finished = false;
|
static int readInt(String label, int defaultValue) {
|
||||||
String defaultValueStr = defaultValue == -1 ? "" : String.valueOf(defaultValue);
|
int ret = 0;
|
||||||
|
boolean finished = false;
|
||||||
while(!finished) {
|
String defaultValueStr = defaultValue == -1 ? "" : String.valueOf(defaultValue);
|
||||||
String line = readString(label, defaultValueStr);
|
|
||||||
|
while (!finished) {
|
||||||
try {
|
String line = readString(label, defaultValueStr);
|
||||||
ret = Integer.parseInt(line);
|
|
||||||
finished = true;
|
try {
|
||||||
} catch (NumberFormatException e) {
|
ret = Integer.parseInt(line);
|
||||||
System.err.println("Invalid input: Please insert a valid number!");
|
finished = true;
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
}
|
System.err.println("Invalid input: Please insert a valid number!");
|
||||||
|
}
|
||||||
return ret;
|
}
|
||||||
}
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,115 +22,113 @@ import java.util.List;
|
||||||
public class Main {
|
public class Main {
|
||||||
private static ORM _orm;
|
private static ORM _orm;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the application.
|
* Starts the application.
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
_orm = new ORM();
|
_orm = new ORM();
|
||||||
showMainMenu();
|
showMainMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shows the main menu.
|
* Shows the main menu.
|
||||||
*/
|
*/
|
||||||
private static void showMainMenu() {
|
private static void showMainMenu() {
|
||||||
// menu options
|
// menu options
|
||||||
final int MENU_AGENT = 0;
|
final int MENU_AGENT = 0;
|
||||||
final int MENU_ESTATES = 1;
|
final int MENU_ESTATES = 1;
|
||||||
final int MENU_CONTRACTS = 2;
|
final int MENU_CONTRACTS = 2;
|
||||||
final int QUIT = 3;
|
final int QUIT = 3;
|
||||||
|
|
||||||
// create menu
|
// create menu
|
||||||
Menu mainMenu = new Menu("Main menu");
|
Menu mainMenu = new Menu("Main menu");
|
||||||
mainMenu.addEntry("EstateAgent management", MENU_AGENT);
|
mainMenu.addEntry("EstateAgent management", MENU_AGENT);
|
||||||
mainMenu.addEntry("Estate management", MENU_ESTATES);
|
mainMenu.addEntry("Estate management", MENU_ESTATES);
|
||||||
mainMenu.addEntry("Contract management", MENU_CONTRACTS);
|
mainMenu.addEntry("Contract management", MENU_CONTRACTS);
|
||||||
mainMenu.addEntry("Quit", QUIT);
|
mainMenu.addEntry("Quit", QUIT);
|
||||||
|
|
||||||
// process input
|
// process input
|
||||||
while(true) {
|
while (true) {
|
||||||
int response = mainMenu.show();
|
int response = mainMenu.show();
|
||||||
|
|
||||||
switch(response) {
|
switch (response) {
|
||||||
case MENU_AGENT:
|
case MENU_AGENT:
|
||||||
if (checkPassword()) {
|
if (checkPassword()) {
|
||||||
showEstateAgentMenu();
|
showEstateAgentMenu();
|
||||||
|
} else {
|
||||||
|
System.out.println("The password was wrong.");
|
||||||
}
|
}
|
||||||
else {
|
break;
|
||||||
System.out.println("The password was wrong.");
|
case MENU_ESTATES:
|
||||||
}
|
if (loginEstateAgent()) {
|
||||||
break;
|
|
||||||
case MENU_ESTATES:
|
|
||||||
if (loginEstateAgent()) {
|
|
||||||
showEstateMenu();
|
showEstateMenu();
|
||||||
|
} else {
|
||||||
|
System.out.println("The username or password was wrong.");
|
||||||
}
|
}
|
||||||
else {
|
break;
|
||||||
System.out.println("The username or password was wrong.");
|
case MENU_CONTRACTS:
|
||||||
}
|
showContractMenu();
|
||||||
break;
|
break;
|
||||||
case MENU_CONTRACTS:
|
case QUIT:
|
||||||
showContractMenu();
|
return;
|
||||||
break;
|
}
|
||||||
case QUIT:
|
}
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
}
|
/**
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks the password for sudo-like menu areas.
|
* Checks the password for sudo-like menu areas.
|
||||||
*/
|
*/
|
||||||
private static boolean checkPassword() {
|
private static boolean checkPassword() {
|
||||||
System.out.println("Please insert the sudo password. You are entering dangerous territory.");
|
System.out.println("Please insert the sudo password. You are entering dangerous territory.");
|
||||||
String passwordInput = FormUtil.readPassword();
|
String passwordInput = FormUtil.readPassword();
|
||||||
String sudoPassword = "ea-sudo";
|
String sudoPassword = "ea-sudo";
|
||||||
return sudoPassword.equals(passwordInput);
|
return sudoPassword.equals(passwordInput);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs a login for an estate agent.
|
* Performs a login for an estate agent.
|
||||||
*
|
*
|
||||||
* @return true if the login is successful, false otherwise
|
* @return true if the login is successful, false otherwise
|
||||||
*/
|
*/
|
||||||
private static boolean loginEstateAgent() {
|
private static boolean loginEstateAgent() {
|
||||||
System.out.println("Please insert the username and password of a valid estate agent.");
|
System.out.println("Please insert the username and password of a valid estate agent.");
|
||||||
String username = FormUtil.readString("Username");
|
String username = FormUtil.readString("Username");
|
||||||
String passwordInput = FormUtil.readPassword();
|
String passwordInput = FormUtil.readPassword();
|
||||||
EstateAgent agent = _orm.getAgent(username);
|
EstateAgent agent = _orm.getAgent(username);
|
||||||
return agent != null && agent.getPassword().equals(passwordInput);
|
return agent != null && agent.getPassword().equals(passwordInput);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shows the estate management menu.
|
* Shows the estate management menu.
|
||||||
*/
|
*/
|
||||||
private static void showEstateMenu() {
|
private static void showEstateMenu() {
|
||||||
// menu options
|
// menu options
|
||||||
final int NEW_ESTATE = 0;
|
final int NEW_ESTATE = 0;
|
||||||
final int LIST_ESTATES = 1;
|
final int LIST_ESTATES = 1;
|
||||||
final int BACK = 2;
|
final int BACK = 2;
|
||||||
|
|
||||||
// estate management menu
|
// estate management menu
|
||||||
Menu estateMenu = new Menu("Estate management");
|
Menu estateMenu = new Menu("Estate management");
|
||||||
estateMenu.addEntry("Create Estate", NEW_ESTATE);
|
estateMenu.addEntry("Create Estate", NEW_ESTATE);
|
||||||
estateMenu.addEntry("List Estates", LIST_ESTATES);
|
estateMenu.addEntry("List Estates", LIST_ESTATES);
|
||||||
estateMenu.addEntry("Back to the main menu", BACK);
|
estateMenu.addEntry("Back to the main menu", BACK);
|
||||||
|
|
||||||
// process input
|
// process input
|
||||||
while(true) {
|
while (true) {
|
||||||
int response = estateMenu.show();
|
int response = estateMenu.show();
|
||||||
|
|
||||||
switch(response) {
|
switch (response) {
|
||||||
case NEW_ESTATE:
|
case NEW_ESTATE:
|
||||||
newEstate();
|
newEstate();
|
||||||
break;
|
break;
|
||||||
case LIST_ESTATES:
|
case LIST_ESTATES:
|
||||||
listEstates();
|
listEstates();
|
||||||
break;
|
break;
|
||||||
case BACK:
|
case BACK:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new estate agent after the usesr has entered the necessary data.
|
* Creates a new estate agent after the usesr has entered the necessary data.
|
||||||
|
@ -138,9 +136,9 @@ public class Main {
|
||||||
private static void newEstate() {
|
private static void newEstate() {
|
||||||
printListOfAgents();
|
printListOfAgents();
|
||||||
|
|
||||||
String input = FormUtil.readString("Apartment(A)/House(H)");
|
String input = FormUtil.readString("Apartment(A)/House(H)");
|
||||||
boolean isApartment = input.equals("A") || input.equals("a");
|
boolean isApartment = input.equals("A") || input.equals("a");
|
||||||
Estate estate = new Estate();
|
Estate estate = new Estate();
|
||||||
estate.setStreet(FormUtil.readString("Street"));
|
estate.setStreet(FormUtil.readString("Street"));
|
||||||
estate.setStreetNumber(FormUtil.readInt("Street Number"));
|
estate.setStreetNumber(FormUtil.readInt("Street Number"));
|
||||||
estate.setPostalCode(FormUtil.readString("Postal Code"));
|
estate.setPostalCode(FormUtil.readString("Postal Code"));
|
||||||
|
@ -156,8 +154,7 @@ public class Main {
|
||||||
apartment.setBalcony(input.equals("Y") || input.equals("y"));
|
apartment.setBalcony(input.equals("Y") || input.equals("y"));
|
||||||
input = FormUtil.readString("Built-in Kitchen(Y/N)");
|
input = FormUtil.readString("Built-in Kitchen(Y/N)");
|
||||||
apartment.setBuiltinKitchen(input.equals("Y") || input.equals("y"));
|
apartment.setBuiltinKitchen(input.equals("Y") || input.equals("y"));
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
House house = new House(estate);
|
House house = new House(estate);
|
||||||
house.setPrice(FormUtil.readInt("Price"));
|
house.setPrice(FormUtil.readInt("Price"));
|
||||||
house.setFloors(FormUtil.readInt("Floors"));
|
house.setFloors(FormUtil.readInt("Floors"));
|
||||||
|
@ -172,9 +169,9 @@ public class Main {
|
||||||
/**
|
/**
|
||||||
* Lists estates.
|
* Lists estates.
|
||||||
*/
|
*/
|
||||||
private static void listEstates() {
|
private static void listEstates() {
|
||||||
List<?> estates = _orm.getAll(Type.ESTATE);
|
List<?> estates = _orm.getAll(Type.ESTATE);
|
||||||
Menu listEstates = new Menu("Please select the estate you want to modify or delete");
|
Menu listEstates = new Menu("Please select the estate you want to modify or delete");
|
||||||
System.out.println("List of Estates");
|
System.out.println("List of Estates");
|
||||||
|
|
||||||
final int BACK = 0;
|
final int BACK = 0;
|
||||||
|
@ -188,7 +185,7 @@ public class Main {
|
||||||
listEstates.addEntry("Back to the Estate management menu", BACK);
|
listEstates.addEntry("Back to the Estate management menu", BACK);
|
||||||
|
|
||||||
// process input
|
// process input
|
||||||
while(true) {
|
while (true) {
|
||||||
int response = listEstates.show();
|
int response = listEstates.show();
|
||||||
|
|
||||||
switch (response) {
|
switch (response) {
|
||||||
|
@ -199,13 +196,14 @@ public class Main {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shows a selected estate.
|
* Shows a selected estate.
|
||||||
*
|
*
|
||||||
* @param id the id of the selected estate
|
* @param id
|
||||||
|
* the id of the selected estate
|
||||||
*/
|
*/
|
||||||
private static void showEstate(int id) {
|
private static void showEstate(int id) {
|
||||||
Estate estate = _orm.getEstate(id);
|
Estate estate = _orm.getEstate(id);
|
||||||
|
@ -215,14 +213,14 @@ public class Main {
|
||||||
|
|
||||||
final int MODIFY = 1;
|
final int MODIFY = 1;
|
||||||
final int DELETE = 2;
|
final int DELETE = 2;
|
||||||
final int BACK = 3;
|
final int BACK = 3;
|
||||||
|
|
||||||
Menu showEstateMenu = new Menu("Do you want to modify or delete the estate?");
|
Menu showEstateMenu = new Menu("Do you want to modify or delete the estate?");
|
||||||
showEstateMenu.addEntry("Modify", MODIFY);
|
showEstateMenu.addEntry("Modify", MODIFY);
|
||||||
showEstateMenu.addEntry("Delete", DELETE);
|
showEstateMenu.addEntry("Delete", DELETE);
|
||||||
showEstateMenu.addEntry("Back to the list of estates", BACK);
|
showEstateMenu.addEntry("Back to the list of estates", BACK);
|
||||||
|
|
||||||
while(true) {
|
while (true) {
|
||||||
int response = showEstateMenu.show();
|
int response = showEstateMenu.show();
|
||||||
|
|
||||||
switch (response) {
|
switch (response) {
|
||||||
|
@ -241,7 +239,8 @@ public class Main {
|
||||||
/**
|
/**
|
||||||
* Prints the estate details to command line.
|
* Prints the estate details to command line.
|
||||||
*
|
*
|
||||||
* @param estate the estate from which the details should be printed to commandline
|
* @param estate
|
||||||
|
* the estate from which the details should be printed to commandline
|
||||||
*/
|
*/
|
||||||
private static void printEstateDetails(Estate estate) {
|
private static void printEstateDetails(Estate estate) {
|
||||||
System.out.println("------------------");
|
System.out.println("------------------");
|
||||||
|
@ -257,8 +256,7 @@ public class Main {
|
||||||
System.out.println("Price: " + house.getPrice());
|
System.out.println("Price: " + house.getPrice());
|
||||||
System.out.println("Floors: " + house.getFloors());
|
System.out.println("Floors: " + house.getFloors());
|
||||||
System.out.println("Garden: " + (house.hasGarden() ? "yes" : "false"));
|
System.out.println("Garden: " + (house.hasGarden() ? "yes" : "false"));
|
||||||
}
|
} else if (estate instanceof Apartment) {
|
||||||
else if (estate instanceof Apartment) {
|
|
||||||
Apartment apartment = (Apartment) estate;
|
Apartment apartment = (Apartment) estate;
|
||||||
System.out.println("Floor: " + apartment.getFloor());
|
System.out.println("Floor: " + apartment.getFloor());
|
||||||
System.out.println("Rooms: " + apartment.getRooms());
|
System.out.println("Rooms: " + apartment.getRooms());
|
||||||
|
@ -276,7 +274,7 @@ public class Main {
|
||||||
List<?> agents = _orm.getAll(Type.ESTATEAGENT);
|
List<?> agents = _orm.getAll(Type.ESTATEAGENT);
|
||||||
System.out.println("List of EstateAgents");
|
System.out.println("List of EstateAgents");
|
||||||
System.out.println("------------------");
|
System.out.println("------------------");
|
||||||
|
|
||||||
for (Object o : agents) {
|
for (Object o : agents) {
|
||||||
EstateAgent agent = (EstateAgent) o;
|
EstateAgent agent = (EstateAgent) o;
|
||||||
System.out.println("ID: " + agent.getId() + ", Name: " + agent.getName());
|
System.out.println("ID: " + agent.getId() + ", Name: " + agent.getName());
|
||||||
|
@ -287,35 +285,35 @@ public class Main {
|
||||||
/**
|
/**
|
||||||
* Modify estate.
|
* Modify estate.
|
||||||
*
|
*
|
||||||
* @param estate the modified estate
|
* @param estate
|
||||||
|
* the modified estate
|
||||||
*/
|
*/
|
||||||
private static void modifyEstate(Estate estate) {
|
private static void modifyEstate(Estate estate) {
|
||||||
System.out.println("Modify Estate");
|
System.out.println("Modify Estate");
|
||||||
printEstateDetails(estate);
|
printEstateDetails(estate);
|
||||||
printListOfAgents();
|
printListOfAgents();
|
||||||
|
|
||||||
estate.setStreet(FormUtil.readString("Street", estate.getStreet()));
|
estate.setStreet(FormUtil.readString("Street", estate.getStreet()));
|
||||||
estate.setStreetNumber(FormUtil.readInt("Street Number", estate.getStreetNumber()));
|
estate.setStreetNumber(FormUtil.readInt("Street Number", estate.getStreetNumber()));
|
||||||
estate.setPostalCode(FormUtil.readString("Postal Code", estate.getPostalCode()));
|
estate.setPostalCode(FormUtil.readString("Postal Code", estate.getPostalCode()));
|
||||||
estate.setCity(FormUtil.readString("City", estate.getCity()));
|
estate.setCity(FormUtil.readString("City", estate.getCity()));
|
||||||
estate.setSquareArea(FormUtil.readInt("Square Area", estate.getSquareArea()));
|
estate.setSquareArea(FormUtil.readInt("Square Area", estate.getSquareArea()));
|
||||||
estate.setAgent(FormUtil.readInt("EstateAgent ID", estate.getAgent()));
|
estate.setAgent(FormUtil.readInt("EstateAgent ID", estate.getAgent()));
|
||||||
|
|
||||||
if (estate instanceof Apartment) {
|
if (estate instanceof Apartment) {
|
||||||
Apartment apartment = (Apartment) estate;
|
Apartment apartment = (Apartment) estate;
|
||||||
apartment.setFloor(FormUtil.readInt("Floor", apartment.getFloor()));
|
apartment.setFloor(FormUtil.readInt("Floor", apartment.getFloor()));
|
||||||
apartment.setRooms(FormUtil.readInt("Rooms", apartment.getRooms()));
|
apartment.setRooms(FormUtil.readInt("Rooms", apartment.getRooms()));
|
||||||
apartment.setRent(FormUtil.readInt("Rent", apartment.getRent()));
|
apartment.setRent(FormUtil.readInt("Rent", apartment.getRent()));
|
||||||
String input = FormUtil.readString("Balcony(Y/N)", apartment.hasBalcony()?"Y":"N");
|
String input = FormUtil.readString("Balcony(Y/N)", apartment.hasBalcony() ? "Y" : "N");
|
||||||
apartment.setBalcony(input.equals("Y") || input.equals("y"));
|
apartment.setBalcony(input.equals("Y") || input.equals("y"));
|
||||||
input = FormUtil.readString("Built-in Kitchen(Y/N)", apartment.hasBuiltinKitchen()?"Y":"N");
|
input = FormUtil.readString("Built-in Kitchen(Y/N)", apartment.hasBuiltinKitchen() ? "Y" : "N");
|
||||||
apartment.setBuiltinKitchen(input.equals("Y") || input.equals("y"));
|
apartment.setBuiltinKitchen(input.equals("Y") || input.equals("y"));
|
||||||
}
|
} else if (estate instanceof House) {
|
||||||
else if (estate instanceof House){
|
|
||||||
House house = (House) estate;
|
House house = (House) estate;
|
||||||
house.setFloors(FormUtil.readInt("Floors", house.getFloors()));
|
house.setFloors(FormUtil.readInt("Floors", house.getFloors()));
|
||||||
house.setPrice(FormUtil.readInt("Price", house.getPrice()));
|
house.setPrice(FormUtil.readInt("Price", house.getPrice()));
|
||||||
String input = FormUtil.readString("Garden(Y/N)", house.hasGarden()?"Y":"N");
|
String input = FormUtil.readString("Garden(Y/N)", house.hasGarden() ? "Y" : "N");
|
||||||
house.setGarden(input.equals("Y") || input.equals("y"));
|
house.setGarden(input.equals("Y") || input.equals("y"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -328,68 +326,69 @@ public class Main {
|
||||||
/**
|
/**
|
||||||
* Deletes an estate.
|
* Deletes an estate.
|
||||||
*
|
*
|
||||||
* @param estate the estate that should be deleted
|
* @param estate
|
||||||
|
* the estate that should be deleted
|
||||||
*/
|
*/
|
||||||
private static void deleteEstate(Estate estate) {
|
private static void deleteEstate(Estate estate) {
|
||||||
_orm.delete(estate);
|
_orm.delete(estate);
|
||||||
System.out.println("Estate with the ID " + estate.getId() + " was deleted.");
|
System.out.println("Estate with the ID " + estate.getId() + " was deleted.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shows the estate agent management.
|
* Shows the estate agent management.
|
||||||
*/
|
*/
|
||||||
private static void showEstateAgentMenu() {
|
private static void showEstateAgentMenu() {
|
||||||
// menu options
|
// menu options
|
||||||
final int NEW_AGENT = 0;
|
final int NEW_AGENT = 0;
|
||||||
final int LIST_AGENTS = 1;
|
final int LIST_AGENTS = 1;
|
||||||
final int BACK = 2;
|
final int BACK = 2;
|
||||||
|
|
||||||
// estate management menu
|
// estate management menu
|
||||||
Menu estateAgentMenu = new Menu("EstateAgent management");
|
Menu estateAgentMenu = new Menu("EstateAgent management");
|
||||||
estateAgentMenu.addEntry("Create EstateAgent", NEW_AGENT);
|
estateAgentMenu.addEntry("Create EstateAgent", NEW_AGENT);
|
||||||
estateAgentMenu.addEntry("List EstateAgents", LIST_AGENTS);
|
estateAgentMenu.addEntry("List EstateAgents", LIST_AGENTS);
|
||||||
estateAgentMenu.addEntry("Back to the main menu", BACK);
|
estateAgentMenu.addEntry("Back to the main menu", BACK);
|
||||||
|
|
||||||
// process input
|
// process input
|
||||||
while(true) {
|
while (true) {
|
||||||
int response = estateAgentMenu.show();
|
int response = estateAgentMenu.show();
|
||||||
|
|
||||||
switch(response) {
|
switch (response) {
|
||||||
case NEW_AGENT:
|
case NEW_AGENT:
|
||||||
newEstateAgent();
|
newEstateAgent();
|
||||||
break;
|
break;
|
||||||
case LIST_AGENTS:
|
case LIST_AGENTS:
|
||||||
listEstateAgents();
|
listEstateAgents();
|
||||||
break;
|
break;
|
||||||
case BACK:
|
case BACK:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new estate agent after the user has entered the necessary data.
|
* Creates a new estate agent after the user has entered the necessary data.
|
||||||
*/
|
*/
|
||||||
private static void newEstateAgent() {
|
private static void newEstateAgent() {
|
||||||
EstateAgent agent = new EstateAgent();
|
EstateAgent agent = new EstateAgent();
|
||||||
|
|
||||||
agent.setName(FormUtil.readString("Name"));
|
agent.setName(FormUtil.readString("Name"));
|
||||||
agent.setAddress(FormUtil.readString("Address"));
|
agent.setAddress(FormUtil.readString("Address"));
|
||||||
agent.setLogin(FormUtil.readString("Login"));
|
agent.setLogin(FormUtil.readString("Login"));
|
||||||
agent.setPassword(FormUtil.readPassword());
|
agent.setPassword(FormUtil.readPassword());
|
||||||
|
|
||||||
_orm.persist(agent);
|
_orm.persist(agent);
|
||||||
|
|
||||||
System.out.println("EstateAgent with the ID " + agent.getId() + " was created.");
|
System.out.println("EstateAgent with the ID " + agent.getId() + " was created.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List estate agents.
|
* List estate agents.
|
||||||
*/
|
*/
|
||||||
private static void listEstateAgents() {
|
private static void listEstateAgents() {
|
||||||
// get all agents
|
// get all agents
|
||||||
List<?> agents = _orm.getAll(Type.ESTATEAGENT);
|
List<?> agents = _orm.getAll(Type.ESTATEAGENT);
|
||||||
Menu listEstateAgents = new Menu("Please select the estate agent you want to modify or delete");
|
Menu listEstateAgents = new Menu("Please select the estate agent you want to modify or delete");
|
||||||
System.out.println("List of EstateAgents");
|
System.out.println("List of EstateAgents");
|
||||||
|
|
||||||
final int BACK = 0;
|
final int BACK = 0;
|
||||||
|
@ -401,7 +400,7 @@ public class Main {
|
||||||
listEstateAgents.addEntry("Back to the EstateAgent management menu", BACK);
|
listEstateAgents.addEntry("Back to the EstateAgent management menu", BACK);
|
||||||
|
|
||||||
// process input
|
// process input
|
||||||
while(true) {
|
while (true) {
|
||||||
int response = listEstateAgents.show();
|
int response = listEstateAgents.show();
|
||||||
|
|
||||||
switch (response) {
|
switch (response) {
|
||||||
|
@ -417,11 +416,12 @@ public class Main {
|
||||||
/**
|
/**
|
||||||
* Shows a selected estate agent.
|
* Shows a selected estate agent.
|
||||||
*
|
*
|
||||||
* @param id the id of the selected agent
|
* @param id
|
||||||
|
* the id of the selected agent
|
||||||
*/
|
*/
|
||||||
private static void showEstateAgent(int id) {
|
private static void showEstateAgent(int id) {
|
||||||
EstateAgent agent = _orm.getAgent(id);
|
EstateAgent agent = _orm.getAgent(id);
|
||||||
|
|
||||||
System.out.println("EstateAgent");
|
System.out.println("EstateAgent");
|
||||||
System.out.println("------------------");
|
System.out.println("------------------");
|
||||||
System.out.println("ID: " + id);
|
System.out.println("ID: " + id);
|
||||||
|
@ -432,14 +432,14 @@ public class Main {
|
||||||
|
|
||||||
final int MODIFY = 1;
|
final int MODIFY = 1;
|
||||||
final int DELETE = 2;
|
final int DELETE = 2;
|
||||||
final int BACK = 3;
|
final int BACK = 3;
|
||||||
|
|
||||||
Menu showEstateAgentMenu = new Menu("Do you want to modify or delete the agent?");
|
Menu showEstateAgentMenu = new Menu("Do you want to modify or delete the agent?");
|
||||||
showEstateAgentMenu.addEntry("Modify", MODIFY);
|
showEstateAgentMenu.addEntry("Modify", MODIFY);
|
||||||
showEstateAgentMenu.addEntry("Delete", DELETE);
|
showEstateAgentMenu.addEntry("Delete", DELETE);
|
||||||
showEstateAgentMenu.addEntry("Back to the list of agents", BACK);
|
showEstateAgentMenu.addEntry("Back to the list of agents", BACK);
|
||||||
|
|
||||||
while(true) {
|
while (true) {
|
||||||
int response = showEstateAgentMenu.show();
|
int response = showEstateAgentMenu.show();
|
||||||
|
|
||||||
switch (response) {
|
switch (response) {
|
||||||
|
@ -458,24 +458,25 @@ public class Main {
|
||||||
/**
|
/**
|
||||||
* Modify estate agent.
|
* Modify estate agent.
|
||||||
*
|
*
|
||||||
* @param agent the modified agent
|
* @param agent
|
||||||
|
* the modified agent
|
||||||
*/
|
*/
|
||||||
private static void modifyEstateAgent(EstateAgent agent) {
|
private static void modifyEstateAgent(EstateAgent agent) {
|
||||||
System.out.println("Modify EstateAgent");
|
System.out.println("Modify EstateAgent");
|
||||||
System.out.println("------------------");
|
System.out.println("------------------");
|
||||||
System.out.println("ID: " + agent.getId());
|
System.out.println("ID: " + agent.getId());
|
||||||
System.out.println("Name: " + agent.getName());
|
System.out.println("Name: " + agent.getName());
|
||||||
System.out.println("Address: " + agent.getAddress());
|
System.out.println("Address: " + agent.getAddress());
|
||||||
System.out.println("Username: " + agent.getLogin());
|
System.out.println("Username: " + agent.getLogin());
|
||||||
System.out.println("------------------");
|
System.out.println("------------------");
|
||||||
|
|
||||||
agent.setName(FormUtil.readString("Name", agent.getName()));
|
agent.setName(FormUtil.readString("Name", agent.getName()));
|
||||||
agent.setAddress(FormUtil.readString("Address", agent.getAddress()));
|
agent.setAddress(FormUtil.readString("Address", agent.getAddress()));
|
||||||
agent.setLogin(FormUtil.readString("Username", agent.getLogin()));
|
agent.setLogin(FormUtil.readString("Username", agent.getLogin()));
|
||||||
agent.setPassword(FormUtil.readPassword(agent.getPassword()));
|
agent.setPassword(FormUtil.readPassword(agent.getPassword()));
|
||||||
|
|
||||||
_orm.persist(agent);
|
_orm.persist(agent);
|
||||||
|
|
||||||
System.out.println("------------------");
|
System.out.println("------------------");
|
||||||
System.out.println("Agent was modified.");
|
System.out.println("Agent was modified.");
|
||||||
}
|
}
|
||||||
|
@ -483,7 +484,8 @@ public class Main {
|
||||||
/**
|
/**
|
||||||
* Deletes an estate agent.
|
* Deletes an estate agent.
|
||||||
*
|
*
|
||||||
* @param agent the agent that should be deleted
|
* @param agent
|
||||||
|
* the agent that should be deleted
|
||||||
*/
|
*/
|
||||||
private static void deleteEstateAgent(EstateAgent agent) {
|
private static void deleteEstateAgent(EstateAgent agent) {
|
||||||
_orm.delete(agent);
|
_orm.delete(agent);
|
||||||
|
@ -495,23 +497,23 @@ public class Main {
|
||||||
*/
|
*/
|
||||||
private static void showContractMenu() {
|
private static void showContractMenu() {
|
||||||
// menu options
|
// menu options
|
||||||
final int INSERT_PERSON = 0;
|
final int INSERT_PERSON = 0;
|
||||||
final int CREATE_CONTRACT = 1;
|
final int CREATE_CONTRACT = 1;
|
||||||
final int OVERVIEW_CONTRACTS = 2;
|
final int OVERVIEW_CONTRACTS = 2;
|
||||||
final int BACK = 3;
|
final int BACK = 3;
|
||||||
|
|
||||||
// create menu
|
// create menu
|
||||||
Menu mainMenu = new Menu("Contract Management");
|
Menu mainMenu = new Menu("Contract Management");
|
||||||
mainMenu.addEntry("Insert person", INSERT_PERSON );
|
mainMenu.addEntry("Insert person", INSERT_PERSON);
|
||||||
mainMenu.addEntry("Create/Sign contract", CREATE_CONTRACT);
|
mainMenu.addEntry("Create/Sign contract", CREATE_CONTRACT);
|
||||||
mainMenu.addEntry("Contracts overview", OVERVIEW_CONTRACTS);
|
mainMenu.addEntry("Contracts overview", OVERVIEW_CONTRACTS);
|
||||||
mainMenu.addEntry("Back", BACK);
|
mainMenu.addEntry("Back", BACK);
|
||||||
|
|
||||||
// process input
|
// process input
|
||||||
while(true) {
|
while (true) {
|
||||||
int response = mainMenu.show();
|
int response = mainMenu.show();
|
||||||
|
|
||||||
switch(response) {
|
switch (response) {
|
||||||
case INSERT_PERSON:
|
case INSERT_PERSON:
|
||||||
newPerson();
|
newPerson();
|
||||||
break;
|
break;
|
||||||
|
@ -551,7 +553,7 @@ public class Main {
|
||||||
System.out.println("Please enter the date of the contract.");
|
System.out.println("Please enter the date of the contract.");
|
||||||
contract.setDate(Date.valueOf(FormUtil.readInt("Year") + "-" + FormUtil.readInt("Month") + "-"
|
contract.setDate(Date.valueOf(FormUtil.readInt("Year") + "-" + FormUtil.readInt("Month") + "-"
|
||||||
+ FormUtil.readInt("Day")));
|
+ FormUtil.readInt("Day")));
|
||||||
String input = FormUtil.readString("Purchase Contract(P) / Tenancy Contract(T)");
|
String input = FormUtil.readString("Purchase Contract(P) / Tenancy Contract(T)");
|
||||||
boolean isTenancy = input.equals("T") || input.equals("t");
|
boolean isTenancy = input.equals("T") || input.equals("t");
|
||||||
if (isTenancy) {
|
if (isTenancy) {
|
||||||
boolean apartmentsAvailable = printListOfApartments();
|
boolean apartmentsAvailable = printListOfApartments();
|
||||||
|
@ -572,8 +574,7 @@ public class Main {
|
||||||
long duration = endDate.getTime() - startDate.getTime();
|
long duration = endDate.getTime() - startDate.getTime();
|
||||||
tenancyContract.setDuration(Duration.ofMillis(duration));
|
tenancyContract.setDuration(Duration.ofMillis(duration));
|
||||||
tenancyContract.setAdditionalCost(FormUtil.readInt("Additional Costs"));
|
tenancyContract.setAdditionalCost(FormUtil.readInt("Additional Costs"));
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
boolean housesAvailable = printListOfHouses();
|
boolean housesAvailable = printListOfHouses();
|
||||||
if (!housesAvailable) {
|
if (!housesAvailable) {
|
||||||
System.out.println("No houses available to sell.");
|
System.out.println("No houses available to sell.");
|
||||||
|
@ -598,7 +599,7 @@ public class Main {
|
||||||
|
|
||||||
for (Object o : persons) {
|
for (Object o : persons) {
|
||||||
Person person = (Person) o;
|
Person person = (Person) o;
|
||||||
System.out.println("ID: " + person.getId() + ", Name: " + person.getFirstName() + " " + person.getName()
|
System.out.println("ID: " + person.getId() + ", Name: " + person.getFirstName() + " " + person.getName()
|
||||||
+ " , Address: " + person.getAddress());
|
+ " , Address: " + person.getAddress());
|
||||||
}
|
}
|
||||||
System.out.println("------------------");
|
System.out.println("------------------");
|
||||||
|
@ -610,7 +611,7 @@ public class Main {
|
||||||
* @return true if houses are available, false otherwise
|
* @return true if houses are available, false otherwise
|
||||||
*/
|
*/
|
||||||
private static boolean printListOfHouses() {
|
private static boolean printListOfHouses() {
|
||||||
List<?> houses = _orm.getAll(Type.HOUSE);
|
List<?> houses = _orm.getAll(Type.HOUSE);
|
||||||
List<Integer> soldHouses = _orm.getSoldHouses();
|
List<Integer> soldHouses = _orm.getSoldHouses();
|
||||||
|
|
||||||
if (houses.size() <= soldHouses.size()) {
|
if (houses.size() <= soldHouses.size()) {
|
||||||
|
@ -639,7 +640,7 @@ public class Main {
|
||||||
* @return true if apartments are available, false otherwise
|
* @return true if apartments are available, false otherwise
|
||||||
*/
|
*/
|
||||||
private static boolean printListOfApartments() {
|
private static boolean printListOfApartments() {
|
||||||
List<?> apartments = _orm.getAll(Type.APARTMENT);
|
List<?> apartments = _orm.getAll(Type.APARTMENT);
|
||||||
List<Integer> rentedApartments = _orm.getRentedApartments();
|
List<Integer> rentedApartments = _orm.getRentedApartments();
|
||||||
if (apartments.size() <= rentedApartments.size()) {
|
if (apartments.size() <= rentedApartments.size()) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -654,7 +655,8 @@ public class Main {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
System.out.println("ID: " + apartment.getId() + "; Address: " + apartment.getStreet() + " "
|
System.out.println("ID: " + apartment.getId() + "; Address: " + apartment.getStreet() + " "
|
||||||
+ apartment.getStreetNumber() + ", " + apartment.getPostalCode() + " " + apartment.getCity());
|
+ apartment.getStreetNumber() + ", " + apartment.getPostalCode() + " " +
|
||||||
|
apartment.getCity());
|
||||||
}
|
}
|
||||||
System.out.println("------------------");
|
System.out.println("------------------");
|
||||||
|
|
||||||
|
@ -665,8 +667,8 @@ public class Main {
|
||||||
* Shows the contracts overview.
|
* Shows the contracts overview.
|
||||||
*/
|
*/
|
||||||
private static void showContractsOverview() {
|
private static void showContractsOverview() {
|
||||||
List<?> contracts = _orm.getAll(Type.CONTRACT);
|
List<?> contracts = _orm.getAll(Type.CONTRACT);
|
||||||
Menu listContracts = new Menu("Select a contract to view the details");
|
Menu listContracts = new Menu("Select a contract to view the details");
|
||||||
|
|
||||||
final int BACK = 0;
|
final int BACK = 0;
|
||||||
|
|
||||||
|
@ -678,7 +680,7 @@ public class Main {
|
||||||
listContracts.addEntry("Back to the Contract management menu", BACK);
|
listContracts.addEntry("Back to the Contract management menu", BACK);
|
||||||
|
|
||||||
// process input
|
// process input
|
||||||
while(true) {
|
while (true) {
|
||||||
int response = listContracts.show();
|
int response = listContracts.show();
|
||||||
|
|
||||||
switch (response) {
|
switch (response) {
|
||||||
|
@ -694,7 +696,8 @@ public class Main {
|
||||||
/**
|
/**
|
||||||
* Prints the contract details to command line.
|
* Prints the contract details to command line.
|
||||||
*
|
*
|
||||||
* @param contract the contract from which the details should be printed to commandline
|
* @param contract
|
||||||
|
* the contract from which the details should be printed to commandline
|
||||||
*/
|
*/
|
||||||
private static void printContractDetails(Contract contract) {
|
private static void printContractDetails(Contract contract) {
|
||||||
System.out.println("------------------");
|
System.out.println("------------------");
|
||||||
|
@ -711,8 +714,7 @@ public class Main {
|
||||||
estate = _orm.getEstate(purchaseContract.getHouse());
|
estate = _orm.getEstate(purchaseContract.getHouse());
|
||||||
System.out.println("House: " + estate.getStreet() + " " + estate.getStreetNumber() + ", "
|
System.out.println("House: " + estate.getStreet() + " " + estate.getStreetNumber() + ", "
|
||||||
+ estate.getPostalCode() + " " + estate.getCity());
|
+ estate.getPostalCode() + " " + estate.getCity());
|
||||||
}
|
} else if (contract instanceof TenancyContract) {
|
||||||
else if (contract instanceof TenancyContract) {
|
|
||||||
TenancyContract tenancyContract = (TenancyContract) contract;
|
TenancyContract tenancyContract = (TenancyContract) contract;
|
||||||
Duration duration = tenancyContract.getDuration();
|
Duration duration = tenancyContract.getDuration();
|
||||||
System.out.println("Start Date: " + DateFormat.getInstance().format(tenancyContract.getStartDate()));
|
System.out.println("Start Date: " + DateFormat.getInstance().format(tenancyContract.getStartDate()));
|
||||||
|
@ -728,7 +730,8 @@ public class Main {
|
||||||
/**
|
/**
|
||||||
* Shows a selected contract.
|
* Shows a selected contract.
|
||||||
*
|
*
|
||||||
* @param id the id of the selected contract
|
* @param id
|
||||||
|
* the id of the selected contract
|
||||||
*/
|
*/
|
||||||
private static void showContract(int id) {
|
private static void showContract(int id) {
|
||||||
Contract contract = _orm.getContract(id);
|
Contract contract = _orm.getContract(id);
|
||||||
|
|
|
@ -7,17 +7,17 @@ import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Small helper class for menus.
|
* Small helper class for menus.
|
||||||
*
|
* <p>
|
||||||
* First menu entries have to be created with addEntry. Afterwards the menu can be shown
|
* First menu entries have to be created with addEntry. Afterwards the menu can be shown
|
||||||
* on the console with show(). show() also returns the constant number of the selected menu entry.
|
* on the console with show(). show() also returns the constant number of the selected menu entry.
|
||||||
*
|
* <p>
|
||||||
* Example:
|
* Example:
|
||||||
* Menu m = new Menu("Main menu");
|
* Menu m = new Menu("Main menu");
|
||||||
* m.addEntry("Work hard", 0);
|
* m.addEntry("Work hard", 0);
|
||||||
* m.addEntry("Rest", 1);
|
* m.addEntry("Rest", 1);
|
||||||
* m.addEntry("Go home", 2);
|
* m.addEntry("Go home", 2);
|
||||||
* int chosenOption = m.show();
|
* int chosenOption = m.show();
|
||||||
*
|
* <p>
|
||||||
* This results in the following output on the console:
|
* This results in the following output on the console:
|
||||||
* Main menu:
|
* Main menu:
|
||||||
* [1] Work hard
|
* [1] Work hard
|
||||||
|
@ -26,61 +26,64 @@ import java.util.ArrayList;
|
||||||
* --
|
* --
|
||||||
*/
|
*/
|
||||||
class Menu {
|
class Menu {
|
||||||
private String _title;
|
private String _title;
|
||||||
private ArrayList<String> _labels = new ArrayList<>();
|
private ArrayList<String> _labels = new ArrayList<>();
|
||||||
private ArrayList<Integer> _returnValues = new ArrayList<>();
|
private ArrayList<Integer> _returnValues = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the menu object.
|
* Initializes the menu object.
|
||||||
*
|
*
|
||||||
* @param title Title of the menu (e.g. "Main menu")
|
* @param title
|
||||||
*/
|
* Title of the menu (e.g. "Main menu")
|
||||||
Menu(String title) {
|
*/
|
||||||
super();
|
Menu(String title) {
|
||||||
_title = title;
|
super();
|
||||||
}
|
_title = title;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Adds a new menu entry
|
/**
|
||||||
|
* Adds a new menu entry
|
||||||
*
|
*
|
||||||
* @param label Name of the entry
|
* @param label
|
||||||
* @param returnValue constant number which is returned upon selection this entry
|
* Name of the entry
|
||||||
*/
|
* @param returnValue
|
||||||
void addEntry(String label, int returnValue) {
|
* constant number which is returned upon selection this entry
|
||||||
_labels.add(label);
|
*/
|
||||||
_returnValues.add(returnValue);
|
void addEntry(String label, int returnValue) {
|
||||||
}
|
_labels.add(label);
|
||||||
|
_returnValues.add(returnValue);
|
||||||
/**
|
}
|
||||||
* Shows the menu.
|
|
||||||
|
/**
|
||||||
|
* Shows the menu.
|
||||||
*
|
*
|
||||||
* @return The constant number of the selected menu entry.
|
* @return The constant number of the selected menu entry.
|
||||||
*/
|
*/
|
||||||
int show() {
|
int show() {
|
||||||
int selection = -1;
|
int selection = -1;
|
||||||
|
|
||||||
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
|
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
|
||||||
while(selection == -1) {
|
while (selection == -1) {
|
||||||
System.out.println(_title+":");
|
System.out.println(_title + ":");
|
||||||
|
|
||||||
for(int i = 0; i < _labels.size(); ++i) {
|
for (int i = 0; i < _labels.size(); ++i) {
|
||||||
System.out.println("[" + (i + 1) + "] " + _labels.get(i));
|
System.out.println("[" + (i + 1) + "] " + _labels.get(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.print("-- ");
|
System.out.print("-- ");
|
||||||
try {
|
try {
|
||||||
selection = Integer.parseInt(stdin.readLine());
|
selection = Integer.parseInt(stdin.readLine());
|
||||||
} catch (NumberFormatException | IOException e) {
|
} catch (NumberFormatException | IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(selection < 1 || selection > _returnValues.size()) {
|
if (selection < 1 || selection > _returnValues.size()) {
|
||||||
System.err.println("Invalid input!");
|
System.err.println("Invalid input!");
|
||||||
selection = -1;
|
selection = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return _returnValues.get(selection-1);
|
return _returnValues.get(selection - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,54 +1,63 @@
|
||||||
package de.dis2017.data;
|
package de.dis2017.data;
|
||||||
|
|
||||||
public class Apartment extends Estate{
|
public class Apartment extends Estate {
|
||||||
private int floor;
|
private int floor;
|
||||||
private int rent;
|
private int rent;
|
||||||
private int rooms;
|
private int rooms;
|
||||||
private boolean balcony;
|
private boolean balcony;
|
||||||
private boolean builtinKitchen;
|
private boolean builtinKitchen;
|
||||||
|
|
||||||
public Apartment(){
|
public Apartment() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Apartment(Estate estate){
|
public Apartment(Estate estate) {
|
||||||
this.setId(estate.getId());
|
this.setId(estate.getId());
|
||||||
this.setCity(estate.getCity());
|
this.setCity(estate.getCity());
|
||||||
this.setPostalCode(estate.getPostalCode());
|
this.setPostalCode(estate.getPostalCode());
|
||||||
this.setStreet(estate.getStreet());
|
this.setStreet(estate.getStreet());
|
||||||
this.setStreetNumber(estate.getStreetNumber());
|
this.setStreetNumber(estate.getStreetNumber());
|
||||||
this.setSquareArea(estate.getSquareArea());
|
this.setSquareArea(estate.getSquareArea());
|
||||||
this.setAgent(estate.getAgent());
|
this.setAgent(estate.getAgent());
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getFloor() {
|
public int getFloor() {
|
||||||
return floor;
|
return floor;
|
||||||
}
|
}
|
||||||
public void setFloor(int floor) {
|
|
||||||
this.floor = floor;
|
public void setFloor(int floor) {
|
||||||
}
|
this.floor = floor;
|
||||||
public int getRent() {
|
}
|
||||||
return rent;
|
|
||||||
}
|
public int getRent() {
|
||||||
public void setRent(int rent) {
|
return rent;
|
||||||
this.rent = rent;
|
}
|
||||||
}
|
|
||||||
public int getRooms() {
|
public void setRent(int rent) {
|
||||||
return rooms;
|
this.rent = rent;
|
||||||
}
|
}
|
||||||
public void setRooms(int rooms) {
|
|
||||||
this.rooms = rooms;
|
public int getRooms() {
|
||||||
}
|
return rooms;
|
||||||
public boolean hasBalcony() {
|
}
|
||||||
return balcony;
|
|
||||||
}
|
public void setRooms(int rooms) {
|
||||||
public void setBalcony(boolean balcony) {
|
this.rooms = rooms;
|
||||||
this.balcony = balcony;
|
}
|
||||||
}
|
|
||||||
public boolean hasBuiltinKitchen() {
|
public boolean hasBalcony() {
|
||||||
return builtinKitchen;
|
return balcony;
|
||||||
}
|
}
|
||||||
public void setBuiltinKitchen(boolean builtinKitchen) {
|
|
||||||
this.builtinKitchen = builtinKitchen;
|
public void setBalcony(boolean balcony) {
|
||||||
}
|
this.balcony = balcony;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasBuiltinKitchen() {
|
||||||
|
return builtinKitchen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBuiltinKitchen(boolean builtinKitchen) {
|
||||||
|
this.builtinKitchen = builtinKitchen;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,40 +6,49 @@ import java.sql.Date;
|
||||||
* Contract data class
|
* Contract data class
|
||||||
*/
|
*/
|
||||||
public class Contract {
|
public class Contract {
|
||||||
private int contractNo = -1;
|
private int contractNo = -1;
|
||||||
private Date date;
|
private Date date;
|
||||||
private String place;
|
private String place;
|
||||||
private int person;
|
private int person;
|
||||||
private int estate;
|
private int estate;
|
||||||
|
|
||||||
public int getEstate() {
|
public int getEstate() {
|
||||||
return estate;
|
return estate;
|
||||||
}
|
}
|
||||||
public void setEstate(int estate) {
|
|
||||||
this.estate = estate;
|
public void setEstate(int estate) {
|
||||||
}
|
this.estate = estate;
|
||||||
public int getContractNo() {
|
}
|
||||||
return contractNo;
|
|
||||||
}
|
public int getContractNo() {
|
||||||
public void setContractNo(int contractNo) {
|
return contractNo;
|
||||||
this.contractNo = contractNo;
|
}
|
||||||
}
|
|
||||||
public Date getDate() {
|
public void setContractNo(int contractNo) {
|
||||||
return date;
|
this.contractNo = contractNo;
|
||||||
}
|
}
|
||||||
public void setDate(Date date) {
|
|
||||||
this.date = date;
|
public Date getDate() {
|
||||||
}
|
return date;
|
||||||
public String getPlace() {
|
}
|
||||||
return place;
|
|
||||||
}
|
public void setDate(Date date) {
|
||||||
public void setPlace(String place) {
|
this.date = date;
|
||||||
this.place = place;
|
}
|
||||||
}
|
|
||||||
public int getPerson() {
|
public String getPlace() {
|
||||||
return person;
|
return place;
|
||||||
}
|
}
|
||||||
public void setPerson(int person) {
|
|
||||||
this.person = person;
|
public void setPlace(String place) {
|
||||||
}
|
this.place = place;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPerson() {
|
||||||
|
return person;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPerson(int person) {
|
||||||
|
this.person = person;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,63 +4,63 @@ package de.dis2017.data;
|
||||||
* EstateAgent data class
|
* EstateAgent data class
|
||||||
*/
|
*/
|
||||||
public class Estate {
|
public class Estate {
|
||||||
private int id = -1;
|
private int id = -1;
|
||||||
private String city;
|
private String city;
|
||||||
private String postalCode;
|
private String postalCode;
|
||||||
private String street;
|
private String street;
|
||||||
private int streetNumber;
|
private int streetNumber;
|
||||||
private int squareArea;
|
private int squareArea;
|
||||||
private int agent;
|
private int agent;
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(int id) {
|
public void setId(int id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCity() {
|
public String getCity() {
|
||||||
return city;
|
return city;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCity(String city) {
|
public void setCity(String city) {
|
||||||
this.city = city;
|
this.city = city;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPostalCode() {
|
public String getPostalCode() {
|
||||||
return postalCode;
|
return postalCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPostalCode(String postalCode) {
|
public void setPostalCode(String postalCode) {
|
||||||
this.postalCode = postalCode;
|
this.postalCode = postalCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getStreet() {
|
public String getStreet() {
|
||||||
return street;
|
return street;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setStreet(String street) {
|
public void setStreet(String street) {
|
||||||
this.street = street;
|
this.street = street;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getStreetNumber() {
|
public int getStreetNumber() {
|
||||||
return streetNumber;
|
return streetNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setStreetNumber(int streetNumber) {
|
public void setStreetNumber(int streetNumber) {
|
||||||
this.streetNumber = streetNumber;
|
this.streetNumber = streetNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSquareArea() {
|
public int getSquareArea() {
|
||||||
return squareArea;
|
return squareArea;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSquareArea(int squareArea) {
|
public void setSquareArea(int squareArea) {
|
||||||
this.squareArea = squareArea;
|
this.squareArea = squareArea;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAgent() { return agent; }
|
public int getAgent() { return agent; }
|
||||||
|
|
||||||
public void setAgent(int agent) { this.agent = agent; }
|
public void setAgent(int agent) { this.agent = agent; }
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,49 +4,49 @@ package de.dis2017.data;
|
||||||
* EstateAgent data class
|
* EstateAgent data class
|
||||||
*/
|
*/
|
||||||
public class EstateAgent {
|
public class EstateAgent {
|
||||||
private int id = -1;
|
private int id = -1;
|
||||||
private String name;
|
private String name;
|
||||||
private String address;
|
private String address;
|
||||||
private String login;
|
private String login;
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(int id) {
|
public void setId(int id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAddress() {
|
public String getAddress() {
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAddress(String address) {
|
public void setAddress(String address) {
|
||||||
this.address = address;
|
this.address = address;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLogin() {
|
public String getLogin() {
|
||||||
return login;
|
return login;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLogin(String login) {
|
public void setLogin(String login) {
|
||||||
this.login = login;
|
this.login = login;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPassword() {
|
public String getPassword() {
|
||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPassword(String password) {
|
public void setPassword(String password) {
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,42 +1,47 @@
|
||||||
package de.dis2017.data;
|
package de.dis2017.data;
|
||||||
|
|
||||||
public class House extends Estate {
|
public class House extends Estate {
|
||||||
private int floors;
|
private int floors;
|
||||||
private int price;
|
private int price;
|
||||||
private boolean garden;
|
private boolean garden;
|
||||||
|
|
||||||
public House(){
|
public House() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public House(Estate estate){
|
public House(Estate estate) {
|
||||||
this.setId(estate.getId());
|
this.setId(estate.getId());
|
||||||
this.setCity(estate.getCity());
|
this.setCity(estate.getCity());
|
||||||
this.setPostalCode(estate.getPostalCode());
|
this.setPostalCode(estate.getPostalCode());
|
||||||
this.setStreet(estate.getStreet());
|
this.setStreet(estate.getStreet());
|
||||||
this.setStreetNumber(estate.getStreetNumber());
|
this.setStreetNumber(estate.getStreetNumber());
|
||||||
this.setSquareArea(estate.getSquareArea());
|
this.setSquareArea(estate.getSquareArea());
|
||||||
this.setAgent(estate.getAgent());
|
this.setAgent(estate.getAgent());
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getFloors() {
|
public int getFloors() {
|
||||||
return floors;
|
return floors;
|
||||||
}
|
}
|
||||||
public void setFloors(int floors) {
|
|
||||||
this.floors = floors;
|
public void setFloors(int floors) {
|
||||||
}
|
this.floors = floors;
|
||||||
public int getPrice() {
|
}
|
||||||
return price;
|
|
||||||
}
|
public int getPrice() {
|
||||||
public void setPrice(int price) {
|
return price;
|
||||||
this.price = price;
|
}
|
||||||
}
|
|
||||||
public boolean hasGarden() {
|
public void setPrice(int price) {
|
||||||
return garden;
|
this.price = price;
|
||||||
}
|
}
|
||||||
public void setGarden(boolean garden) {
|
|
||||||
this.garden = garden;
|
public boolean hasGarden() {
|
||||||
}
|
return garden;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGarden(boolean garden) {
|
||||||
|
this.garden = garden;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,31 +1,38 @@
|
||||||
package de.dis2017.data;
|
package de.dis2017.data;
|
||||||
|
|
||||||
public class Person {
|
public class Person {
|
||||||
private int id = -1;
|
private int id = -1;
|
||||||
private String firstName;
|
private String firstName;
|
||||||
private String name;
|
private String name;
|
||||||
private String address;
|
private String address;
|
||||||
|
|
||||||
public int getId() { return id; }
|
public int getId() { return id; }
|
||||||
public void setId(int id) { this.id = id; }
|
|
||||||
public String getFirstName() {
|
public void setId(int id) { this.id = id; }
|
||||||
return firstName;
|
|
||||||
}
|
public String getFirstName() {
|
||||||
public void setFirstName(String firstName) {
|
return firstName;
|
||||||
this.firstName = firstName;
|
}
|
||||||
}
|
|
||||||
public String getName() {
|
public void setFirstName(String firstName) {
|
||||||
return name;
|
this.firstName = firstName;
|
||||||
}
|
}
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
public String getName() {
|
||||||
}
|
return name;
|
||||||
public String getAddress() {
|
}
|
||||||
return address;
|
|
||||||
}
|
public void setName(String name) {
|
||||||
public void setAddress(String adress) {
|
this.name = name;
|
||||||
this.address = adress;
|
}
|
||||||
}
|
|
||||||
|
public String getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(String adress) {
|
||||||
|
this.address = adress;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,40 +1,41 @@
|
||||||
package de.dis2017.data;
|
package de.dis2017.data;
|
||||||
|
|
||||||
public class PurchaseContract extends Contract {
|
public class PurchaseContract extends Contract {
|
||||||
private int noOfInstallments;
|
private int noOfInstallments;
|
||||||
private int interestRate;
|
private int interestRate;
|
||||||
|
|
||||||
public PurchaseContract(){
|
public PurchaseContract() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
public PurchaseContract(Contract contract){
|
|
||||||
this.setContractNo(contract.getContractNo());
|
public PurchaseContract(Contract contract) {
|
||||||
this.setDate(contract.getDate());
|
this.setContractNo(contract.getContractNo());
|
||||||
this.setPlace(contract.getPlace());
|
this.setDate(contract.getDate());
|
||||||
this.setPerson(contract.getPerson());
|
this.setPlace(contract.getPlace());
|
||||||
}
|
this.setPerson(contract.getPerson());
|
||||||
|
}
|
||||||
public int getNoOfInstallments() {
|
|
||||||
return noOfInstallments;
|
public int getNoOfInstallments() {
|
||||||
}
|
return noOfInstallments;
|
||||||
|
}
|
||||||
public void setNoOfInstallments(int noOfInstallments) {
|
|
||||||
this.noOfInstallments = noOfInstallments;
|
public void setNoOfInstallments(int noOfInstallments) {
|
||||||
}
|
this.noOfInstallments = noOfInstallments;
|
||||||
|
}
|
||||||
public int getInterestRate() {
|
|
||||||
return interestRate;
|
public int getInterestRate() {
|
||||||
}
|
return interestRate;
|
||||||
|
}
|
||||||
public void setInterestRate(int interestRate) {
|
|
||||||
this.interestRate = interestRate;
|
public void setInterestRate(int interestRate) {
|
||||||
}
|
this.interestRate = interestRate;
|
||||||
|
}
|
||||||
public int getHouse() {
|
|
||||||
return super.getEstate();
|
public int getHouse() {
|
||||||
}
|
return super.getEstate();
|
||||||
|
}
|
||||||
public void setHouse(int house) {
|
|
||||||
super.setEstate(house);
|
public void setHouse(int house) {
|
||||||
}
|
super.setEstate(house);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,54 +5,54 @@ import java.time.Duration;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public class TenancyContract extends Contract {
|
public class TenancyContract extends Contract {
|
||||||
private Timestamp startDate;
|
private Timestamp startDate;
|
||||||
private Duration duration;
|
private Duration duration;
|
||||||
private int additionalCost;
|
private int additionalCost;
|
||||||
|
|
||||||
public TenancyContract(){
|
public TenancyContract() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public TenancyContract(Contract contract){
|
public TenancyContract(Contract contract) {
|
||||||
this.setContractNo(contract.getContractNo());
|
this.setContractNo(contract.getContractNo());
|
||||||
this.setDate(contract.getDate());
|
this.setDate(contract.getDate());
|
||||||
this.setPlace(contract.getPlace());
|
this.setPlace(contract.getPlace());
|
||||||
this.setPerson(contract.getPerson());
|
this.setPerson(contract.getPerson());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Timestamp getStartDate() {
|
public Timestamp getStartDate() {
|
||||||
return startDate;
|
return startDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setStartDate(Date startDate) {
|
public void setStartDate(Date startDate) {
|
||||||
this.startDate = new Timestamp(startDate.getTime());
|
this.startDate = new Timestamp(startDate.getTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Duration getDuration() {
|
public Duration getDuration() {
|
||||||
return duration;
|
return duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDuration(Duration duration) {
|
public void setDuration(Duration duration) {
|
||||||
this.duration = duration;
|
this.duration = duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDuration(Timestamp duration) {
|
public void setDuration(Timestamp duration) {
|
||||||
this.duration = Duration.ofMillis(duration.getTime());
|
this.duration = Duration.ofMillis(duration.getTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAdditionalCost() {
|
public int getAdditionalCost() {
|
||||||
return additionalCost;
|
return additionalCost;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAdditionalCost(int additionalCost) {
|
public void setAdditionalCost(int additionalCost) {
|
||||||
this.additionalCost = additionalCost;
|
this.additionalCost = additionalCost;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getApartment() {
|
public int getApartment() {
|
||||||
return super.getEstate();
|
return super.getEstate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setApartment(int apartment) {
|
public void setApartment(int apartment) {
|
||||||
super.setEstate(apartment);
|
super.setEstate(apartment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,62 +12,62 @@ import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Singleton for management of database connections
|
* Singleton for management of database connections
|
||||||
*
|
*
|
||||||
* @author Michael von Riegen
|
* @author Michael von Riegen
|
||||||
* @version April 2009
|
* @version April 2009
|
||||||
*/
|
*/
|
||||||
class DB2ConnectionManager {
|
class DB2ConnectionManager {
|
||||||
|
|
||||||
// instance of Driver Manager
|
// instance of Driver Manager
|
||||||
private static DB2ConnectionManager _instance = null;
|
private static DB2ConnectionManager _instance = null;
|
||||||
|
|
||||||
// DB2 connection
|
// DB2 connection
|
||||||
private Connection _con;
|
private Connection _con;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes database connection
|
* Initializes database connection
|
||||||
*/
|
*/
|
||||||
private DB2ConnectionManager() {
|
private DB2ConnectionManager() {
|
||||||
try {
|
try {
|
||||||
// load properties from db2.properties file
|
// load properties from db2.properties file
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
URL url = ClassLoader.getSystemResource("db2.properties");
|
URL url = ClassLoader.getSystemResource("db2.properties");
|
||||||
FileInputStream stream = new FileInputStream(new File(url.toURI()));
|
FileInputStream stream = new FileInputStream(new File(url.toURI()));
|
||||||
properties.load(stream);
|
properties.load(stream);
|
||||||
stream.close();
|
stream.close();
|
||||||
|
|
||||||
String jdbcUser = properties.getProperty("jdbc_user");
|
String jdbcUser = properties.getProperty("jdbc_user");
|
||||||
String jdbcPass = properties.getProperty("jdbc_pass");
|
String jdbcPass = properties.getProperty("jdbc_pass");
|
||||||
String jdbcUrl = properties.getProperty("jdbc_url");
|
String jdbcUrl = properties.getProperty("jdbc_url");
|
||||||
|
|
||||||
// created connection to DB2 database
|
// created connection to DB2 database
|
||||||
Class.forName("com.ibm.db2.jcc.DB2Driver");
|
Class.forName("com.ibm.db2.jcc.DB2Driver");
|
||||||
_con = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPass);
|
_con = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPass);
|
||||||
|
|
||||||
} catch (IOException | ClassNotFoundException | SQLException | URISyntaxException e){
|
} catch (IOException | ClassNotFoundException | SQLException | URISyntaxException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the instance of the connection manager
|
* Returns the instance of the connection manager
|
||||||
*
|
*
|
||||||
* @return DB2ConnectionManager
|
* @return DB2ConnectionManager
|
||||||
*/
|
*/
|
||||||
static DB2ConnectionManager getInstance() {
|
static DB2ConnectionManager getInstance() {
|
||||||
if (_instance == null) {
|
if (_instance == null) {
|
||||||
_instance = new DB2ConnectionManager();
|
_instance = new DB2ConnectionManager();
|
||||||
}
|
}
|
||||||
return _instance;
|
return _instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the connection to the DB2 database
|
* Returns the connection to the DB2 database
|
||||||
*
|
*
|
||||||
* @return Connection
|
* @return Connection
|
||||||
*/
|
*/
|
||||||
Connection getConnection() {
|
Connection getConnection() {
|
||||||
return _con;
|
return _con;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,11 +23,11 @@ public class ORM {
|
||||||
private Connection _connection;
|
private Connection _connection;
|
||||||
|
|
||||||
private Map<Integer, EstateAgent> _agents;
|
private Map<Integer, EstateAgent> _agents;
|
||||||
private Map<String, EstateAgent> _agentsUsername;
|
private Map<String, EstateAgent> _agentsUsername;
|
||||||
private Map<Integer, Estate> _estates;
|
private Map<Integer, Estate> _estates;
|
||||||
private Map<Integer, Contract> _contracts;
|
private Map<Integer, Contract> _contracts;
|
||||||
private Map<Integer, Person> _persons;
|
private Map<Integer, Person> _persons;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the ORM.
|
* Initializes the ORM.
|
||||||
|
@ -45,7 +45,8 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Loads all objects from the database and returns a list of them.
|
* Loads all objects from the database and returns a list of them.
|
||||||
*
|
*
|
||||||
* @param objectType the type of objects to load
|
* @param objectType
|
||||||
|
* the type of objects to load
|
||||||
* @return a list of objects
|
* @return a list of objects
|
||||||
*/
|
*/
|
||||||
public List<?> getAll(Type objectType) {
|
public List<?> getAll(Type objectType) {
|
||||||
|
@ -54,7 +55,7 @@ public class ORM {
|
||||||
// create query
|
// create query
|
||||||
String selectSQL = "SELECT * FROM " + objectType.name();
|
String selectSQL = "SELECT * FROM " + objectType.name();
|
||||||
PreparedStatement pstmt = _connection.prepareStatement(selectSQL);
|
PreparedStatement pstmt = _connection.prepareStatement(selectSQL);
|
||||||
|
|
||||||
// execute query
|
// execute query
|
||||||
ResultSet rs = pstmt.executeQuery();
|
ResultSet rs = pstmt.executeQuery();
|
||||||
switch (objectType) {
|
switch (objectType) {
|
||||||
|
@ -84,7 +85,7 @@ public class ORM {
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return objects;
|
return objects;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,13 +140,16 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Process a select all query for estates.
|
* Process a select all query for estates.
|
||||||
*
|
*
|
||||||
* @param rs the result set of such a query
|
* @param rs
|
||||||
|
* the result set of such a query
|
||||||
* @return a list of estates
|
* @return a list of estates
|
||||||
* @throws SQLException when an error occurs during the rs.next call
|
*
|
||||||
|
* @throws SQLException
|
||||||
|
* when an error occurs during the rs.next call
|
||||||
*/
|
*/
|
||||||
private List<Estate> processEstates(ResultSet rs) throws SQLException {
|
private List<Estate> processEstates(ResultSet rs) throws SQLException {
|
||||||
List<Estate> estates = new ArrayList<>();
|
List<Estate> estates = new ArrayList<>();
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
Estate estate = new Estate();
|
Estate estate = new Estate();
|
||||||
estate.setId(rs.getInt("ID"));
|
estate.setId(rs.getInt("ID"));
|
||||||
|
@ -155,7 +159,7 @@ public class ORM {
|
||||||
estate.setStreetNumber(rs.getInt("streetNumber"));
|
estate.setStreetNumber(rs.getInt("streetNumber"));
|
||||||
estate.setSquareArea(rs.getInt("squareArea"));
|
estate.setSquareArea(rs.getInt("squareArea"));
|
||||||
estate.setAgent(rs.getInt("agent"));
|
estate.setAgent(rs.getInt("agent"));
|
||||||
|
|
||||||
estates.add(estate);
|
estates.add(estate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,13 +169,17 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Process a select all query for houses.
|
* Process a select all query for houses.
|
||||||
*
|
*
|
||||||
* @param rs the result set of such a query
|
* @param rs
|
||||||
* @param estates a list of estates
|
* the result set of such a query
|
||||||
|
* @param estates
|
||||||
|
* a list of estates
|
||||||
* @return a list of houses
|
* @return a list of houses
|
||||||
* @throws SQLException when an error occurs during the rs.next call
|
*
|
||||||
|
* @throws SQLException
|
||||||
|
* when an error occurs during the rs.next call
|
||||||
*/
|
*/
|
||||||
private List<House> processHouses(ResultSet rs, List<?> estates) throws SQLException {
|
private List<House> processHouses(ResultSet rs, List<?> estates) throws SQLException {
|
||||||
List<House> houses = new ArrayList<>();
|
List<House> houses = new ArrayList<>();
|
||||||
Map<Integer, House> housesMap = new HashMap<>();
|
Map<Integer, House> housesMap = new HashMap<>();
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
|
@ -205,13 +213,17 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Process a select all query for houses.
|
* Process a select all query for houses.
|
||||||
*
|
*
|
||||||
* @param rs the result set of such a query
|
* @param rs
|
||||||
* @param estates a list of estates
|
* the result set of such a query
|
||||||
|
* @param estates
|
||||||
|
* a list of estates
|
||||||
* @return a list of houses
|
* @return a list of houses
|
||||||
* @throws SQLException when an error occurs during the rs.next call
|
*
|
||||||
|
* @throws SQLException
|
||||||
|
* when an error occurs during the rs.next call
|
||||||
*/
|
*/
|
||||||
private List<Apartment> processApartments(ResultSet rs, List<?> estates) throws SQLException {
|
private List<Apartment> processApartments(ResultSet rs, List<?> estates) throws SQLException {
|
||||||
List<Apartment> apartments = new ArrayList<>();
|
List<Apartment> apartments = new ArrayList<>();
|
||||||
Map<Integer, Apartment> apartmentsMap = new HashMap<>();
|
Map<Integer, Apartment> apartmentsMap = new HashMap<>();
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
|
@ -247,9 +259,12 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Processes a select all query for estate agents.
|
* Processes a select all query for estate agents.
|
||||||
*
|
*
|
||||||
* @param rs the result set of such a query
|
* @param rs
|
||||||
|
* the result set of such a query
|
||||||
* @return a list of agents
|
* @return a list of agents
|
||||||
* @throws SQLException when an error occurs during the rs.next call
|
*
|
||||||
|
* @throws SQLException
|
||||||
|
* when an error occurs during the rs.next call
|
||||||
*/
|
*/
|
||||||
private List<EstateAgent> processAgents(ResultSet rs) throws SQLException {
|
private List<EstateAgent> processAgents(ResultSet rs) throws SQLException {
|
||||||
List<EstateAgent> agents = new ArrayList<>();
|
List<EstateAgent> agents = new ArrayList<>();
|
||||||
|
@ -260,7 +275,7 @@ public class ORM {
|
||||||
agent.setAddress(rs.getString("address"));
|
agent.setAddress(rs.getString("address"));
|
||||||
agent.setLogin(rs.getString("login"));
|
agent.setLogin(rs.getString("login"));
|
||||||
agent.setPassword(rs.getString("password"));
|
agent.setPassword(rs.getString("password"));
|
||||||
|
|
||||||
_agents.put(agent.getId(), agent);
|
_agents.put(agent.getId(), agent);
|
||||||
_agentsUsername.put(agent.getLogin(), agent);
|
_agentsUsername.put(agent.getLogin(), agent);
|
||||||
agents.add(agent);
|
agents.add(agent);
|
||||||
|
@ -272,9 +287,12 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Process a select all query for contracts.
|
* Process a select all query for contracts.
|
||||||
*
|
*
|
||||||
* @param rs the result set of such a query
|
* @param rs
|
||||||
|
* the result set of such a query
|
||||||
* @return a list of contracts
|
* @return a list of contracts
|
||||||
* @throws SQLException when an error occurs during the rs.next call
|
*
|
||||||
|
* @throws SQLException
|
||||||
|
* when an error occurs during the rs.next call
|
||||||
*/
|
*/
|
||||||
private List<Contract> processContracts(ResultSet rs) throws SQLException {
|
private List<Contract> processContracts(ResultSet rs) throws SQLException {
|
||||||
List<Contract> contracts = new ArrayList<>();
|
List<Contract> contracts = new ArrayList<>();
|
||||||
|
@ -294,9 +312,12 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Process a select all query for persons.
|
* Process a select all query for persons.
|
||||||
*
|
*
|
||||||
* @param rs the result set of such a query
|
* @param rs
|
||||||
|
* the result set of such a query
|
||||||
* @return a list of persons
|
* @return a list of persons
|
||||||
* @throws SQLException when an error occurs during the rs.next call
|
*
|
||||||
|
* @throws SQLException
|
||||||
|
* when an error occurs during the rs.next call
|
||||||
*/
|
*/
|
||||||
private List<Person> processPersons(ResultSet rs) throws SQLException {
|
private List<Person> processPersons(ResultSet rs) throws SQLException {
|
||||||
List<Person> persons = new ArrayList<>();
|
List<Person> persons = new ArrayList<>();
|
||||||
|
@ -317,7 +338,8 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Loads the contract with the given ID from database and returns the corresponding object.
|
* Loads the contract with the given ID from database and returns the corresponding object.
|
||||||
*
|
*
|
||||||
* @param ID the id of the contract to load
|
* @param ID
|
||||||
|
* the id of the contract to load
|
||||||
* @return the Contract or null if there is no such object
|
* @return the Contract or null if there is no such object
|
||||||
*/
|
*/
|
||||||
public Contract getContract(int ID) {
|
public Contract getContract(int ID) {
|
||||||
|
@ -325,25 +347,25 @@ public class ORM {
|
||||||
return _contracts.get(ID);
|
return _contracts.get(ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
String selectSQLPurchase = "SELECT * FROM PURCHASECONTRACT " +
|
String selectSQLPurchase = "SELECT * FROM PURCHASECONTRACT " +
|
||||||
"LEFT JOIN CONTRACT ON PURCHASECONTRACT.CONTRACTNUMBER = CONTRACT.CONTRACTNUMBER " +
|
"LEFT JOIN CONTRACT ON PURCHASECONTRACT.CONTRACTNUMBER = CONTRACT.CONTRACTNUMBER " +
|
||||||
"LEFT JOIN SALES ON PURCHASECONTRACT.CONTRACTNUMBER = SALES.CONTRACTNUMBER " +
|
"LEFT JOIN SALES ON PURCHASECONTRACT.CONTRACTNUMBER = SALES.CONTRACTNUMBER " +
|
||||||
"WHERE PURCHASECONTRACT.CONTRACTNUMBER = ?";
|
"WHERE PURCHASECONTRACT.CONTRACTNUMBER = ?";
|
||||||
String selectSQLTenancy = "SELECT * FROM TENANCYCONTRACT " +
|
String selectSQLTenancy = "SELECT * FROM TENANCYCONTRACT " +
|
||||||
"LEFT JOIN CONTRACT ON TENANCYCONTRACT.CONTRACTNUMBER = CONTRACT.CONTRACTNUMBER " +
|
"LEFT JOIN CONTRACT ON TENANCYCONTRACT.CONTRACTNUMBER = CONTRACT.CONTRACTNUMBER " +
|
||||||
"LEFT JOIN RENTALS ON TENANCYCONTRACT.CONTRACTNUMBER = RENTALS.CONTRACTNUMBER " +
|
"LEFT JOIN RENTALS ON TENANCYCONTRACT.CONTRACTNUMBER = RENTALS.CONTRACTNUMBER " +
|
||||||
"WHERE TENANCYCONTRACT.CONTRACTNUMBER = ?";
|
"WHERE TENANCYCONTRACT.CONTRACTNUMBER = ?";
|
||||||
|
|
||||||
String countPurchase = "SELECT COUNT(contractNumber) AS count FROM PURCHASECONTRACT WHERE contractNumber = ?";
|
String countPurchase = "SELECT COUNT(contractNumber) AS count FROM PURCHASECONTRACT WHERE contractNumber = ?";
|
||||||
String countTenancy = "SELECT COUNT(contractNumber) AS count FROM TENANCYCONTRACT WHERE contractNumber = ?";
|
String countTenancy = "SELECT COUNT(contractNumber) AS count FROM TENANCYCONTRACT WHERE contractNumber = ?";
|
||||||
try {
|
try {
|
||||||
// try purchase contract first
|
// try purchase contract first
|
||||||
PreparedStatement preparedStatementCount = _connection.prepareStatement(countPurchase);
|
PreparedStatement preparedStatementCount = _connection.prepareStatement(countPurchase);
|
||||||
preparedStatementCount.setInt(1, ID);
|
preparedStatementCount.setInt(1, ID);
|
||||||
ResultSet rs = preparedStatementCount.executeQuery();
|
ResultSet rs = preparedStatementCount.executeQuery();
|
||||||
rs.next();
|
rs.next();
|
||||||
int count = rs.getInt("count");
|
int count = rs.getInt("count");
|
||||||
String type = "None";
|
String type = "None";
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
// try tenancy contract next
|
// try tenancy contract next
|
||||||
preparedStatementCount = _connection.prepareStatement(countTenancy);
|
preparedStatementCount = _connection.prepareStatement(countTenancy);
|
||||||
|
@ -354,15 +376,14 @@ public class ORM {
|
||||||
if (count == 1) {
|
if (count == 1) {
|
||||||
type = "TenancyContract";
|
type = "TenancyContract";
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
type = "PurchaseContract";
|
type = "PurchaseContract";
|
||||||
}
|
}
|
||||||
rs.close();
|
rs.close();
|
||||||
preparedStatementCount.close();
|
preparedStatementCount.close();
|
||||||
|
|
||||||
PreparedStatement pstmt;
|
PreparedStatement pstmt;
|
||||||
Contract contract;
|
Contract contract;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "PurchaseContract":
|
case "PurchaseContract":
|
||||||
pstmt = _connection.prepareStatement(selectSQLPurchase);
|
pstmt = _connection.prepareStatement(selectSQLPurchase);
|
||||||
|
@ -414,20 +435,21 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Loads the estate with the given ID from database and returns the corresponding object.
|
* Loads the estate with the given ID from database and returns the corresponding object.
|
||||||
*
|
*
|
||||||
* @param ID the id of the estate to load
|
* @param ID
|
||||||
|
* the id of the estate to load
|
||||||
* @return the Estate or null if there is no such object
|
* @return the Estate or null if there is no such object
|
||||||
*/
|
*/
|
||||||
public Estate getEstate(int ID) {
|
public Estate getEstate(int ID) {
|
||||||
if (_estates.containsKey(ID)) {
|
if (_estates.containsKey(ID)) {
|
||||||
return _estates.get(ID);
|
return _estates.get(ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
String selectSQLHouse = "SELECT * FROM HOUSE LEFT JOIN ESTATE ON HOUSE.ID = ESTATE.ID " +
|
String selectSQLHouse = "SELECT * FROM HOUSE LEFT JOIN ESTATE ON HOUSE.ID = ESTATE.ID " +
|
||||||
"WHERE HOUSE.ID = ?";
|
"WHERE HOUSE.ID = ?";
|
||||||
String selectSQLApartment = "SELECT * FROM APARTMENT LEFT JOIN ESTATE ON APARTMENT.ID = ESTATE.ID " +
|
String selectSQLApartment = "SELECT * FROM APARTMENT LEFT JOIN ESTATE ON APARTMENT.ID = ESTATE.ID " +
|
||||||
"WHERE APARTMENT.ID = ?";
|
"WHERE APARTMENT.ID = ?";
|
||||||
|
|
||||||
String countHouse = "SELECT COUNT(ID) AS count FROM HOUSE WHERE ID = ?";
|
String countHouse = "SELECT COUNT(ID) AS count FROM HOUSE WHERE ID = ?";
|
||||||
String countApartment = "SELECT COUNT(ID) AS count FROM APARTMENT WHERE ID = ?";
|
String countApartment = "SELECT COUNT(ID) AS count FROM APARTMENT WHERE ID = ?";
|
||||||
try {
|
try {
|
||||||
// try house first
|
// try house first
|
||||||
|
@ -435,8 +457,8 @@ public class ORM {
|
||||||
preparedStatementCount.setInt(1, ID);
|
preparedStatementCount.setInt(1, ID);
|
||||||
ResultSet rs = preparedStatementCount.executeQuery();
|
ResultSet rs = preparedStatementCount.executeQuery();
|
||||||
rs.next();
|
rs.next();
|
||||||
int count = rs.getInt("count");
|
int count = rs.getInt("count");
|
||||||
String type = "None";
|
String type = "None";
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
// try apartment next
|
// try apartment next
|
||||||
preparedStatementCount = _connection.prepareStatement(countApartment);
|
preparedStatementCount = _connection.prepareStatement(countApartment);
|
||||||
|
@ -447,15 +469,14 @@ public class ORM {
|
||||||
if (count == 1) {
|
if (count == 1) {
|
||||||
type = "Apartment";
|
type = "Apartment";
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
type = "House";
|
type = "House";
|
||||||
}
|
}
|
||||||
rs.close();
|
rs.close();
|
||||||
preparedStatementCount.close();
|
preparedStatementCount.close();
|
||||||
|
|
||||||
PreparedStatement pstmt;
|
PreparedStatement pstmt;
|
||||||
Estate estate;
|
Estate estate;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "House":
|
case "House":
|
||||||
pstmt = _connection.prepareStatement(selectSQLHouse);
|
pstmt = _connection.prepareStatement(selectSQLHouse);
|
||||||
|
@ -472,7 +493,7 @@ public class ORM {
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
estate.setId(ID);
|
estate.setId(ID);
|
||||||
estate.setCity(rs.getString("city"));
|
estate.setCity(rs.getString("city"));
|
||||||
|
@ -494,7 +515,7 @@ public class ORM {
|
||||||
((Apartment) estate).setBalcony(rs.getBoolean("balcony"));
|
((Apartment) estate).setBalcony(rs.getBoolean("balcony"));
|
||||||
((Apartment) estate).setBuiltinKitchen(rs.getBoolean("builtInKitchen"));
|
((Apartment) estate).setBuiltinKitchen(rs.getBoolean("builtInKitchen"));
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.close();
|
rs.close();
|
||||||
pstmt.close();
|
pstmt.close();
|
||||||
_estates.put(ID, estate);
|
_estates.put(ID, estate);
|
||||||
|
@ -511,7 +532,8 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Loads the estate agent with the given ID from database and returns the corresponding object.
|
* Loads the estate agent with the given ID from database and returns the corresponding object.
|
||||||
*
|
*
|
||||||
* @param ID the ID of the agent to load
|
* @param ID
|
||||||
|
* the ID of the agent to load
|
||||||
* @return the EstateAgent or null if there is no such agent
|
* @return the EstateAgent or null if there is no such agent
|
||||||
*/
|
*/
|
||||||
public EstateAgent getAgent(int ID) {
|
public EstateAgent getAgent(int ID) {
|
||||||
|
@ -524,7 +546,7 @@ public class ORM {
|
||||||
String selectSQL = "SELECT * FROM ESTATEAGENT WHERE ID = ?";
|
String selectSQL = "SELECT * FROM ESTATEAGENT WHERE ID = ?";
|
||||||
PreparedStatement pstmt = _connection.prepareStatement(selectSQL);
|
PreparedStatement pstmt = _connection.prepareStatement(selectSQL);
|
||||||
pstmt.setInt(1, ID);
|
pstmt.setInt(1, ID);
|
||||||
|
|
||||||
return getAgent(pstmt);
|
return getAgent(pstmt);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -536,7 +558,8 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Loads the person with the given ID from database and returns the corresponding object.
|
* Loads the person with the given ID from database and returns the corresponding object.
|
||||||
*
|
*
|
||||||
* @param ID the ID of the person to load
|
* @param ID
|
||||||
|
* the ID of the person to load
|
||||||
* @return the Person or null if there is no such agent
|
* @return the Person or null if there is no such agent
|
||||||
*/
|
*/
|
||||||
public Person getPerson(int ID) {
|
public Person getPerson(int ID) {
|
||||||
|
@ -550,7 +573,7 @@ public class ORM {
|
||||||
PreparedStatement pstmt = _connection.prepareStatement(selectSQL);
|
PreparedStatement pstmt = _connection.prepareStatement(selectSQL);
|
||||||
pstmt.setInt(1, ID);
|
pstmt.setInt(1, ID);
|
||||||
ResultSet rs = pstmt.executeQuery();
|
ResultSet rs = pstmt.executeQuery();
|
||||||
Person person;
|
Person person;
|
||||||
|
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
person = new Person();
|
person = new Person();
|
||||||
|
@ -576,7 +599,8 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Loads the estate agent with the given username from database and returns the corresponding object,
|
* Loads the estate agent with the given username from database and returns the corresponding object,
|
||||||
*
|
*
|
||||||
* @param username the username of the estate agent
|
* @param username
|
||||||
|
* the username of the estate agent
|
||||||
* @return the EstateAgent or null if there is no such agent
|
* @return the EstateAgent or null if there is no such agent
|
||||||
*/
|
*/
|
||||||
public EstateAgent getAgent(String username) {
|
public EstateAgent getAgent(String username) {
|
||||||
|
@ -601,12 +625,12 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Executes the given statement and returns an estate agent.
|
* Executes the given statement and returns an estate agent.
|
||||||
*
|
*
|
||||||
* @param pstmt the prepared statement with parameters already set
|
* @param pstmt
|
||||||
|
* the prepared statement with parameters already set
|
||||||
* @return the EstateAgent or null
|
* @return the EstateAgent or null
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private EstateAgent getAgent(PreparedStatement pstmt)
|
private EstateAgent getAgent(PreparedStatement pstmt) {
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
// execute query
|
// execute query
|
||||||
ResultSet rs = pstmt.executeQuery();
|
ResultSet rs = pstmt.executeQuery();
|
||||||
|
@ -618,10 +642,10 @@ public class ORM {
|
||||||
agent.setAddress(rs.getString("address"));
|
agent.setAddress(rs.getString("address"));
|
||||||
agent.setLogin(rs.getString("login"));
|
agent.setLogin(rs.getString("login"));
|
||||||
agent.setPassword(rs.getString("password"));
|
agent.setPassword(rs.getString("password"));
|
||||||
|
|
||||||
rs.close();
|
rs.close();
|
||||||
pstmt.close();
|
pstmt.close();
|
||||||
|
|
||||||
_agents.put(agent.getId(), agent);
|
_agents.put(agent.getId(), agent);
|
||||||
_agentsUsername.put(agent.getLogin(), agent);
|
_agentsUsername.put(agent.getLogin(), agent);
|
||||||
return agent;
|
return agent;
|
||||||
|
@ -636,10 +660,10 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Deletes the given agent from the database.
|
* Deletes the given agent from the database.
|
||||||
*
|
*
|
||||||
* @param agent the agent that should be deleted
|
* @param agent
|
||||||
|
* the agent that should be deleted
|
||||||
*/
|
*/
|
||||||
public void delete(EstateAgent agent)
|
public void delete(EstateAgent agent) {
|
||||||
{
|
|
||||||
if (agent.getId() == -1) {
|
if (agent.getId() == -1) {
|
||||||
System.err.println("This agent is not yet persisted to the database and cannot be deleted.");
|
System.err.println("This agent is not yet persisted to the database and cannot be deleted.");
|
||||||
return;
|
return;
|
||||||
|
@ -659,7 +683,8 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Deletes an estate from the database.
|
* Deletes an estate from the database.
|
||||||
*
|
*
|
||||||
* @param estate the estate to be deleted
|
* @param estate
|
||||||
|
* the estate to be deleted
|
||||||
*/
|
*/
|
||||||
public void delete(Estate estate) {
|
public void delete(Estate estate) {
|
||||||
if (estate.getId() == -1) {
|
if (estate.getId() == -1) {
|
||||||
|
@ -682,15 +707,16 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Deletes an object from the database.
|
* Deletes an object from the database.
|
||||||
*
|
*
|
||||||
* @param sql the sql used for deletion
|
* @param sql
|
||||||
* @param id the id of the object to be deleted
|
* the sql used for deletion
|
||||||
|
* @param id
|
||||||
|
* the id of the object to be deleted
|
||||||
*/
|
*/
|
||||||
private void delete(String sql, int id)
|
private void delete(String sql, int id) {
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
PreparedStatement pstmt = _connection.prepareStatement(sql);
|
PreparedStatement pstmt = _connection.prepareStatement(sql);
|
||||||
pstmt.setInt(1, id);
|
pstmt.setInt(1, id);
|
||||||
|
|
||||||
// execute query
|
// execute query
|
||||||
pstmt.executeUpdate();
|
pstmt.executeUpdate();
|
||||||
pstmt.close();
|
pstmt.close();
|
||||||
|
@ -702,13 +728,14 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Persists the given agent.
|
* Persists the given agent.
|
||||||
*
|
*
|
||||||
* @param agent the agent that should be persisted
|
* @param agent
|
||||||
|
* the agent that should be persisted
|
||||||
*/
|
*/
|
||||||
public void persist(EstateAgent agent)
|
public void persist(EstateAgent agent) {
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
if (agent.getId() == -1) {
|
if (agent.getId() == -1) {
|
||||||
String insertSQL = "INSERT INTO ESTATEAGENT (name, address, login, password) VALUES (?, ?, ?, ?)";
|
String insertSQL
|
||||||
|
= "INSERT INTO ESTATEAGENT (name, address, login, password) VALUES (?, ?, ?, ?)";
|
||||||
PreparedStatement pstmt = _connection.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS);
|
PreparedStatement pstmt = _connection.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS);
|
||||||
|
|
||||||
pstmt.setString(1, agent.getName());
|
pstmt.setString(1, agent.getName());
|
||||||
|
@ -716,7 +743,7 @@ public class ORM {
|
||||||
pstmt.setString(3, agent.getLogin());
|
pstmt.setString(3, agent.getLogin());
|
||||||
pstmt.setString(4, agent.getPassword());
|
pstmt.setString(4, agent.getPassword());
|
||||||
pstmt.executeUpdate();
|
pstmt.executeUpdate();
|
||||||
|
|
||||||
ResultSet rs = pstmt.getGeneratedKeys();
|
ResultSet rs = pstmt.getGeneratedKeys();
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
agent.setId(rs.getInt(1));
|
agent.setId(rs.getInt(1));
|
||||||
|
@ -726,13 +753,13 @@ public class ORM {
|
||||||
pstmt.close();
|
pstmt.close();
|
||||||
} else {
|
} else {
|
||||||
// create query
|
// create query
|
||||||
String updateSQL = "UPDATE ESTATEAGENT SET name = ?, address = ?, password = ? WHERE ID = ?";
|
String updateSQL = "UPDATE ESTATEAGENT SET name = ?, address = ?, password = ? WHERE ID = ?";
|
||||||
PreparedStatement pstmt = _connection.prepareStatement(updateSQL);
|
PreparedStatement pstmt = _connection.prepareStatement(updateSQL);
|
||||||
pstmt.setString(1, agent.getName());
|
pstmt.setString(1, agent.getName());
|
||||||
pstmt.setString(2, agent.getAddress());
|
pstmt.setString(2, agent.getAddress());
|
||||||
pstmt.setString(3, agent.getPassword());
|
pstmt.setString(3, agent.getPassword());
|
||||||
pstmt.setInt(4, agent.getId());
|
pstmt.setInt(4, agent.getId());
|
||||||
|
|
||||||
// execute query
|
// execute query
|
||||||
pstmt.executeUpdate();
|
pstmt.executeUpdate();
|
||||||
pstmt.close();
|
pstmt.close();
|
||||||
|
@ -751,10 +778,10 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Persists the given estate.
|
* Persists the given estate.
|
||||||
*
|
*
|
||||||
* @param estate the estate that should be persisted
|
* @param estate
|
||||||
|
* the estate that should be persisted
|
||||||
*/
|
*/
|
||||||
public void persist(Estate estate)
|
public void persist(Estate estate) {
|
||||||
{
|
|
||||||
boolean changeFinished = false;
|
boolean changeFinished = false;
|
||||||
try {
|
try {
|
||||||
_connection.setAutoCommit(false);
|
_connection.setAutoCommit(false);
|
||||||
|
@ -778,9 +805,10 @@ public class ORM {
|
||||||
pstmt.close();
|
pstmt.close();
|
||||||
|
|
||||||
if (estate instanceof House) {
|
if (estate instanceof House) {
|
||||||
House house = (House) estate;
|
House house = (House) estate;
|
||||||
String insertSQLHouse = "INSERT INTO HOUSE (ID, price, garden, floors) VALUES (?, ?, ?, ?)";
|
String insertSQLHouse
|
||||||
PreparedStatement pstmtHouse = _connection.prepareStatement(insertSQLHouse);
|
= "INSERT INTO HOUSE (ID, price, garden, floors) VALUES (?, ?, ?, ?)";
|
||||||
|
PreparedStatement pstmtHouse = _connection.prepareStatement(insertSQLHouse);
|
||||||
pstmtHouse.setInt(1, house.getId());
|
pstmtHouse.setInt(1, house.getId());
|
||||||
pstmtHouse.setInt(2, house.getPrice());
|
pstmtHouse.setInt(2, house.getPrice());
|
||||||
pstmtHouse.setInt(3, house.hasGarden() ? 1 : 0);
|
pstmtHouse.setInt(3, house.hasGarden() ? 1 : 0);
|
||||||
|
@ -790,11 +818,11 @@ public class ORM {
|
||||||
pstmt.close();
|
pstmt.close();
|
||||||
pstmtHouse.close();
|
pstmtHouse.close();
|
||||||
changeFinished = true;
|
changeFinished = true;
|
||||||
}
|
} else if (estate instanceof Apartment) {
|
||||||
else if (estate instanceof Apartment) {
|
|
||||||
Apartment apartment = (Apartment) estate;
|
Apartment apartment = (Apartment) estate;
|
||||||
String insertSQLApartment = "INSERT INTO APARTMENT (ID, floor, rent, rooms, balcony, builtInKitchen) " +
|
String insertSQLApartment =
|
||||||
"VALUES (?, ?, ?, ?, ?, ?)";
|
"INSERT INTO APARTMENT (ID, floor, rent, rooms, balcony, builtInKitchen) " +
|
||||||
|
"VALUES (?, ?, ?, ?, ?, ?)";
|
||||||
PreparedStatement pstmtApartment = _connection.prepareStatement(insertSQLApartment);
|
PreparedStatement pstmtApartment = _connection.prepareStatement(insertSQLApartment);
|
||||||
pstmtApartment.setInt(1, apartment.getId());
|
pstmtApartment.setInt(1, apartment.getId());
|
||||||
pstmtApartment.setInt(2, apartment.getFloor());
|
pstmtApartment.setInt(2, apartment.getFloor());
|
||||||
|
@ -822,9 +850,10 @@ public class ORM {
|
||||||
pstmt.setInt(7, estate.getId());
|
pstmt.setInt(7, estate.getId());
|
||||||
|
|
||||||
if (estate instanceof House) {
|
if (estate instanceof House) {
|
||||||
House house = (House) estate;
|
House house = (House) estate;
|
||||||
String updateSQLHouse = "UPDATE HOUSE SET floors = ?, garden = ?, price = ? WHERE ID = ?";
|
String updateSQLHouse
|
||||||
PreparedStatement pstmtHouse = _connection.prepareStatement(updateSQLHouse);
|
= "UPDATE HOUSE SET floors = ?, garden = ?, price = ? WHERE ID = ?";
|
||||||
|
PreparedStatement pstmtHouse = _connection.prepareStatement(updateSQLHouse);
|
||||||
pstmtHouse.setInt(1, house.getFloors());
|
pstmtHouse.setInt(1, house.getFloors());
|
||||||
pstmtHouse.setInt(2, house.hasGarden() ? 1 : 0);
|
pstmtHouse.setInt(2, house.hasGarden() ? 1 : 0);
|
||||||
pstmtHouse.setInt(3, house.getPrice());
|
pstmtHouse.setInt(3, house.getPrice());
|
||||||
|
@ -834,8 +863,7 @@ public class ORM {
|
||||||
pstmt.close();
|
pstmt.close();
|
||||||
pstmtHouse.close();
|
pstmtHouse.close();
|
||||||
changeFinished = true;
|
changeFinished = true;
|
||||||
}
|
} else if (estate instanceof Apartment) {
|
||||||
else if (estate instanceof Apartment) {
|
|
||||||
Apartment apartment = (Apartment) estate;
|
Apartment apartment = (Apartment) estate;
|
||||||
String updateSQLApartment = "UPDATE APARTMENT SET floor = ?, rent = ?, rooms = ?, " +
|
String updateSQLApartment = "UPDATE APARTMENT SET floor = ?, rent = ?, rooms = ?, " +
|
||||||
"balcony = ?, builtInKitchen = ? WHERE ID = ?";
|
"balcony = ?, builtInKitchen = ? WHERE ID = ?";
|
||||||
|
@ -873,10 +901,10 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Persists the given contract.
|
* Persists the given contract.
|
||||||
*
|
*
|
||||||
* @param contract the contract that should be persisted
|
* @param contract
|
||||||
|
* the contract that should be persisted
|
||||||
*/
|
*/
|
||||||
public void persist(Contract contract)
|
public void persist(Contract contract) {
|
||||||
{
|
|
||||||
boolean changeFinished = false;
|
boolean changeFinished = false;
|
||||||
try {
|
try {
|
||||||
_connection.setAutoCommit(false);
|
_connection.setAutoCommit(false);
|
||||||
|
@ -907,7 +935,8 @@ public class ORM {
|
||||||
pstmtPurchase.executeUpdate();
|
pstmtPurchase.executeUpdate();
|
||||||
pstmtPurchase.close();
|
pstmtPurchase.close();
|
||||||
|
|
||||||
String insertSQLSale = "INSERT INTO SALES (contractNumber, house, person) VALUES (?, ?, ?)";
|
String insertSQLSale
|
||||||
|
= "INSERT INTO SALES (contractNumber, house, person) VALUES (?, ?, ?)";
|
||||||
PreparedStatement pstmtSale = _connection.prepareStatement(insertSQLSale);
|
PreparedStatement pstmtSale = _connection.prepareStatement(insertSQLSale);
|
||||||
pstmtSale.setInt(1, purchaseContract.getContractNo());
|
pstmtSale.setInt(1, purchaseContract.getContractNo());
|
||||||
pstmtSale.setInt(2, purchaseContract.getHouse());
|
pstmtSale.setInt(2, purchaseContract.getHouse());
|
||||||
|
@ -915,8 +944,7 @@ public class ORM {
|
||||||
pstmtSale.executeUpdate();
|
pstmtSale.executeUpdate();
|
||||||
pstmtSale.close();
|
pstmtSale.close();
|
||||||
changeFinished = true;
|
changeFinished = true;
|
||||||
}
|
} else if (contract instanceof TenancyContract) {
|
||||||
else if (contract instanceof TenancyContract) {
|
|
||||||
TenancyContract tenancyContract = (TenancyContract) contract;
|
TenancyContract tenancyContract = (TenancyContract) contract;
|
||||||
String insertSQLTenancyContract = "INSERT INTO TENANCYCONTRACT " +
|
String insertSQLTenancyContract = "INSERT INTO TENANCYCONTRACT " +
|
||||||
"(contractNumber, startDate, duration, additionalCosts) " +
|
"(contractNumber, startDate, duration, additionalCosts) " +
|
||||||
|
@ -928,8 +956,9 @@ public class ORM {
|
||||||
pstmtTenancy.setInt(4, tenancyContract.getAdditionalCost());
|
pstmtTenancy.setInt(4, tenancyContract.getAdditionalCost());
|
||||||
pstmtTenancy.executeUpdate();
|
pstmtTenancy.executeUpdate();
|
||||||
pstmtTenancy.close();
|
pstmtTenancy.close();
|
||||||
|
|
||||||
String insertSQLRental = "INSERT INTO RENTALS (contractNumber, apartment, person) VALUES (?, ?, ?)";
|
String insertSQLRental
|
||||||
|
= "INSERT INTO RENTALS (contractNumber, apartment, person) VALUES (?, ?, ?)";
|
||||||
PreparedStatement pstmtRental = _connection.prepareStatement(insertSQLRental);
|
PreparedStatement pstmtRental = _connection.prepareStatement(insertSQLRental);
|
||||||
pstmtRental.setInt(1, tenancyContract.getContractNo());
|
pstmtRental.setInt(1, tenancyContract.getContractNo());
|
||||||
pstmtRental.setInt(2, tenancyContract.getApartment());
|
pstmtRental.setInt(2, tenancyContract.getApartment());
|
||||||
|
@ -961,14 +990,14 @@ public class ORM {
|
||||||
/**
|
/**
|
||||||
* Persists the given person.
|
* Persists the given person.
|
||||||
*
|
*
|
||||||
* @param person the person that should be persisted
|
* @param person
|
||||||
|
* the person that should be persisted
|
||||||
*/
|
*/
|
||||||
public void persist(Person person)
|
public void persist(Person person) {
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
if (person.getId() == -1) {
|
if (person.getId() == -1) {
|
||||||
String insertSQL = "INSERT INTO PERSON (firstName, name, address) VALUES (?, ?, ?)";
|
String insertSQL = "INSERT INTO PERSON (firstName, name, address) VALUES (?, ?, ?)";
|
||||||
PreparedStatement pstmt = _connection.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS);
|
PreparedStatement pstmt = _connection.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS);
|
||||||
|
|
||||||
pstmt.setString(1, person.getFirstName());
|
pstmt.setString(1, person.getFirstName());
|
||||||
pstmt.setString(2, person.getName());
|
pstmt.setString(2, person.getName());
|
||||||
|
@ -984,8 +1013,8 @@ public class ORM {
|
||||||
pstmt.close();
|
pstmt.close();
|
||||||
} else {
|
} else {
|
||||||
// create query
|
// create query
|
||||||
String updateSQL = "UPDATE PERSON SET firstName = ?, name = ?, address = ? WHERE ID = ?";
|
String updateSQL = "UPDATE PERSON SET firstName = ?, name = ?, address = ? WHERE ID = ?";
|
||||||
PreparedStatement pstmt = _connection.prepareStatement(updateSQL);
|
PreparedStatement pstmt = _connection.prepareStatement(updateSQL);
|
||||||
pstmt.setString(1, person.getFirstName());
|
pstmt.setString(1, person.getFirstName());
|
||||||
pstmt.setString(2, person.getName());
|
pstmt.setString(2, person.getName());
|
||||||
pstmt.setString(3, person.getAddress());
|
pstmt.setString(3, person.getAddress());
|
||||||
|
|
Loading…
Reference in New Issue