diff --git a/02/cli.bat b/02/cli.bat new file mode 100644 index 0000000..3d84bce --- /dev/null +++ b/02/cli.bat @@ -0,0 +1 @@ +java -cp "out\production\DIS sheet 2:lib\db2jcc.jar:lib\db2jcc_license_cu.jar" de.dis2017.Main \ No newline at end of file diff --git a/02/src/de/dis2017/Main.java b/02/src/de/dis2017/Main.java index 3d9319e..acd28e7 100644 --- a/02/src/de/dis2017/Main.java +++ b/02/src/de/dis2017/Main.java @@ -239,6 +239,8 @@ public class Main { * Creates a new estate agent after the usesr has entered the necessary data. */ private static void newEstate() { + printListOfAgents(); + String input = FormUtil.readString("Apartment(A)/House(H)"); boolean isApartment = input.equals("A") || input.equals("a"); Estate estate = new Estate(); @@ -247,11 +249,12 @@ public class Main { estate.setPostalCode(FormUtil.readString("Postal Code")); estate.setCity(FormUtil.readString("City")); estate.setSquareArea(FormUtil.readInt("Square Area")); + estate.setAgent(FormUtil.readInt("EstateAgent ID")); if(isApartment){ Apartment apartment = (Apartment) estate; apartment.setFloor(FormUtil.readInt("Floor")); - apartment.setRent(FormUtil.readInt("Rent")); apartment.setRooms(FormUtil.readInt("Rooms")); + apartment.setRent(FormUtil.readInt("Rent")); input = FormUtil.readString("Balcony(Y/N)"); apartment.setBalcony(input.equals("Y") || input.equals("y")); input = FormUtil.readString("Built-in Kitchen(Y/N)"); @@ -259,8 +262,8 @@ public class Main { } else{ House house = (House) estate; - house.setFloors(FormUtil.readInt("Floors")); house.setPrice(FormUtil.readInt("Price")); + house.setFloors(FormUtil.readInt("Floors")); input = FormUtil.readString("Garden(Y/N)"); house.setGarden(input.equals("Y") || input.equals("y")); } @@ -350,6 +353,8 @@ public class Main { System.out.println("PostalCode: " + estate.getPostalCode()); System.out.println("City: " + estate.getCity()); System.out.println("SquareArea: " + estate.getSquareArea()); + EstateAgent agent = _orm.getAgent(estate.getAgent()); + System.out.println("Agent: " + agent.getName()); if (estate instanceof House) { House house = (House) estate; System.out.println("Price: " + house.getPrice()); @@ -367,6 +372,21 @@ public class Main { System.out.println("------------------"); } + /** + * Print a list of agents. + */ + private static void printListOfAgents() { + 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()); + } + System.out.println("------------------"); + } + /** * Modify estate. * @@ -375,33 +395,34 @@ public class Main { 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.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){ + if (estate instanceof Apartment) { Apartment apartment = (Apartment) estate; apartment.setFloor(FormUtil.readInt("Floor",apartment.getFloor())); - apartment.setRent(FormUtil.readInt("Rent",apartment.getRent())); 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"); apartment.setBalcony(input.equals("Y") || input.equals("y")); input = FormUtil.readString("Built-in Kitchen(Y/N)",apartment.hasBuiltinKitchen()?"Y":"N"); apartment.setBuiltinKitchen(input.equals("Y") || input.equals("y")); } - else{ - House house = (House)estate; - + 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"); house.setGarden(input.equals("Y") || input.equals("y")); } - //_orm.persist(estate); + _orm.persist(estate); System.out.println("------------------"); System.out.println("Estate with the ID " + estate.getId() + " was modified."); diff --git a/02/src/de/dis2017/data/db/ORM.java b/02/src/de/dis2017/data/db/ORM.java index d060e0f..b9348c2 100644 --- a/02/src/de/dis2017/data/db/ORM.java +++ b/02/src/de/dis2017/data/db/ORM.java @@ -85,7 +85,6 @@ public class ORM { estate.setSquareArea(rs.getInt("squareArea")); estate.setAgent(rs.getInt("agent")); - _estates.put(estate.getId(), estate); estates.add(estate); } @@ -413,4 +412,126 @@ public class ORM { _agentsUsername.put(agent.getLogin(), agent); } } + + /** + * Persists the given estate. + * + * @param estate the estate that should be persisted + */ + public void persist(Estate estate) + { + boolean changeFinished = false; + try { + _connection.setAutoCommit(false); + if (estate.getId() == -1) { + String insertSQL = "INSERT INTO ESTATE (city, postalCode, street, streetNumber, squareArea, agent) " + + "VALUES (?, ?, ?, ?, ?, ?)"; + PreparedStatement pstmt = _connection.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS); + pstmt.setString(1, estate.getCity()); + pstmt.setString(2, estate.getPostalCode()); + pstmt.setString(3, estate.getStreet()); + pstmt.setInt(4, estate.getStreetNumber()); + pstmt.setInt(5, estate.getSquareArea()); + pstmt.setInt(6, estate.getAgent()); + pstmt.executeUpdate(); + + ResultSet rs = pstmt.getGeneratedKeys(); + if (rs.next()) { + estate.setId(rs.getInt(1)); + } + rs.close(); + 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); + pstmtHouse.setInt(1, house.getId()); + pstmtHouse.setInt(2, house.getPrice()); + pstmtHouse.setInt(3, house.hasGarden() ? 1 : 0); + pstmtHouse.setInt(4, house.getFloors()); + pstmt.executeUpdate(); + pstmtHouse.executeUpdate(); + pstmt.close(); + pstmtHouse.close(); + changeFinished = true; + } + else if (estate instanceof Apartment) { + Apartment apartment = (Apartment) estate; + 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()); + pstmtApartment.setInt(3, apartment.getRent()); + pstmtApartment.setInt(4, apartment.getRooms()); + pstmtApartment.setInt(5, apartment.hasBalcony() ? 1 : 0); + pstmtApartment.setInt(6, apartment.hasBuiltinKitchen() ? 1 : 0); + pstmt.executeUpdate(); + pstmtApartment.executeUpdate(); + pstmt.close(); + pstmtApartment.close(); + changeFinished = true; + } + } else { + // create query + String updateSQL = "UPDATE ESTATE SET city = ?, postalCode = ?, street = ?, streetNumber = ?, " + + "squareArea = ?, agent = ? WHERE ID = ?"; + PreparedStatement pstmt = _connection.prepareStatement(updateSQL); + pstmt.setString(1, estate.getCity()); + pstmt.setString(2, estate.getPostalCode()); + pstmt.setString(3, estate.getStreet()); + pstmt.setInt(4, estate.getStreetNumber()); + pstmt.setInt(5, estate.getSquareArea()); + pstmt.setInt(6, estate.getAgent()); + 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); + pstmtHouse.setInt(1, house.getFloors()); + pstmtHouse.setInt(2, house.hasGarden() ? 1 : 0); + pstmtHouse.setInt(3, house.getPrice()); + pstmtHouse.setInt(4, house.getId()); + pstmt.executeUpdate(); + pstmtHouse.executeUpdate(); + pstmt.close(); + pstmtHouse.close(); + changeFinished = true; + } + else if (estate instanceof Apartment) { + Apartment apartment = (Apartment) estate; + String updateSQLApartment = "UPDATE APARTMENT SET floor = ?, rent = ?, rooms = ?, " + + "balcony = ?, builtInKitchen = ? WHERE ID = ?"; + PreparedStatement pstmtApartment = _connection.prepareStatement(updateSQLApartment); + pstmtApartment.setInt(1, apartment.getFloor()); + pstmtApartment.setInt(2, apartment.getRent()); + pstmtApartment.setInt(3, apartment.getRooms()); + pstmtApartment.setInt(4, apartment.hasBalcony() ? 1 : 0); + pstmtApartment.setInt(5, apartment.hasBuiltinKitchen() ? 1 : 0); + pstmtApartment.setInt(6, apartment.getId()); + pstmt.executeUpdate(); + pstmtApartment.executeUpdate(); + pstmt.close(); + pstmtApartment.close(); + changeFinished = true; + } + } + if (changeFinished) { + _connection.commit(); + if (!_estates.containsKey(estate.getId())) { + _estates.put(estate.getId(), estate); + } + } + _connection.setAutoCommit(true); + } catch (SQLException e) { + try { + _connection.rollback(); + } catch (SQLException e1) { + e1.printStackTrace(); + } + e.printStackTrace(); + } + } }