Merge remote-tracking branch 'origin/master'

# Conflicts:
#	02/src/de/dis2017/Main.java
This commit is contained in:
Togepy95
2017-04-19 15:37:25 +02:00
3 changed files with 157 additions and 14 deletions

1
02/cli.bat Normal file
View File

@ -0,0 +1 @@
java -cp "out\production\DIS sheet 2:lib\db2jcc.jar:lib\db2jcc_license_cu.jar" de.dis2017.Main

View File

@ -239,6 +239,8 @@ public class Main {
* Creates a new estate agent after the usesr has entered the necessary data. * Creates a new estate agent after the usesr has entered the necessary data.
*/ */
private static void newEstate() { 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"); boolean isApartment = input.equals("A") || input.equals("a");
Estate estate = new Estate(); Estate estate = new Estate();
@ -247,11 +249,12 @@ public class Main {
estate.setPostalCode(FormUtil.readString("Postal Code")); estate.setPostalCode(FormUtil.readString("Postal Code"));
estate.setCity(FormUtil.readString("City")); estate.setCity(FormUtil.readString("City"));
estate.setSquareArea(FormUtil.readInt("Square Area")); estate.setSquareArea(FormUtil.readInt("Square Area"));
estate.setAgent(FormUtil.readInt("EstateAgent ID"));
if(isApartment){ if(isApartment){
Apartment apartment = (Apartment) estate; Apartment apartment = (Apartment) estate;
apartment.setFloor(FormUtil.readInt("Floor")); apartment.setFloor(FormUtil.readInt("Floor"));
apartment.setRent(FormUtil.readInt("Rent"));
apartment.setRooms(FormUtil.readInt("Rooms")); apartment.setRooms(FormUtil.readInt("Rooms"));
apartment.setRent(FormUtil.readInt("Rent"));
input = FormUtil.readString("Balcony(Y/N)"); input = FormUtil.readString("Balcony(Y/N)");
apartment.setBalcony(input.equals("Y") || input.equals("y")); apartment.setBalcony(input.equals("Y") || input.equals("y"));
input = FormUtil.readString("Built-in Kitchen(Y/N)"); input = FormUtil.readString("Built-in Kitchen(Y/N)");
@ -259,8 +262,8 @@ public class Main {
} }
else{ else{
House house = (House) estate; House house = (House) estate;
house.setFloors(FormUtil.readInt("Floors"));
house.setPrice(FormUtil.readInt("Price")); house.setPrice(FormUtil.readInt("Price"));
house.setFloors(FormUtil.readInt("Floors"));
input = FormUtil.readString("Garden(Y/N)"); input = FormUtil.readString("Garden(Y/N)");
house.setGarden(input.equals("Y") || input.equals("y")); house.setGarden(input.equals("Y") || input.equals("y"));
} }
@ -350,6 +353,8 @@ public class Main {
System.out.println("PostalCode: " + estate.getPostalCode()); System.out.println("PostalCode: " + estate.getPostalCode());
System.out.println("City: " + estate.getCity()); System.out.println("City: " + estate.getCity());
System.out.println("SquareArea: " + estate.getSquareArea()); System.out.println("SquareArea: " + estate.getSquareArea());
EstateAgent agent = _orm.getAgent(estate.getAgent());
System.out.println("Agent: " + agent.getName());
if (estate instanceof House) { if (estate instanceof House) {
House house = (House) estate; House house = (House) estate;
System.out.println("Price: " + house.getPrice()); System.out.println("Price: " + house.getPrice());
@ -367,6 +372,21 @@ public class Main {
System.out.println("------------------"); 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. * Modify estate.
* *
@ -375,33 +395,34 @@ public class Main {
private static void modifyEstate(Estate estate) { private static void modifyEstate(Estate estate) {
System.out.println("Modify Estate"); System.out.println("Modify Estate");
printEstateDetails(estate); printEstateDetails(estate);
printListOfAgents();
estate.setStreet(FormUtil.readString("Street",estate.getStreet())); estate.setStreet(FormUtil.readString("Street", estate.getStreet()));
estate.setStreetNumber(FormUtil.readInt("Street Number",estate.getStreetNumber())); estate.setStreetNumber(FormUtil.readInt("Street Number", estate.getStreetNumber()));
estate.setPostalCode(FormUtil.readString("Postal Code",estate.getPostalCode())); estate.setPostalCode(FormUtil.readString("Postal Code", estate.getPostalCode()));
estate.setCity(FormUtil.readString("City",estate.getCity())); estate.setCity(FormUtil.readString("City", estate.getCity()));
estate.setSquareArea(FormUtil.readInt("Square Area",estate.getSquareArea())); estate.setSquareArea(FormUtil.readInt("Square Area", estate.getSquareArea()));
estate.setAgent(FormUtil.readInt("EstateAgent ID", estate.getAgent()));
if(estate instanceof Apartment){ if (estate instanceof Apartment) {
Apartment apartment = (Apartment) estate; Apartment apartment = (Apartment) estate;
apartment.setFloor(FormUtil.readInt("Floor",apartment.getFloor())); apartment.setFloor(FormUtil.readInt("Floor",apartment.getFloor()));
apartment.setRent(FormUtil.readInt("Rent",apartment.getRent()));
apartment.setRooms(FormUtil.readInt("Rooms",apartment.getRooms())); 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")); 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{ else if (estate instanceof House){
House house = (House)estate; House house = (House) estate;
house.setFloors(FormUtil.readInt("Floors",house.getFloors())); house.setFloors(FormUtil.readInt("Floors",house.getFloors()));
house.setPrice(FormUtil.readInt("Price",house.getPrice())); house.setPrice(FormUtil.readInt("Price",house.getPrice()));
String input = FormUtil.readString("Garden(Y/N)",house.hasGarden()?"Y":"N"); String input = FormUtil.readString("Garden(Y/N)",house.hasGarden()?"Y":"N");
house.setGarden(input.equals("Y") || input.equals("y")); house.setGarden(input.equals("Y") || input.equals("y"));
} }
//_orm.persist(estate); _orm.persist(estate);
System.out.println("------------------"); System.out.println("------------------");
System.out.println("Estate with the ID " + estate.getId() + " was modified."); System.out.println("Estate with the ID " + estate.getId() + " was modified.");

View File

@ -85,7 +85,6 @@ public class ORM {
estate.setSquareArea(rs.getInt("squareArea")); estate.setSquareArea(rs.getInt("squareArea"));
estate.setAgent(rs.getInt("agent")); estate.setAgent(rs.getInt("agent"));
_estates.put(estate.getId(), estate);
estates.add(estate); estates.add(estate);
} }
@ -413,4 +412,126 @@ public class ORM {
_agentsUsername.put(agent.getLogin(), agent); _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();
}
}
} }