diff --git a/numa/src/main/java/numa/NUMA.java b/numa/src/main/java/numa/NUMA.java index c5b644b..b92184c 100644 --- a/numa/src/main/java/numa/NUMA.java +++ b/numa/src/main/java/numa/NUMA.java @@ -36,6 +36,8 @@ public static void main (String[] args) throws IOException { try ( Connection conn = DriverManager.getConnection(DB_URL, loginInfo[0], loginInfo[1]); ) { + conn.setAutoCommit(false); + connected = true; while (connected) { try { @@ -75,6 +77,10 @@ public static void main (String[] args) throws IOException { } catch (MenuException e) { // Do nothing + } + catch (SQLException e) { + System.out.println("SQL Exception: " + e); + System.out.println("Returning to the main menu...\n"); } } } @@ -83,7 +89,7 @@ public static void main (String[] args) throws IOException { if (e.getErrorCode() == 1017) { System.out.println("Login denied. Please try again\n"); } else { - System.out.println("SQLException: " + e); + System.out.println("Failed to connect to the database. Please try again at a later time"); } } catch (IOException e) { diff --git a/numa/src/main/java/numa/Portals/Lease.java b/numa/src/main/java/numa/Portals/Lease.java new file mode 100644 index 0000000..5c1fe57 --- /dev/null +++ b/numa/src/main/java/numa/Portals/Lease.java @@ -0,0 +1,69 @@ +package numa.Portals; + +import java.sql.Timestamp; +import java.util.ArrayList; + +public class Lease { + int prop_id; + String street_name; + String apt; + String city; + String state; + String zip; + Timestamp start_date; + int term_length; + int rent_amount; + + public Lease(int prop_id, String street_name, String apt, String city, String state, String zip, Timestamp start_date, int term_length, int rent_amount) { + this.prop_id = prop_id; + this.street_name = street_name; + this.apt = apt; + this.city = city; + this.state = state; + this.zip = zip; + this.start_date = start_date; + this.term_length = term_length; + this.rent_amount = rent_amount; + } + + public String toString() { + String outStr = String.format( + "Address:\n" + + "%s\n" + + "Apt %s\n" + + "%s, %s %s\n" + + "Lease Start: %s\n" + + "Lease Term Length: %d\n" + + "Rent: $%d\n", + street_name, apt, city, state, zip, start_date, term_length, rent_amount + ); + return outStr; + } + + public String toString(ArrayList amenities) { + String outStr = this.toString(); + outStr += "Amenities:\n"; + for (Amenity prop: amenities) { + outStr += prop.toString(); + } + return outStr; + } +} + +class Amenity { + String amenity; + int cost; + + public Amenity(String amenity, int cost) { + this.amenity = amenity; + this.cost = cost; + } + + public String toString() { + String outStr = String.format( + " - %s: $%d\n", + amenity, cost + ); + return outStr; + } +} \ No newline at end of file diff --git a/numa/src/main/java/numa/Portals/Payment.java b/numa/src/main/java/numa/Portals/Payment.java index d41e16a..860b8a2 100644 --- a/numa/src/main/java/numa/Portals/Payment.java +++ b/numa/src/main/java/numa/Portals/Payment.java @@ -12,7 +12,7 @@ public class Payment { Reader input; int resId; - public Payment(Connection conn, Reader input, int resId) throws NumberFormatException, IOException, ExitException, MenuException { + public Payment(Connection conn, Reader input, int resId) throws NumberFormatException, IOException, ExitException, MenuException, SQLException { this.conn = conn; this.input = input; this.resId = resId; @@ -36,18 +36,18 @@ public Payment(Connection conn, Reader input, int resId) throws NumberFormatExce try { switch(choice) { - case 1: addACH(); - case 2: addCard(false); - case 3: addCard(true); - case 4: addVenmo(); - default: + case 1: addACH(); break; + case 2: addCard(false); break; + case 3: addCard(true); break; + case 4: addVenmo(); break; } + + conn.commit(); } catch (SQLException e) { System.out.println("Error adding payment: " + e); return; } - System.out.println("Payment Method Accepted!"); } diff --git a/numa/src/main/java/numa/Portals/ResidentPortal.java b/numa/src/main/java/numa/Portals/ResidentPortal.java index 84d7910..cf38857 100644 --- a/numa/src/main/java/numa/Portals/ResidentPortal.java +++ b/numa/src/main/java/numa/Portals/ResidentPortal.java @@ -6,10 +6,9 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.sql.Timestamp; import java.util.ArrayList; -import javax.naming.spi.DirStateFactory.Result; - import numa.Reader; import numa.Exceptions.*; @@ -48,15 +47,13 @@ public ResidentPortal(Connection conn, Reader input) throws IOException, ExitExc System.out.println("[1] My Info"); System.out.println("[2] Make Payment"); - System.out.println("[3] View Apartment Details"); - System.out.println("[4] Modify Lease"); - int mainChoice = super.portal(input, 4); + System.out.println("[3] My Lease Details"); + int mainChoice = super.portal(input, 3); switch (mainChoice) { case 1: resInfo(); break; case 2: makePayment(); break; case 3: viewAptDetails(); break; - case 4: modifyLease(); break; } super.sessionReset(input); @@ -129,6 +126,7 @@ public void resInfo() throws SQLException, NumberFormatException, IOException, E String handle = venmoInfo.getString("handle"); Venmo acc = new Venmo(handle); venmoOut += acc.toString(); + venmoOut += "\n"; } while (achInfo.next()) { @@ -137,6 +135,7 @@ public void resInfo() throws SQLException, NumberFormatException, IOException, E String rout = achInfo.getString("routing_num"); ACH acc = new ACH(acct, rout, bank); achOut += acc.toString(); + achOut += "\n"; } while (cardInfo.next()) { @@ -149,6 +148,7 @@ public void resInfo() throws SQLException, NumberFormatException, IOException, E Card acc = new Card(first, last, card_num, exp, cvv, pin); cardOut += acc.toString(); + cardOut += "\n"; } System.out.println(infoOut); @@ -157,18 +157,21 @@ public void resInfo() throws SQLException, NumberFormatException, IOException, E System.out.println(BOLD_ON + "Venmo" + BOLD_OFF); System.out.println(); System.out.println(venmoOut); + System.out.println(); } if (achOut != "") { System.out.println(BOLD_ON + "Bank ACH Transfers" + BOLD_OFF); System.out.println(); System.out.println(achOut); + System.out.println(); } if (cardOut != "") { System.out.println(BOLD_ON + "Payment Cards" + BOLD_OFF); System.out.println(); System.out.println(cardOut); + System.out.println(); } System.out.print("Do you need to add a new payment? [Y/N]: "); @@ -179,7 +182,6 @@ public void resInfo() throws SQLException, NumberFormatException, IOException, E default: return; } } - } /** Show all payments that need to be made and some details */ @@ -189,13 +191,63 @@ public void makePayment() { /** Read only of apartment details */ public void viewAptDetails() throws SQLException { try ( - Statement getApt = conn.prepareStatement(""); + PreparedStatement getApts = conn.prepareStatement( + "select prop_id, street_name, apt, city, state, zipcode, start_date, term_length, rent_amount from (person_on_lease join lease on person_on_lease.lease_id = lease.id) join property on prop_id = property.id where person_id = ?" + ); + PreparedStatement getPropAmenities = conn.prepareStatement( + "select amenity, cost from prop_amenity where prop_id = ?" + ); + PreparedStatement getAptAmenities = conn.prepareStatement( + "select amenity, cost from apt_amenity where prop_id = ? and apt = ?" + ); ) { - } - } + getApts.setInt(1, resId); + ResultSet leased_apts = getApts.executeQuery(); + ArrayList leased = new ArrayList(); + + while (leased_apts.next()) { + int prop_id = leased_apts.getInt("prop_id"); + String street_name = leased_apts.getString("street_name"); + String apt = leased_apts.getString("apt"); + String city = leased_apts.getString("city"); + String state = leased_apts.getString("state"); + String zip = leased_apts.getString("zipcode"); + Timestamp start_date = leased_apts.getTimestamp("start_date"); + int term_length = leased_apts.getInt("term_length"); + int rent_amount = leased_apts.getInt("rent_amount"); + + + Lease newLease = new Lease(prop_id, street_name, apt, city, state, zip, start_date, term_length, rent_amount); + leased.add(newLease); + } + + for (Lease lease : leased) { + ArrayList amenities = new ArrayList(); + + // Get property amenities + getPropAmenities.setInt(1, lease.prop_id); + ResultSet propAmen = getPropAmenities.executeQuery(); + + // Get apartment amenities + getAptAmenities.setInt(1, lease.prop_id); + getAptAmenities.setString(2, lease.apt); + ResultSet aptAmen = getAptAmenities.executeQuery(); - /** Actions to add/remove person/pet and extend lease */ - public void modifyLease() { + while (propAmen.next()) { + String name = propAmen.getString("amenity"); + int cost = propAmen.getInt("cost"); + amenities.add(new Amenity(name, cost)); + } + + while (aptAmen.next()) { + String name = aptAmen.getString("amenity"); + int cost = aptAmen.getInt("cost"); + amenities.add(new Amenity(name, cost)); + } + + System.out.println(lease.toString(amenities)); + } + } } }