Reformatted the codebase

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
2017-04-25 16:09:55 +02:00
parent 6e7c1ed8bd
commit 9366ab384e
13 changed files with 883 additions and 810 deletions

View File

@ -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) {
@ -21,8 +22,10 @@ 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
* @param defaultValue
* the default value in case an empty input is provided
* @return read string * @return read string
*/ */
static String readString(String label, String defaultValue) { static String readString(String label, String defaultValue) {
@ -51,6 +54,7 @@ class FormUtil {
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.
* *
@ -64,7 +68,8 @@ class FormUtil {
/** /**
* 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) {
@ -74,8 +79,10 @@ class FormUtil {
/** /**
* 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
* @param defaultValue
* the default value
* @return read integer * @return read integer
*/ */
static int readInt(String label, int defaultValue) { static int readInt(String label, int defaultValue) {

View File

@ -55,16 +55,14 @@ public class Main {
case MENU_AGENT: case MENU_AGENT:
if (checkPassword()) { if (checkPassword()) {
showEstateAgentMenu(); showEstateAgentMenu();
} } else {
else {
System.out.println("The password was wrong."); System.out.println("The password was wrong.");
} }
break; break;
case MENU_ESTATES: case MENU_ESTATES:
if (loginEstateAgent()) { if (loginEstateAgent()) {
showEstateMenu(); showEstateMenu();
} } else {
else {
System.out.println("The username or password was wrong."); System.out.println("The username or password was wrong.");
} }
break; break;
@ -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"));
@ -205,7 +202,8 @@ public class Main {
/** /**
* 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);
@ -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());
@ -287,7 +285,8 @@ 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");
@ -310,8 +309,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)", 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()));
@ -328,7 +326,8 @@ 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);
@ -417,7 +416,8 @@ 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);
@ -458,7 +458,8 @@ 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");
@ -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);
@ -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.");
@ -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("------------------");
@ -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);

View File

@ -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
@ -33,7 +33,8 @@ class Menu {
/** /**
* 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) { Menu(String title) {
super(); super();
@ -43,8 +44,10 @@ class Menu {
/** /**
* 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
* constant number which is returned upon selection this entry
*/ */
void addEntry(String label, int returnValue) { void addEntry(String label, int returnValue) {
_labels.add(label); _labels.add(label);

View File

@ -24,30 +24,39 @@ public class Apartment extends Estate{
public int getFloor() { public int getFloor() {
return floor; return floor;
} }
public void setFloor(int floor) { public void setFloor(int floor) {
this.floor = floor; this.floor = floor;
} }
public int getRent() { public int getRent() {
return rent; return rent;
} }
public void setRent(int rent) { public void setRent(int rent) {
this.rent = rent; this.rent = rent;
} }
public int getRooms() { public int getRooms() {
return rooms; return rooms;
} }
public void setRooms(int rooms) { public void setRooms(int rooms) {
this.rooms = rooms; this.rooms = rooms;
} }
public boolean hasBalcony() { public boolean hasBalcony() {
return balcony; return balcony;
} }
public void setBalcony(boolean balcony) { public void setBalcony(boolean balcony) {
this.balcony = balcony; this.balcony = balcony;
} }
public boolean hasBuiltinKitchen() { public boolean hasBuiltinKitchen() {
return builtinKitchen; return builtinKitchen;
} }
public void setBuiltinKitchen(boolean builtinKitchen) { public void setBuiltinKitchen(boolean builtinKitchen) {
this.builtinKitchen = builtinKitchen; this.builtinKitchen = builtinKitchen;
} }

View File

@ -15,30 +15,39 @@ public class Contract {
public int getEstate() { public int getEstate() {
return estate; return estate;
} }
public void setEstate(int estate) { public void setEstate(int estate) {
this.estate = estate; this.estate = estate;
} }
public int getContractNo() { public int getContractNo() {
return contractNo; return contractNo;
} }
public void setContractNo(int contractNo) { public void setContractNo(int contractNo) {
this.contractNo = contractNo; this.contractNo = contractNo;
} }
public Date getDate() { public Date getDate() {
return date; return date;
} }
public void setDate(Date date) { public void setDate(Date date) {
this.date = date; this.date = date;
} }
public String getPlace() { public String getPlace() {
return place; return place;
} }
public void setPlace(String place) { public void setPlace(String place) {
this.place = place; this.place = place;
} }
public int getPerson() { public int getPerson() {
return person; return person;
} }
public void setPerson(int person) { public void setPerson(int person) {
this.person = person; this.person = person;
} }

View File

@ -22,18 +22,23 @@ public class House extends Estate {
public int getFloors() { public int getFloors() {
return floors; return floors;
} }
public void setFloors(int floors) { public void setFloors(int floors) {
this.floors = floors; this.floors = floors;
} }
public int getPrice() { public int getPrice() {
return price; return price;
} }
public void setPrice(int price) { public void setPrice(int price) {
this.price = price; this.price = price;
} }
public boolean hasGarden() { public boolean hasGarden() {
return garden; return garden;
} }
public void setGarden(boolean garden) { public void setGarden(boolean garden) {
this.garden = garden; this.garden = garden;
} }

View File

@ -7,22 +7,29 @@ public class Person {
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 void setId(int id) { this.id = id; }
public String getFirstName() { public String getFirstName() {
return firstName; return firstName;
} }
public void setFirstName(String firstName) { public void setFirstName(String firstName) {
this.firstName = firstName; this.firstName = firstName;
} }
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 adress) { public void setAddress(String adress) {
this.address = adress; this.address = adress;
} }

View File

@ -7,6 +7,7 @@ public class PurchaseContract extends Contract {
public PurchaseContract() { public PurchaseContract() {
super(); super();
} }
public PurchaseContract(Contract contract) { public PurchaseContract(Contract contract) {
this.setContractNo(contract.getContractNo()); this.setContractNo(contract.getContractNo());
this.setDate(contract.getDate()); this.setDate(contract.getDate());

View File

@ -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) {
@ -139,9 +140,12 @@ 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<>();
@ -165,10 +169,14 @@ 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<>();
@ -205,10 +213,14 @@ 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<>();
@ -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<>();
@ -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) {
@ -354,8 +376,7 @@ public class ORM {
if (count == 1) { if (count == 1) {
type = "TenancyContract"; type = "TenancyContract";
} }
} } else {
else {
type = "PurchaseContract"; type = "PurchaseContract";
} }
rs.close(); rs.close();
@ -414,7 +435,8 @@ 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) {
@ -447,8 +469,7 @@ public class ORM {
if (count == 1) { if (count == 1) {
type = "Apartment"; type = "Apartment";
} }
} } else {
else {
type = "House"; type = "House";
} }
rs.close(); rs.close();
@ -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) {
@ -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) {
@ -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();
@ -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,11 +707,12 @@ 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);
@ -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());
@ -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);
@ -779,7 +806,8 @@ public class ORM {
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
= "INSERT INTO HOUSE (ID, price, garden, floors) VALUES (?, ?, ?, ?)";
PreparedStatement pstmtHouse = _connection.prepareStatement(insertSQLHouse); 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());
@ -790,10 +818,10 @@ 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 =
"INSERT INTO APARTMENT (ID, floor, rent, rooms, balcony, builtInKitchen) " +
"VALUES (?, ?, ?, ?, ?, ?)"; "VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement pstmtApartment = _connection.prepareStatement(insertSQLApartment); PreparedStatement pstmtApartment = _connection.prepareStatement(insertSQLApartment);
pstmtApartment.setInt(1, apartment.getId()); pstmtApartment.setInt(1, apartment.getId());
@ -823,7 +851,8 @@ public class ORM {
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
= "UPDATE HOUSE SET floors = ?, garden = ?, price = ? WHERE ID = ?";
PreparedStatement pstmtHouse = _connection.prepareStatement(updateSQLHouse); 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);
@ -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) " +
@ -929,7 +957,8 @@ public class ORM {
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,10 +990,10 @@ 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 (?, ?, ?)";