From 9366ab384e5fd0a4f62a71f969e7c0657fe87491 Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Tue, 25 Apr 2017 16:09:55 +0200 Subject: [PATCH] Reformatted the codebase Signed-off-by: Jim Martens --- 02/src/de/dis2017/FormUtil.java | 113 ++--- 02/src/de/dis2017/Main.java | 403 +++++++++--------- 02/src/de/dis2017/Menu.java | 117 ++--- 02/src/de/dis2017/data/Apartment.java | 111 ++--- 02/src/de/dis2017/data/Contract.java | 81 ++-- 02/src/de/dis2017/data/Estate.java | 118 ++--- 02/src/de/dis2017/data/EstateAgent.java | 90 ++-- 02/src/de/dis2017/data/House.java | 81 ++-- 02/src/de/dis2017/data/Person.java | 61 +-- 02/src/de/dis2017/data/PurchaseContract.java | 73 ++-- 02/src/de/dis2017/data/TenancyContract.java | 92 ++-- .../dis2017/data/db/DB2ConnectionManager.java | 108 ++--- 02/src/de/dis2017/data/db/ORM.java | 245 ++++++----- 13 files changed, 883 insertions(+), 810 deletions(-) diff --git a/02/src/de/dis2017/FormUtil.java b/02/src/de/dis2017/FormUtil.java index 6e9a8b1..2252bfb 100644 --- a/02/src/de/dis2017/FormUtil.java +++ b/02/src/de/dis2017/FormUtil.java @@ -11,7 +11,8 @@ class FormUtil { /** * 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 */ static String readString(String label) { @@ -19,81 +20,87 @@ class FormUtil { } /** - * Reads a string from the console. - * - * @param label Label that is shown before the input - * @param defaultValue the default value in case an empty input is provided - * @return read string - */ - static String readString(String label, String defaultValue) { - String ret = null; - BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); - - try { - System.out.print(label + (!defaultValue.isEmpty() ? "[" + defaultValue + "]" : "") + ": "); - ret = stdin.readLine(); - ret = ret.isEmpty() ? defaultValue : ret; - } catch (IOException e) { - e.printStackTrace(); - } - - return ret; - } + * Reads a string from the console. + * + * @param label + * Label that is shown before the input + * @param defaultValue + * the default value in case an empty input is provided + * @return read string + */ + static String readString(String label, String defaultValue) { + String ret = null; + BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); + + try { + System.out.print(label + (!defaultValue.isEmpty() ? "[" + defaultValue + "]" : "") + ": "); + ret = stdin.readLine(); + ret = ret.isEmpty() ? defaultValue : ret; + } catch (IOException e) { + e.printStackTrace(); + } + + return ret; + } /** * Reads a password from the console. * * @return the entered password */ - static String readPassword() { - String password; + static String readPassword() { + String password; System.out.print("Password: "); password = String.valueOf(System.console().readPassword()); return password; } - /** + + /** * Reads a password from the console. * * @return the entered password */ - static String readPassword(String oldPassword) { - String password = readPassword(); - return password.isEmpty() ? oldPassword : password; + static String readPassword(String oldPassword) { + String password = readPassword(); + return password.isEmpty() ? oldPassword : password; } /** * 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 */ static int readInt(String label) { 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 defaultValue the default value - * @return read integer - */ - static int readInt(String label, int defaultValue) { - int ret = 0; - boolean finished = false; - String defaultValueStr = defaultValue == -1 ? "" : String.valueOf(defaultValue); - - while(!finished) { - String line = readString(label, defaultValueStr); - - try { - ret = Integer.parseInt(line); - finished = true; - } catch (NumberFormatException e) { - System.err.println("Invalid input: Please insert a valid number!"); - } - } - - return ret; - } + * @param label + * Label that is shown before the input + * @param defaultValue + * the default value + * @return read integer + */ + static int readInt(String label, int defaultValue) { + int ret = 0; + boolean finished = false; + String defaultValueStr = defaultValue == -1 ? "" : String.valueOf(defaultValue); + + while (!finished) { + String line = readString(label, defaultValueStr); + + try { + ret = Integer.parseInt(line); + finished = true; + } catch (NumberFormatException e) { + System.err.println("Invalid input: Please insert a valid number!"); + } + } + + return ret; + } } diff --git a/02/src/de/dis2017/Main.java b/02/src/de/dis2017/Main.java index 2080e29..628163a 100644 --- a/02/src/de/dis2017/Main.java +++ b/02/src/de/dis2017/Main.java @@ -22,115 +22,113 @@ import java.util.List; public class Main { private static ORM _orm; - /** - * Starts the application. - */ - public static void main(String[] args) { - _orm = new ORM(); - showMainMenu(); - } - - /** - * Shows the main menu. - */ - private static void showMainMenu() { - // menu options - final int MENU_AGENT = 0; - final int MENU_ESTATES = 1; - final int MENU_CONTRACTS = 2; - final int QUIT = 3; - - // create menu - Menu mainMenu = new Menu("Main menu"); - mainMenu.addEntry("EstateAgent management", MENU_AGENT); - mainMenu.addEntry("Estate management", MENU_ESTATES); - mainMenu.addEntry("Contract management", MENU_CONTRACTS); - mainMenu.addEntry("Quit", QUIT); - - // process input - while(true) { - int response = mainMenu.show(); - - switch(response) { - case MENU_AGENT: - if (checkPassword()) { + /** + * Starts the application. + */ + public static void main(String[] args) { + _orm = new ORM(); + showMainMenu(); + } + + /** + * Shows the main menu. + */ + private static void showMainMenu() { + // menu options + final int MENU_AGENT = 0; + final int MENU_ESTATES = 1; + final int MENU_CONTRACTS = 2; + final int QUIT = 3; + + // create menu + Menu mainMenu = new Menu("Main menu"); + mainMenu.addEntry("EstateAgent management", MENU_AGENT); + mainMenu.addEntry("Estate management", MENU_ESTATES); + mainMenu.addEntry("Contract management", MENU_CONTRACTS); + mainMenu.addEntry("Quit", QUIT); + + // process input + while (true) { + int response = mainMenu.show(); + + switch (response) { + case MENU_AGENT: + if (checkPassword()) { showEstateAgentMenu(); + } else { + System.out.println("The password was wrong."); } - else { - System.out.println("The password was wrong."); - } - break; - case MENU_ESTATES: - if (loginEstateAgent()) { + break; + case MENU_ESTATES: + if (loginEstateAgent()) { showEstateMenu(); + } else { + System.out.println("The username or password was wrong."); } - else { - System.out.println("The username or password was wrong."); - } - break; - case MENU_CONTRACTS: - showContractMenu(); - break; - case QUIT: - return; - } - } - } - - /** + break; + case MENU_CONTRACTS: + showContractMenu(); + break; + case QUIT: + return; + } + } + } + + /** * Checks the password for sudo-like menu areas. */ - private static boolean checkPassword() { - System.out.println("Please insert the sudo password. You are entering dangerous territory."); + private static boolean checkPassword() { + System.out.println("Please insert the sudo password. You are entering dangerous territory."); String passwordInput = FormUtil.readPassword(); - String sudoPassword = "ea-sudo"; + String sudoPassword = "ea-sudo"; return sudoPassword.equals(passwordInput); - } + } /** * Performs a login for an estate agent. * * @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."); String username = FormUtil.readString("Username"); String passwordInput = FormUtil.readPassword(); EstateAgent agent = _orm.getAgent(username); return agent != null && agent.getPassword().equals(passwordInput); } - - /** - * Shows the estate management menu. - */ - private static void showEstateMenu() { - // menu options - final int NEW_ESTATE = 0; - final int LIST_ESTATES = 1; - final int BACK = 2; - - // estate management menu - Menu estateMenu = new Menu("Estate management"); - estateMenu.addEntry("Create Estate", NEW_ESTATE); - estateMenu.addEntry("List Estates", LIST_ESTATES); - estateMenu.addEntry("Back to the main menu", BACK); - - // process input - while(true) { - int response = estateMenu.show(); - - switch(response) { - case NEW_ESTATE: - newEstate(); - break; - case LIST_ESTATES: - listEstates(); - break; - case BACK: - return; - } - } - } + + /** + * Shows the estate management menu. + */ + private static void showEstateMenu() { + // menu options + final int NEW_ESTATE = 0; + final int LIST_ESTATES = 1; + final int BACK = 2; + + // estate management menu + Menu estateMenu = new Menu("Estate management"); + estateMenu.addEntry("Create Estate", NEW_ESTATE); + estateMenu.addEntry("List Estates", LIST_ESTATES); + estateMenu.addEntry("Back to the main menu", BACK); + + // process input + while (true) { + int response = estateMenu.show(); + + switch (response) { + case NEW_ESTATE: + newEstate(); + break; + case LIST_ESTATES: + listEstates(); + break; + case BACK: + return; + } + } + } /** * Creates a new estate agent after the usesr has entered the necessary data. @@ -138,9 +136,9 @@ public class Main { private static void newEstate() { 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"); - Estate estate = new Estate(); + Estate estate = new Estate(); estate.setStreet(FormUtil.readString("Street")); estate.setStreetNumber(FormUtil.readInt("Street Number")); estate.setPostalCode(FormUtil.readString("Postal Code")); @@ -156,8 +154,7 @@ public class Main { apartment.setBalcony(input.equals("Y") || input.equals("y")); input = FormUtil.readString("Built-in Kitchen(Y/N)"); apartment.setBuiltinKitchen(input.equals("Y") || input.equals("y")); - } - else { + } else { House house = new House(estate); house.setPrice(FormUtil.readInt("Price")); house.setFloors(FormUtil.readInt("Floors")); @@ -172,9 +169,9 @@ public class Main { /** * Lists estates. */ - private static void listEstates() { - List estates = _orm.getAll(Type.ESTATE); - Menu listEstates = new Menu("Please select the estate you want to modify or delete"); + private static void listEstates() { + List estates = _orm.getAll(Type.ESTATE); + Menu listEstates = new Menu("Please select the estate you want to modify or delete"); System.out.println("List of Estates"); final int BACK = 0; @@ -188,7 +185,7 @@ public class Main { listEstates.addEntry("Back to the Estate management menu", BACK); // process input - while(true) { + while (true) { int response = listEstates.show(); switch (response) { @@ -199,13 +196,14 @@ public class Main { break; } } - + } /** * 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) { Estate estate = _orm.getEstate(id); @@ -215,14 +213,14 @@ public class Main { final int MODIFY = 1; 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?"); showEstateMenu.addEntry("Modify", MODIFY); showEstateMenu.addEntry("Delete", DELETE); showEstateMenu.addEntry("Back to the list of estates", BACK); - while(true) { + while (true) { int response = showEstateMenu.show(); switch (response) { @@ -241,7 +239,8 @@ public class Main { /** * 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) { System.out.println("------------------"); @@ -257,8 +256,7 @@ public class Main { System.out.println("Price: " + house.getPrice()); System.out.println("Floors: " + house.getFloors()); System.out.println("Garden: " + (house.hasGarden() ? "yes" : "false")); - } - else if (estate instanceof Apartment) { + } else if (estate instanceof Apartment) { Apartment apartment = (Apartment) estate; System.out.println("Floor: " + apartment.getFloor()); System.out.println("Rooms: " + apartment.getRooms()); @@ -276,7 +274,7 @@ public class Main { List agents = _orm.getAll(Type.ESTATEAGENT); System.out.println("List of EstateAgents"); System.out.println("------------------"); - + for (Object o : agents) { EstateAgent agent = (EstateAgent) o; System.out.println("ID: " + agent.getId() + ", Name: " + agent.getName()); @@ -287,35 +285,35 @@ public class Main { /** * Modify estate. * - * @param estate the modified estate + * @param estate + * the modified estate */ private static void modifyEstate(Estate estate) { System.out.println("Modify Estate"); printEstateDetails(estate); printListOfAgents(); - + estate.setStreet(FormUtil.readString("Street", estate.getStreet())); estate.setStreetNumber(FormUtil.readInt("Street Number", estate.getStreetNumber())); estate.setPostalCode(FormUtil.readString("Postal Code", estate.getPostalCode())); estate.setCity(FormUtil.readString("City", estate.getCity())); estate.setSquareArea(FormUtil.readInt("Square Area", estate.getSquareArea())); estate.setAgent(FormUtil.readInt("EstateAgent ID", estate.getAgent())); - + if (estate instanceof Apartment) { Apartment apartment = (Apartment) estate; apartment.setFloor(FormUtil.readInt("Floor", apartment.getFloor())); apartment.setRooms(FormUtil.readInt("Rooms", apartment.getRooms())); 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")); - 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")); - } - else if (estate instanceof House){ + } else if (estate instanceof House) { House house = (House) estate; house.setFloors(FormUtil.readInt("Floors", house.getFloors())); 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")); } @@ -328,68 +326,69 @@ public class Main { /** * 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) { _orm.delete(estate); System.out.println("Estate with the ID " + estate.getId() + " was deleted."); } - - /** - * Shows the estate agent management. - */ - private static void showEstateAgentMenu() { - // menu options - final int NEW_AGENT = 0; + + /** + * Shows the estate agent management. + */ + private static void showEstateAgentMenu() { + // menu options + final int NEW_AGENT = 0; final int LIST_AGENTS = 1; - final int BACK = 2; - - // estate management menu - Menu estateAgentMenu = new Menu("EstateAgent management"); - estateAgentMenu.addEntry("Create EstateAgent", NEW_AGENT); + final int BACK = 2; + + // estate management menu + Menu estateAgentMenu = new Menu("EstateAgent management"); + estateAgentMenu.addEntry("Create EstateAgent", NEW_AGENT); estateAgentMenu.addEntry("List EstateAgents", LIST_AGENTS); - estateAgentMenu.addEntry("Back to the main menu", BACK); - - // process input - while(true) { - int response = estateAgentMenu.show(); - - switch(response) { - case NEW_AGENT: - newEstateAgent(); - break; + estateAgentMenu.addEntry("Back to the main menu", BACK); + + // process input + while (true) { + int response = estateAgentMenu.show(); + + switch (response) { + case NEW_AGENT: + newEstateAgent(); + break; case LIST_AGENTS: listEstateAgents(); break; - case BACK: - return; - } - } - } - - /** - * Creates a new estate agent after the user has entered the necessary data. - */ - private static void newEstateAgent() { - EstateAgent agent = new EstateAgent(); - - agent.setName(FormUtil.readString("Name")); - agent.setAddress(FormUtil.readString("Address")); - agent.setLogin(FormUtil.readString("Login")); - agent.setPassword(FormUtil.readPassword()); - - _orm.persist(agent); - - System.out.println("EstateAgent with the ID " + agent.getId() + " was created."); - } + case BACK: + return; + } + } + } + + /** + * Creates a new estate agent after the user has entered the necessary data. + */ + private static void newEstateAgent() { + EstateAgent agent = new EstateAgent(); + + agent.setName(FormUtil.readString("Name")); + agent.setAddress(FormUtil.readString("Address")); + agent.setLogin(FormUtil.readString("Login")); + agent.setPassword(FormUtil.readPassword()); + + _orm.persist(agent); + + System.out.println("EstateAgent with the ID " + agent.getId() + " was created."); + } /** * List estate agents. */ - private static void listEstateAgents() { - // get all agents - List agents = _orm.getAll(Type.ESTATEAGENT); - Menu listEstateAgents = new Menu("Please select the estate agent you want to modify or delete"); + private static void listEstateAgents() { + // get all agents + List agents = _orm.getAll(Type.ESTATEAGENT); + Menu listEstateAgents = new Menu("Please select the estate agent you want to modify or delete"); System.out.println("List of EstateAgents"); final int BACK = 0; @@ -401,7 +400,7 @@ public class Main { listEstateAgents.addEntry("Back to the EstateAgent management menu", BACK); // process input - while(true) { + while (true) { int response = listEstateAgents.show(); switch (response) { @@ -417,11 +416,12 @@ public class Main { /** * 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) { EstateAgent agent = _orm.getAgent(id); - + System.out.println("EstateAgent"); System.out.println("------------------"); System.out.println("ID: " + id); @@ -432,14 +432,14 @@ public class Main { final int MODIFY = 1; 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?"); showEstateAgentMenu.addEntry("Modify", MODIFY); showEstateAgentMenu.addEntry("Delete", DELETE); showEstateAgentMenu.addEntry("Back to the list of agents", BACK); - while(true) { + while (true) { int response = showEstateAgentMenu.show(); switch (response) { @@ -458,24 +458,25 @@ public class Main { /** * Modify estate agent. * - * @param agent the modified agent + * @param agent + * the modified agent */ private static void modifyEstateAgent(EstateAgent agent) { - System.out.println("Modify EstateAgent"); - System.out.println("------------------"); - System.out.println("ID: " + agent.getId()); - System.out.println("Name: " + agent.getName()); - System.out.println("Address: " + agent.getAddress()); + System.out.println("Modify EstateAgent"); + System.out.println("------------------"); + System.out.println("ID: " + agent.getId()); + System.out.println("Name: " + agent.getName()); + System.out.println("Address: " + agent.getAddress()); System.out.println("Username: " + agent.getLogin()); System.out.println("------------------"); - + agent.setName(FormUtil.readString("Name", agent.getName())); - agent.setAddress(FormUtil.readString("Address", agent.getAddress())); - agent.setLogin(FormUtil.readString("Username", agent.getLogin())); - agent.setPassword(FormUtil.readPassword(agent.getPassword())); - - _orm.persist(agent); - + agent.setAddress(FormUtil.readString("Address", agent.getAddress())); + agent.setLogin(FormUtil.readString("Username", agent.getLogin())); + agent.setPassword(FormUtil.readPassword(agent.getPassword())); + + _orm.persist(agent); + System.out.println("------------------"); System.out.println("Agent was modified."); } @@ -483,7 +484,8 @@ public class Main { /** * 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) { _orm.delete(agent); @@ -495,23 +497,23 @@ public class Main { */ private static void showContractMenu() { // menu options - final int INSERT_PERSON = 0; - final int CREATE_CONTRACT = 1; + final int INSERT_PERSON = 0; + final int CREATE_CONTRACT = 1; final int OVERVIEW_CONTRACTS = 2; - final int BACK = 3; + final int BACK = 3; // create menu 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("Contracts overview", OVERVIEW_CONTRACTS); mainMenu.addEntry("Back", BACK); // process input - while(true) { + while (true) { int response = mainMenu.show(); - switch(response) { + switch (response) { case INSERT_PERSON: newPerson(); break; @@ -551,7 +553,7 @@ public class Main { System.out.println("Please enter the date of the contract."); contract.setDate(Date.valueOf(FormUtil.readInt("Year") + "-" + FormUtil.readInt("Month") + "-" + 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"); if (isTenancy) { boolean apartmentsAvailable = printListOfApartments(); @@ -572,8 +574,7 @@ public class Main { long duration = endDate.getTime() - startDate.getTime(); tenancyContract.setDuration(Duration.ofMillis(duration)); tenancyContract.setAdditionalCost(FormUtil.readInt("Additional Costs")); - } - else { + } else { boolean housesAvailable = printListOfHouses(); if (!housesAvailable) { System.out.println("No houses available to sell."); @@ -598,7 +599,7 @@ public class Main { for (Object o : persons) { 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()); } System.out.println("------------------"); @@ -610,7 +611,7 @@ public class Main { * @return true if houses are available, false otherwise */ private static boolean printListOfHouses() { - List houses = _orm.getAll(Type.HOUSE); + List houses = _orm.getAll(Type.HOUSE); List soldHouses = _orm.getSoldHouses(); if (houses.size() <= soldHouses.size()) { @@ -639,7 +640,7 @@ public class Main { * @return true if apartments are available, false otherwise */ private static boolean printListOfApartments() { - List apartments = _orm.getAll(Type.APARTMENT); + List apartments = _orm.getAll(Type.APARTMENT); List rentedApartments = _orm.getRentedApartments(); if (apartments.size() <= rentedApartments.size()) { return false; @@ -654,7 +655,8 @@ public class Main { continue; } System.out.println("ID: " + apartment.getId() + "; Address: " + apartment.getStreet() + " " - + apartment.getStreetNumber() + ", " + apartment.getPostalCode() + " " + apartment.getCity()); + + apartment.getStreetNumber() + ", " + apartment.getPostalCode() + " " + + apartment.getCity()); } System.out.println("------------------"); @@ -665,8 +667,8 @@ public class Main { * Shows the contracts overview. */ private static void showContractsOverview() { - List contracts = _orm.getAll(Type.CONTRACT); - Menu listContracts = new Menu("Select a contract to view the details"); + List contracts = _orm.getAll(Type.CONTRACT); + Menu listContracts = new Menu("Select a contract to view the details"); final int BACK = 0; @@ -678,7 +680,7 @@ public class Main { listContracts.addEntry("Back to the Contract management menu", BACK); // process input - while(true) { + while (true) { int response = listContracts.show(); switch (response) { @@ -694,7 +696,8 @@ public class Main { /** * 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) { System.out.println("------------------"); @@ -711,8 +714,7 @@ public class Main { estate = _orm.getEstate(purchaseContract.getHouse()); System.out.println("House: " + estate.getStreet() + " " + estate.getStreetNumber() + ", " + estate.getPostalCode() + " " + estate.getCity()); - } - else if (contract instanceof TenancyContract) { + } else if (contract instanceof TenancyContract) { TenancyContract tenancyContract = (TenancyContract) contract; Duration duration = tenancyContract.getDuration(); System.out.println("Start Date: " + DateFormat.getInstance().format(tenancyContract.getStartDate())); @@ -728,7 +730,8 @@ public class Main { /** * 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) { Contract contract = _orm.getContract(id); diff --git a/02/src/de/dis2017/Menu.java b/02/src/de/dis2017/Menu.java index e472c1c..c19b773 100644 --- a/02/src/de/dis2017/Menu.java +++ b/02/src/de/dis2017/Menu.java @@ -7,17 +7,17 @@ import java.util.ArrayList; /** * Small helper class for menus. - * + *

* 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. - * + *

* Example: * Menu m = new Menu("Main menu"); * m.addEntry("Work hard", 0); * m.addEntry("Rest", 1); * m.addEntry("Go home", 2); * int chosenOption = m.show(); - * + *

* This results in the following output on the console: * Main menu: * [1] Work hard @@ -26,61 +26,64 @@ import java.util.ArrayList; * -- */ class Menu { - private String _title; - private ArrayList _labels = new ArrayList<>(); - private ArrayList _returnValues = new ArrayList<>(); - - /** - * Initializes the menu object. + private String _title; + private ArrayList _labels = new ArrayList<>(); + private ArrayList _returnValues = new ArrayList<>(); + + /** + * Initializes the menu object. * - * @param title Title of the menu (e.g. "Main menu") - */ - Menu(String title) { - super(); - _title = title; - } - - /** - * Adds a new menu entry + * @param title + * Title of the menu (e.g. "Main menu") + */ + Menu(String title) { + super(); + _title = title; + } + + /** + * Adds a new menu entry * - * @param label Name of the entry - * @param returnValue constant number which is returned upon selection this entry - */ - void addEntry(String label, int returnValue) { - _labels.add(label); - _returnValues.add(returnValue); - } - - /** - * Shows the menu. + * @param label + * Name of the entry + * @param returnValue + * constant number which is returned upon selection this entry + */ + void addEntry(String label, int returnValue) { + _labels.add(label); + _returnValues.add(returnValue); + } + + /** + * Shows the menu. * - * @return The constant number of the selected menu entry. - */ - int show() { - int selection = -1; - - BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); - - while(selection == -1) { - System.out.println(_title+":"); - - for(int i = 0; i < _labels.size(); ++i) { - System.out.println("[" + (i + 1) + "] " + _labels.get(i)); - } - - System.out.print("-- "); - try { - selection = Integer.parseInt(stdin.readLine()); - } catch (NumberFormatException | IOException e) { - e.printStackTrace(); - } - - if(selection < 1 || selection > _returnValues.size()) { - System.err.println("Invalid input!"); - selection = -1; - } - } - - return _returnValues.get(selection-1); - } + * @return The constant number of the selected menu entry. + */ + int show() { + int selection = -1; + + BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); + + while (selection == -1) { + System.out.println(_title + ":"); + + for (int i = 0; i < _labels.size(); ++i) { + System.out.println("[" + (i + 1) + "] " + _labels.get(i)); + } + + System.out.print("-- "); + try { + selection = Integer.parseInt(stdin.readLine()); + } catch (NumberFormatException | IOException e) { + e.printStackTrace(); + } + + if (selection < 1 || selection > _returnValues.size()) { + System.err.println("Invalid input!"); + selection = -1; + } + } + + return _returnValues.get(selection - 1); + } } diff --git a/02/src/de/dis2017/data/Apartment.java b/02/src/de/dis2017/data/Apartment.java index a2ca3f1..1a4096d 100644 --- a/02/src/de/dis2017/data/Apartment.java +++ b/02/src/de/dis2017/data/Apartment.java @@ -1,54 +1,63 @@ package de.dis2017.data; -public class Apartment extends Estate{ - private int floor; - private int rent; - private int rooms; - private boolean balcony; - private boolean builtinKitchen; - - public Apartment(){ - super(); - } - - public Apartment(Estate estate){ - this.setId(estate.getId()); - this.setCity(estate.getCity()); - this.setPostalCode(estate.getPostalCode()); - this.setStreet(estate.getStreet()); - this.setStreetNumber(estate.getStreetNumber()); - this.setSquareArea(estate.getSquareArea()); - this.setAgent(estate.getAgent()); - } - - public int getFloor() { - return floor; - } - public void setFloor(int floor) { - this.floor = floor; - } - public int getRent() { - return rent; - } - public void setRent(int rent) { - this.rent = rent; - } - public int getRooms() { - return rooms; - } - public void setRooms(int rooms) { - this.rooms = rooms; - } - public boolean hasBalcony() { - return balcony; - } - public void setBalcony(boolean balcony) { - this.balcony = balcony; - } - public boolean hasBuiltinKitchen() { - return builtinKitchen; - } - public void setBuiltinKitchen(boolean builtinKitchen) { - this.builtinKitchen = builtinKitchen; - } +public class Apartment extends Estate { + private int floor; + private int rent; + private int rooms; + private boolean balcony; + private boolean builtinKitchen; + + public Apartment() { + super(); + } + + public Apartment(Estate estate) { + this.setId(estate.getId()); + this.setCity(estate.getCity()); + this.setPostalCode(estate.getPostalCode()); + this.setStreet(estate.getStreet()); + this.setStreetNumber(estate.getStreetNumber()); + this.setSquareArea(estate.getSquareArea()); + this.setAgent(estate.getAgent()); + } + + public int getFloor() { + return floor; + } + + public void setFloor(int floor) { + this.floor = floor; + } + + public int getRent() { + return rent; + } + + public void setRent(int rent) { + this.rent = rent; + } + + public int getRooms() { + return rooms; + } + + public void setRooms(int rooms) { + this.rooms = rooms; + } + + public boolean hasBalcony() { + return balcony; + } + + public void setBalcony(boolean balcony) { + this.balcony = balcony; + } + + public boolean hasBuiltinKitchen() { + return builtinKitchen; + } + + public void setBuiltinKitchen(boolean builtinKitchen) { + this.builtinKitchen = builtinKitchen; + } } diff --git a/02/src/de/dis2017/data/Contract.java b/02/src/de/dis2017/data/Contract.java index c7279f1..c9a0922 100644 --- a/02/src/de/dis2017/data/Contract.java +++ b/02/src/de/dis2017/data/Contract.java @@ -6,40 +6,49 @@ import java.sql.Date; * Contract data class */ public class Contract { - private int contractNo = -1; - private Date date; - private String place; - private int person; - private int estate; - - public int getEstate() { - return estate; - } - public void setEstate(int estate) { - this.estate = estate; - } - public int getContractNo() { - return contractNo; - } - public void setContractNo(int contractNo) { - this.contractNo = contractNo; - } - public Date getDate() { - return date; - } - public void setDate(Date date) { - this.date = date; - } - public String getPlace() { - return place; - } - public void setPlace(String place) { - this.place = place; - } - public int getPerson() { - return person; - } - public void setPerson(int person) { - this.person = person; - } + private int contractNo = -1; + private Date date; + private String place; + private int person; + private int estate; + + public int getEstate() { + return estate; + } + + public void setEstate(int estate) { + this.estate = estate; + } + + public int getContractNo() { + return contractNo; + } + + public void setContractNo(int contractNo) { + this.contractNo = contractNo; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public String getPlace() { + return place; + } + + public void setPlace(String place) { + this.place = place; + } + + public int getPerson() { + return person; + } + + public void setPerson(int person) { + this.person = person; + } } diff --git a/02/src/de/dis2017/data/Estate.java b/02/src/de/dis2017/data/Estate.java index 61f7e7a..737563e 100644 --- a/02/src/de/dis2017/data/Estate.java +++ b/02/src/de/dis2017/data/Estate.java @@ -4,63 +4,63 @@ package de.dis2017.data; * EstateAgent data class */ public class Estate { - private int id = -1; - private String city; - private String postalCode; - private String street; - private int streetNumber; - private int squareArea; - private int agent; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getCity() { - return city; - } - - public void setCity(String city) { - this.city = city; - } - - public String getPostalCode() { - return postalCode; - } - - public void setPostalCode(String postalCode) { - this.postalCode = postalCode; - } - - public String getStreet() { - return street; - } - - public void setStreet(String street) { - this.street = street; - } - - public int getStreetNumber() { - return streetNumber; - } - - public void setStreetNumber(int streetNumber) { - this.streetNumber = streetNumber; - } - - public int getSquareArea() { - return squareArea; - } - - public void setSquareArea(int squareArea) { - this.squareArea = squareArea; - } - - public int getAgent() { return agent; } - - public void setAgent(int agent) { this.agent = agent; } + private int id = -1; + private String city; + private String postalCode; + private String street; + private int streetNumber; + private int squareArea; + private int agent; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getPostalCode() { + return postalCode; + } + + public void setPostalCode(String postalCode) { + this.postalCode = postalCode; + } + + public String getStreet() { + return street; + } + + public void setStreet(String street) { + this.street = street; + } + + public int getStreetNumber() { + return streetNumber; + } + + public void setStreetNumber(int streetNumber) { + this.streetNumber = streetNumber; + } + + public int getSquareArea() { + return squareArea; + } + + public void setSquareArea(int squareArea) { + this.squareArea = squareArea; + } + + public int getAgent() { return agent; } + + public void setAgent(int agent) { this.agent = agent; } } diff --git a/02/src/de/dis2017/data/EstateAgent.java b/02/src/de/dis2017/data/EstateAgent.java index 99fa338..7f7f782 100644 --- a/02/src/de/dis2017/data/EstateAgent.java +++ b/02/src/de/dis2017/data/EstateAgent.java @@ -4,49 +4,49 @@ package de.dis2017.data; * EstateAgent data class */ public class EstateAgent { - private int id = -1; - private String name; - private String address; - private String login; - private String password; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getAddress() { - return address; - } - - public void setAddress(String address) { - this.address = address; - } - - public String getLogin() { - return login; - } - - public void setLogin(String login) { - this.login = login; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } + private int id = -1; + private String name; + private String address; + private String login; + private String password; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getLogin() { + return login; + } + + public void setLogin(String login) { + this.login = login; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } } diff --git a/02/src/de/dis2017/data/House.java b/02/src/de/dis2017/data/House.java index 44e3e00..b7f7a51 100644 --- a/02/src/de/dis2017/data/House.java +++ b/02/src/de/dis2017/data/House.java @@ -1,42 +1,47 @@ package de.dis2017.data; public class House extends Estate { - private int floors; - private int price; - private boolean garden; - - public House(){ - super(); - } - - public House(Estate estate){ - this.setId(estate.getId()); - this.setCity(estate.getCity()); - this.setPostalCode(estate.getPostalCode()); - this.setStreet(estate.getStreet()); - this.setStreetNumber(estate.getStreetNumber()); - this.setSquareArea(estate.getSquareArea()); - this.setAgent(estate.getAgent()); - } - - public int getFloors() { - return floors; - } - public void setFloors(int floors) { - this.floors = floors; - } - public int getPrice() { - return price; - } - public void setPrice(int price) { - this.price = price; - } - public boolean hasGarden() { - return garden; - } - public void setGarden(boolean garden) { - this.garden = garden; - } - - + private int floors; + private int price; + private boolean garden; + + public House() { + super(); + } + + public House(Estate estate) { + this.setId(estate.getId()); + this.setCity(estate.getCity()); + this.setPostalCode(estate.getPostalCode()); + this.setStreet(estate.getStreet()); + this.setStreetNumber(estate.getStreetNumber()); + this.setSquareArea(estate.getSquareArea()); + this.setAgent(estate.getAgent()); + } + + public int getFloors() { + return floors; + } + + public void setFloors(int floors) { + this.floors = floors; + } + + public int getPrice() { + return price; + } + + public void setPrice(int price) { + this.price = price; + } + + public boolean hasGarden() { + return garden; + } + + public void setGarden(boolean garden) { + this.garden = garden; + } + + } diff --git a/02/src/de/dis2017/data/Person.java b/02/src/de/dis2017/data/Person.java index 59e7352..63893e3 100644 --- a/02/src/de/dis2017/data/Person.java +++ b/02/src/de/dis2017/data/Person.java @@ -1,31 +1,38 @@ package de.dis2017.data; public class Person { - private int id = -1; - private String firstName; - private String name; - private String address; - - public int getId() { return id; } - public void setId(int id) { this.id = id; } - public String getFirstName() { - return firstName; - } - public void setFirstName(String firstName) { - this.firstName = firstName; - } - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - public String getAddress() { - return address; - } - public void setAddress(String adress) { - this.address = adress; - } - - + private int id = -1; + private String firstName; + private String name; + private String address; + + public int getId() { return id; } + + public void setId(int id) { this.id = id; } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String adress) { + this.address = adress; + } + + } diff --git a/02/src/de/dis2017/data/PurchaseContract.java b/02/src/de/dis2017/data/PurchaseContract.java index 6a1b2fe..caacb48 100644 --- a/02/src/de/dis2017/data/PurchaseContract.java +++ b/02/src/de/dis2017/data/PurchaseContract.java @@ -1,40 +1,41 @@ package de.dis2017.data; public class PurchaseContract extends Contract { - private int noOfInstallments; - private int interestRate; - - public PurchaseContract(){ - super(); - } - public PurchaseContract(Contract contract){ - this.setContractNo(contract.getContractNo()); - this.setDate(contract.getDate()); - this.setPlace(contract.getPlace()); - this.setPerson(contract.getPerson()); - } - - public int getNoOfInstallments() { - return noOfInstallments; - } - - public void setNoOfInstallments(int noOfInstallments) { - this.noOfInstallments = noOfInstallments; - } - - public int getInterestRate() { - return interestRate; - } - - public void setInterestRate(int interestRate) { - this.interestRate = interestRate; - } - - public int getHouse() { - return super.getEstate(); - } - - public void setHouse(int house) { - super.setEstate(house); - } + private int noOfInstallments; + private int interestRate; + + public PurchaseContract() { + super(); + } + + public PurchaseContract(Contract contract) { + this.setContractNo(contract.getContractNo()); + this.setDate(contract.getDate()); + this.setPlace(contract.getPlace()); + this.setPerson(contract.getPerson()); + } + + public int getNoOfInstallments() { + return noOfInstallments; + } + + public void setNoOfInstallments(int noOfInstallments) { + this.noOfInstallments = noOfInstallments; + } + + public int getInterestRate() { + return interestRate; + } + + public void setInterestRate(int interestRate) { + this.interestRate = interestRate; + } + + public int getHouse() { + return super.getEstate(); + } + + public void setHouse(int house) { + super.setEstate(house); + } } diff --git a/02/src/de/dis2017/data/TenancyContract.java b/02/src/de/dis2017/data/TenancyContract.java index ccccb80..3f8c21a 100644 --- a/02/src/de/dis2017/data/TenancyContract.java +++ b/02/src/de/dis2017/data/TenancyContract.java @@ -5,54 +5,54 @@ import java.time.Duration; import java.util.Date; public class TenancyContract extends Contract { - private Timestamp startDate; - private Duration duration; - private int additionalCost; - - public TenancyContract(){ - super(); - } - - public TenancyContract(Contract contract){ - this.setContractNo(contract.getContractNo()); - this.setDate(contract.getDate()); - this.setPlace(contract.getPlace()); - this.setPerson(contract.getPerson()); - } - - public Timestamp getStartDate() { - return startDate; - } - - public void setStartDate(Date startDate) { - this.startDate = new Timestamp(startDate.getTime()); - } - - public Duration getDuration() { - return duration; - } - - public void setDuration(Duration duration) { - this.duration = duration; - } + private Timestamp startDate; + private Duration duration; + private int additionalCost; + + public TenancyContract() { + super(); + } + + public TenancyContract(Contract contract) { + this.setContractNo(contract.getContractNo()); + this.setDate(contract.getDate()); + this.setPlace(contract.getPlace()); + this.setPerson(contract.getPerson()); + } + + public Timestamp getStartDate() { + return startDate; + } + + public void setStartDate(Date startDate) { + this.startDate = new Timestamp(startDate.getTime()); + } + + public Duration getDuration() { + return duration; + } + + public void setDuration(Duration duration) { + this.duration = duration; + } public void setDuration(Timestamp duration) { this.duration = Duration.ofMillis(duration.getTime()); } - - public int getAdditionalCost() { - return additionalCost; - } - - public void setAdditionalCost(int additionalCost) { - this.additionalCost = additionalCost; - } - - public int getApartment() { - return super.getEstate(); - } - - public void setApartment(int apartment) { - super.setEstate(apartment); - } + + public int getAdditionalCost() { + return additionalCost; + } + + public void setAdditionalCost(int additionalCost) { + this.additionalCost = additionalCost; + } + + public int getApartment() { + return super.getEstate(); + } + + public void setApartment(int apartment) { + super.setEstate(apartment); + } } diff --git a/02/src/de/dis2017/data/db/DB2ConnectionManager.java b/02/src/de/dis2017/data/db/DB2ConnectionManager.java index 3711056..6f3e5aa 100644 --- a/02/src/de/dis2017/data/db/DB2ConnectionManager.java +++ b/02/src/de/dis2017/data/db/DB2ConnectionManager.java @@ -12,62 +12,62 @@ import java.util.Properties; /** * Singleton for management of database connections - * + * * @author Michael von Riegen * @version April 2009 */ class DB2ConnectionManager { - - // instance of Driver Manager - private static DB2ConnectionManager _instance = null; - - // DB2 connection - private Connection _con; - - /** - * Initializes database connection - */ - private DB2ConnectionManager() { - try { - // load properties from db2.properties file - Properties properties = new Properties(); - URL url = ClassLoader.getSystemResource("db2.properties"); - FileInputStream stream = new FileInputStream(new File(url.toURI())); - properties.load(stream); - stream.close(); - - String jdbcUser = properties.getProperty("jdbc_user"); - String jdbcPass = properties.getProperty("jdbc_pass"); - String jdbcUrl = properties.getProperty("jdbc_url"); - - // created connection to DB2 database - Class.forName("com.ibm.db2.jcc.DB2Driver"); - _con = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPass); - - } catch (IOException | ClassNotFoundException | SQLException | URISyntaxException e){ - e.printStackTrace(); - } - - } - - /** - * Returns the instance of the connection manager - * - * @return DB2ConnectionManager - */ - static DB2ConnectionManager getInstance() { - if (_instance == null) { - _instance = new DB2ConnectionManager(); - } - return _instance; - } - - /** - * Returns the connection to the DB2 database - * - * @return Connection - */ - Connection getConnection() { - return _con; - } + + // instance of Driver Manager + private static DB2ConnectionManager _instance = null; + + // DB2 connection + private Connection _con; + + /** + * Initializes database connection + */ + private DB2ConnectionManager() { + try { + // load properties from db2.properties file + Properties properties = new Properties(); + URL url = ClassLoader.getSystemResource("db2.properties"); + FileInputStream stream = new FileInputStream(new File(url.toURI())); + properties.load(stream); + stream.close(); + + String jdbcUser = properties.getProperty("jdbc_user"); + String jdbcPass = properties.getProperty("jdbc_pass"); + String jdbcUrl = properties.getProperty("jdbc_url"); + + // created connection to DB2 database + Class.forName("com.ibm.db2.jcc.DB2Driver"); + _con = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPass); + + } catch (IOException | ClassNotFoundException | SQLException | URISyntaxException e) { + e.printStackTrace(); + } + + } + + /** + * Returns the instance of the connection manager + * + * @return DB2ConnectionManager + */ + static DB2ConnectionManager getInstance() { + if (_instance == null) { + _instance = new DB2ConnectionManager(); + } + return _instance; + } + + /** + * Returns the connection to the DB2 database + * + * @return Connection + */ + Connection getConnection() { + return _con; + } } diff --git a/02/src/de/dis2017/data/db/ORM.java b/02/src/de/dis2017/data/db/ORM.java index 5b050e5..eca144e 100644 --- a/02/src/de/dis2017/data/db/ORM.java +++ b/02/src/de/dis2017/data/db/ORM.java @@ -23,11 +23,11 @@ public class ORM { private Connection _connection; private Map _agents; - private Map _agentsUsername; - private Map _estates; - private Map _contracts; - private Map _persons; - + private Map _agentsUsername; + private Map _estates; + private Map _contracts; + private Map _persons; + /** * Initializes the ORM. @@ -45,7 +45,8 @@ public class ORM { /** * 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 */ public List getAll(Type objectType) { @@ -54,7 +55,7 @@ public class ORM { // create query String selectSQL = "SELECT * FROM " + objectType.name(); PreparedStatement pstmt = _connection.prepareStatement(selectSQL); - + // execute query ResultSet rs = pstmt.executeQuery(); switch (objectType) { @@ -84,7 +85,7 @@ public class ORM { } catch (SQLException e) { e.printStackTrace(); } - + return objects; } @@ -139,13 +140,16 @@ public class ORM { /** * 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 - * @throws SQLException when an error occurs during the rs.next call + * + * @throws SQLException + * when an error occurs during the rs.next call */ private List processEstates(ResultSet rs) throws SQLException { List estates = new ArrayList<>(); - + while (rs.next()) { Estate estate = new Estate(); estate.setId(rs.getInt("ID")); @@ -155,7 +159,7 @@ public class ORM { estate.setStreetNumber(rs.getInt("streetNumber")); estate.setSquareArea(rs.getInt("squareArea")); estate.setAgent(rs.getInt("agent")); - + estates.add(estate); } @@ -165,13 +169,17 @@ public class ORM { /** * Process a select all query for houses. * - * @param rs the result set of such a query - * @param estates a list of estates + * @param rs + * the result set of such a query + * @param estates + * a list of estates * @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 processHouses(ResultSet rs, List estates) throws SQLException { - List houses = new ArrayList<>(); + List houses = new ArrayList<>(); Map housesMap = new HashMap<>(); while (rs.next()) { @@ -205,13 +213,17 @@ public class ORM { /** * Process a select all query for houses. * - * @param rs the result set of such a query - * @param estates a list of estates + * @param rs + * the result set of such a query + * @param estates + * a list of estates * @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 processApartments(ResultSet rs, List estates) throws SQLException { - List apartments = new ArrayList<>(); + List apartments = new ArrayList<>(); Map apartmentsMap = new HashMap<>(); while (rs.next()) { @@ -247,9 +259,12 @@ public class ORM { /** * 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 - * @throws SQLException when an error occurs during the rs.next call + * + * @throws SQLException + * when an error occurs during the rs.next call */ private List processAgents(ResultSet rs) throws SQLException { List agents = new ArrayList<>(); @@ -260,7 +275,7 @@ public class ORM { agent.setAddress(rs.getString("address")); agent.setLogin(rs.getString("login")); agent.setPassword(rs.getString("password")); - + _agents.put(agent.getId(), agent); _agentsUsername.put(agent.getLogin(), agent); agents.add(agent); @@ -272,9 +287,12 @@ public class ORM { /** * 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 - * @throws SQLException when an error occurs during the rs.next call + * + * @throws SQLException + * when an error occurs during the rs.next call */ private List processContracts(ResultSet rs) throws SQLException { List contracts = new ArrayList<>(); @@ -294,9 +312,12 @@ public class ORM { /** * 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 - * @throws SQLException when an error occurs during the rs.next call + * + * @throws SQLException + * when an error occurs during the rs.next call */ private List processPersons(ResultSet rs) throws SQLException { List persons = new ArrayList<>(); @@ -317,7 +338,8 @@ public class ORM { /** * 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 */ public Contract getContract(int ID) { @@ -325,25 +347,25 @@ public class ORM { return _contracts.get(ID); } - String selectSQLPurchase = "SELECT * FROM PURCHASECONTRACT " + - "LEFT JOIN CONTRACT ON PURCHASECONTRACT.CONTRACTNUMBER = CONTRACT.CONTRACTNUMBER " + - "LEFT JOIN SALES ON PURCHASECONTRACT.CONTRACTNUMBER = SALES.CONTRACTNUMBER " + - "WHERE PURCHASECONTRACT.CONTRACTNUMBER = ?"; + String selectSQLPurchase = "SELECT * FROM PURCHASECONTRACT " + + "LEFT JOIN CONTRACT ON PURCHASECONTRACT.CONTRACTNUMBER = CONTRACT.CONTRACTNUMBER " + + "LEFT JOIN SALES ON PURCHASECONTRACT.CONTRACTNUMBER = SALES.CONTRACTNUMBER " + + "WHERE PURCHASECONTRACT.CONTRACTNUMBER = ?"; String selectSQLTenancy = "SELECT * FROM TENANCYCONTRACT " + "LEFT JOIN CONTRACT ON TENANCYCONTRACT.CONTRACTNUMBER = CONTRACT.CONTRACTNUMBER " + "LEFT JOIN RENTALS ON TENANCYCONTRACT.CONTRACTNUMBER = RENTALS.CONTRACTNUMBER " + "WHERE TENANCYCONTRACT.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 purchase contract first PreparedStatement preparedStatementCount = _connection.prepareStatement(countPurchase); preparedStatementCount.setInt(1, ID); ResultSet rs = preparedStatementCount.executeQuery(); rs.next(); - int count = rs.getInt("count"); - String type = "None"; + int count = rs.getInt("count"); + String type = "None"; if (count == 0) { // try tenancy contract next preparedStatementCount = _connection.prepareStatement(countTenancy); @@ -354,15 +376,14 @@ public class ORM { if (count == 1) { type = "TenancyContract"; } - } - else { + } else { type = "PurchaseContract"; } rs.close(); preparedStatementCount.close(); PreparedStatement pstmt; - Contract contract; + Contract contract; switch (type) { case "PurchaseContract": 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. * - * @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 */ public Estate getEstate(int ID) { if (_estates.containsKey(ID)) { return _estates.get(ID); } - - String selectSQLHouse = "SELECT * FROM HOUSE LEFT JOIN ESTATE ON HOUSE.ID = ESTATE.ID " + - "WHERE HOUSE.ID = ?"; + + String selectSQLHouse = "SELECT * FROM HOUSE LEFT JOIN ESTATE ON HOUSE.ID = ESTATE.ID " + + "WHERE HOUSE.ID = ?"; String selectSQLApartment = "SELECT * FROM APARTMENT LEFT JOIN ESTATE ON APARTMENT.ID = ESTATE.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 = ?"; try { // try house first @@ -435,8 +457,8 @@ public class ORM { preparedStatementCount.setInt(1, ID); ResultSet rs = preparedStatementCount.executeQuery(); rs.next(); - int count = rs.getInt("count"); - String type = "None"; + int count = rs.getInt("count"); + String type = "None"; if (count == 0) { // try apartment next preparedStatementCount = _connection.prepareStatement(countApartment); @@ -447,15 +469,14 @@ public class ORM { if (count == 1) { type = "Apartment"; } - } - else { + } else { type = "House"; } rs.close(); preparedStatementCount.close(); PreparedStatement pstmt; - Estate estate; + Estate estate; switch (type) { case "House": pstmt = _connection.prepareStatement(selectSQLHouse); @@ -472,7 +493,7 @@ public class ORM { default: return null; } - + if (rs.next()) { estate.setId(ID); estate.setCity(rs.getString("city")); @@ -494,7 +515,7 @@ public class ORM { ((Apartment) estate).setBalcony(rs.getBoolean("balcony")); ((Apartment) estate).setBuiltinKitchen(rs.getBoolean("builtInKitchen")); } - + rs.close(); pstmt.close(); _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. * - * @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 */ public EstateAgent getAgent(int ID) { @@ -524,7 +546,7 @@ public class ORM { String selectSQL = "SELECT * FROM ESTATEAGENT WHERE ID = ?"; PreparedStatement pstmt = _connection.prepareStatement(selectSQL); pstmt.setInt(1, ID); - + return getAgent(pstmt); } catch (SQLException e) { e.printStackTrace(); @@ -536,7 +558,8 @@ public class ORM { /** * 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 */ public Person getPerson(int ID) { @@ -550,7 +573,7 @@ public class ORM { PreparedStatement pstmt = _connection.prepareStatement(selectSQL); pstmt.setInt(1, ID); ResultSet rs = pstmt.executeQuery(); - Person person; + Person person; if (rs.next()) { 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, * - * @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 */ public EstateAgent getAgent(String username) { @@ -601,12 +625,12 @@ public class ORM { /** * 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 */ @Nullable - private EstateAgent getAgent(PreparedStatement pstmt) - { + private EstateAgent getAgent(PreparedStatement pstmt) { try { // execute query ResultSet rs = pstmt.executeQuery(); @@ -618,10 +642,10 @@ public class ORM { agent.setAddress(rs.getString("address")); agent.setLogin(rs.getString("login")); agent.setPassword(rs.getString("password")); - + rs.close(); pstmt.close(); - + _agents.put(agent.getId(), agent); _agentsUsername.put(agent.getLogin(), agent); return agent; @@ -636,10 +660,10 @@ public class ORM { /** * 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) { System.err.println("This agent is not yet persisted to the database and cannot be deleted."); return; @@ -659,7 +683,8 @@ public class ORM { /** * 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) { if (estate.getId() == -1) { @@ -682,15 +707,16 @@ public class ORM { /** * Deletes an object from the database. * - * @param sql the sql used for deletion - * @param id the id of the object to be deleted + * @param sql + * 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 { PreparedStatement pstmt = _connection.prepareStatement(sql); pstmt.setInt(1, id); - + // execute query pstmt.executeUpdate(); pstmt.close(); @@ -702,13 +728,14 @@ public class ORM { /** * 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 { 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); pstmt.setString(1, agent.getName()); @@ -716,7 +743,7 @@ public class ORM { pstmt.setString(3, agent.getLogin()); pstmt.setString(4, agent.getPassword()); pstmt.executeUpdate(); - + ResultSet rs = pstmt.getGeneratedKeys(); if (rs.next()) { agent.setId(rs.getInt(1)); @@ -726,13 +753,13 @@ public class ORM { pstmt.close(); } else { // create query - String updateSQL = "UPDATE ESTATEAGENT SET name = ?, address = ?, password = ? WHERE ID = ?"; - PreparedStatement pstmt = _connection.prepareStatement(updateSQL); + String updateSQL = "UPDATE ESTATEAGENT SET name = ?, address = ?, password = ? WHERE ID = ?"; + PreparedStatement pstmt = _connection.prepareStatement(updateSQL); pstmt.setString(1, agent.getName()); pstmt.setString(2, agent.getAddress()); pstmt.setString(3, agent.getPassword()); pstmt.setInt(4, agent.getId()); - + // execute query pstmt.executeUpdate(); pstmt.close(); @@ -751,10 +778,10 @@ public class ORM { /** * 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; try { _connection.setAutoCommit(false); @@ -778,9 +805,10 @@ public class ORM { pstmt.close(); if (estate instanceof House) { - House house = (House) estate; - String insertSQLHouse = "INSERT INTO HOUSE (ID, price, garden, floors) VALUES (?, ?, ?, ?)"; - PreparedStatement pstmtHouse = _connection.prepareStatement(insertSQLHouse); + House house = (House) estate; + String insertSQLHouse + = "INSERT INTO HOUSE (ID, price, garden, floors) VALUES (?, ?, ?, ?)"; + PreparedStatement pstmtHouse = _connection.prepareStatement(insertSQLHouse); pstmtHouse.setInt(1, house.getId()); pstmtHouse.setInt(2, house.getPrice()); pstmtHouse.setInt(3, house.hasGarden() ? 1 : 0); @@ -790,11 +818,11 @@ public class ORM { pstmt.close(); pstmtHouse.close(); changeFinished = true; - } - else if (estate instanceof Apartment) { + } else if (estate instanceof Apartment) { Apartment apartment = (Apartment) estate; - String insertSQLApartment = "INSERT INTO APARTMENT (ID, floor, rent, rooms, balcony, builtInKitchen) " + - "VALUES (?, ?, ?, ?, ?, ?)"; + String insertSQLApartment = + "INSERT INTO APARTMENT (ID, floor, rent, rooms, balcony, builtInKitchen) " + + "VALUES (?, ?, ?, ?, ?, ?)"; PreparedStatement pstmtApartment = _connection.prepareStatement(insertSQLApartment); pstmtApartment.setInt(1, apartment.getId()); pstmtApartment.setInt(2, apartment.getFloor()); @@ -822,9 +850,10 @@ public class ORM { pstmt.setInt(7, estate.getId()); if (estate instanceof House) { - House house = (House) estate; - String updateSQLHouse = "UPDATE HOUSE SET floors = ?, garden = ?, price = ? WHERE ID = ?"; - PreparedStatement pstmtHouse = _connection.prepareStatement(updateSQLHouse); + House house = (House) estate; + String updateSQLHouse + = "UPDATE HOUSE SET floors = ?, garden = ?, price = ? WHERE ID = ?"; + PreparedStatement pstmtHouse = _connection.prepareStatement(updateSQLHouse); pstmtHouse.setInt(1, house.getFloors()); pstmtHouse.setInt(2, house.hasGarden() ? 1 : 0); pstmtHouse.setInt(3, house.getPrice()); @@ -834,8 +863,7 @@ public class ORM { pstmt.close(); pstmtHouse.close(); changeFinished = true; - } - else if (estate instanceof Apartment) { + } else if (estate instanceof Apartment) { Apartment apartment = (Apartment) estate; String updateSQLApartment = "UPDATE APARTMENT SET floor = ?, rent = ?, rooms = ?, " + "balcony = ?, builtInKitchen = ? WHERE ID = ?"; @@ -873,10 +901,10 @@ public class ORM { /** * 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; try { _connection.setAutoCommit(false); @@ -907,7 +935,8 @@ public class ORM { pstmtPurchase.executeUpdate(); 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); pstmtSale.setInt(1, purchaseContract.getContractNo()); pstmtSale.setInt(2, purchaseContract.getHouse()); @@ -915,8 +944,7 @@ public class ORM { pstmtSale.executeUpdate(); pstmtSale.close(); changeFinished = true; - } - else if (contract instanceof TenancyContract) { + } else if (contract instanceof TenancyContract) { TenancyContract tenancyContract = (TenancyContract) contract; String insertSQLTenancyContract = "INSERT INTO TENANCYCONTRACT " + "(contractNumber, startDate, duration, additionalCosts) " + @@ -928,8 +956,9 @@ public class ORM { pstmtTenancy.setInt(4, tenancyContract.getAdditionalCost()); pstmtTenancy.executeUpdate(); 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); pstmtRental.setInt(1, tenancyContract.getContractNo()); pstmtRental.setInt(2, tenancyContract.getApartment()); @@ -961,14 +990,14 @@ public class ORM { /** * 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 { if (person.getId() == -1) { - String insertSQL = "INSERT INTO PERSON (firstName, name, address) VALUES (?, ?, ?)"; - PreparedStatement pstmt = _connection.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS); + String insertSQL = "INSERT INTO PERSON (firstName, name, address) VALUES (?, ?, ?)"; + PreparedStatement pstmt = _connection.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS); pstmt.setString(1, person.getFirstName()); pstmt.setString(2, person.getName()); @@ -984,8 +1013,8 @@ public class ORM { pstmt.close(); } else { // create query - String updateSQL = "UPDATE PERSON SET firstName = ?, name = ?, address = ? WHERE ID = ?"; - PreparedStatement pstmt = _connection.prepareStatement(updateSQL); + String updateSQL = "UPDATE PERSON SET firstName = ?, name = ?, address = ? WHERE ID = ?"; + PreparedStatement pstmt = _connection.prepareStatement(updateSQL); pstmt.setString(1, person.getFirstName()); pstmt.setString(2, person.getName()); pstmt.setString(3, person.getAddress());