diff --git a/db/procedures/update_apt.sql b/db/procedures/update_apt.sql index 7a119cd..53e7131 100644 --- a/db/procedures/update_apt.sql +++ b/db/procedures/update_apt.sql @@ -17,7 +17,7 @@ BEGIN SELECT COUNT(*) into amt FROM lease WHERE - prop_id = ? AND apt = ? AND + prop_id = propId AND apt = apt AND TO_CHAR(ADD_MONTHS(start_date, term_length), 'YYYYMMDD') < TO_CHAR(CURRENT_TIMESTAMP, 'YYYYMMDD'); IF amt <> 0 THEN diff --git a/numa/src/main/java/numa/Exceptions/BadParamException.java b/numa/src/main/java/numa/Exceptions/BadParamException.java deleted file mode 100644 index d32b1ec..0000000 --- a/numa/src/main/java/numa/Exceptions/BadParamException.java +++ /dev/null @@ -1,10 +0,0 @@ -package numa.Exceptions; - -/** - * Custom exception for handling erroneous user-inputted parameters - */ -public class BadParamException extends Exception { - public BadParamException(String err) { - super(err); - } -} diff --git a/numa/src/main/java/numa/Exceptions/MenuException.java b/numa/src/main/java/numa/Exceptions/MenuException.java index 7968443..07df987 100644 --- a/numa/src/main/java/numa/Exceptions/MenuException.java +++ b/numa/src/main/java/numa/Exceptions/MenuException.java @@ -2,5 +2,6 @@ public class MenuException extends Exception { public MenuException() { + super(); } } diff --git a/numa/src/main/java/numa/NUMA.java b/numa/src/main/java/numa/NUMA.java index 467d4af..ccdbc1a 100644 --- a/numa/src/main/java/numa/NUMA.java +++ b/numa/src/main/java/numa/NUMA.java @@ -15,11 +15,11 @@ public class NUMA { public static void main (String[] args) throws IOException { boolean connected = false; - // Re-prompts whenever user is not connected - do { - try ( - Reader input = new Reader(); - ) { + try ( + Reader input = new Reader(); + ) { + // Re-prompts whenever user is not connected + do { String[] loginInfo = new String[2]; Dotenv dotenv = Dotenv.load(); String DB_URL = dotenv.get("DB_URL"); @@ -31,77 +31,72 @@ public static void main (String[] args) throws IOException { loginInfo = getLogin(input); } - // Try logging in and creating a prepared statement try ( Connection conn = DriverManager.getConnection(DB_URL, loginInfo[0], loginInfo[1]); ) { conn.setAutoCommit(false); - connected = true; while (connected) { try { + System.out.println(); System.out.println("============================================"); System.out.println("Northside Uncommons Management of Apartments"); System.out.println("============================================"); System.out.println("Enter 'm' to return here and 'q' to quit the program at any time\n"); System.out.println("[1] Resident Portal"); - System.out.println("[2] Management Portal"); - // System.out.println("[3] Shareholder Disclosures\n"); + System.out.println("[2] Management Portal\n"); + + boolean portalEntered = false; - Boolean portalEntered = false; while (!portalEntered) { try { String portalChoice = input.getMenuLine("Portal #: "); - - switch(portalChoice) { - case "1": - new ResidentPortal(conn, input); - portalEntered = true; - break; - case "2": - new ManagementPortal(conn, input); - portalEntered = true; - break; - // case "3": - // new ShareholderPortal(conn, input); - // portalEntered = true; - // break; - default: - System.out.println("Invalid input, please only enter numbers 1 - 3\n"); - } - } finally {} + + switch (portalChoice) { + case "1": + new ResidentPortal(conn, input); + portalEntered = true; + break; + // case "2": + // new ManagementPortal(conn, input); + // portalEntered = true; + // break; + default: + System.out.println("Invalid input, please only enter numbers 1 - 2\n"); + } + } + finally {} } + System.out.println("Returning to the main menu...\n"); } catch (MenuException e) { // Do nothing - } + } catch (SQLException e) { System.out.println("SQL Exception: " + e); System.out.println("Returning to the main menu...\n"); } } - } + } catch (SQLException e) { - // Specific exception for bad user/pass combo if (e.getErrorCode() == 1017) { - System.out.println("Login denied. Please try again"); + System.out.println("Login denied. Please try again\n"); } else { System.out.println("Failed to connect to the database. Please try again at a later time"); } } - catch (IOException e) { - System.out.println("IOException: " + e); - } - catch (ExitException e) { - System.out.println("Exiting..."); - } - } catch (IOException e) { - System.out.println("IOException: " + e); - } - } while (!connected); + } while (!connected); + + } + catch (IOException e) { + System.out.println("IO Exception: " + e); + } + catch (ExitException e) { + System.out.println("Exiting..."); + } } /** diff --git a/numa/src/main/java/numa/Reader.java b/numa/src/main/java/numa/Reader.java index 524a73b..a26966e 100644 --- a/numa/src/main/java/numa/Reader.java +++ b/numa/src/main/java/numa/Reader.java @@ -5,27 +5,29 @@ import java.io.InputStreamReader; import numa.Exceptions.*; - /** * Enhanced version of Java's BufferedReader * Reader with various parsing methods */ public class Reader extends BufferedReader { + int MAX_TRIES = 5; + public Reader() { super(new InputStreamReader(System.in)); } - /** Used for menu input (Navigating between portals and subportals) */ + /** + * Used for menu input (Navigating between portals and subportals) + * Allows for immediate quitting and main menu jump + */ public String getMenuLine(String prompt) throws IOException, ExitException, MenuException { System.out.print(prompt); String input = super.readLine().toLowerCase(); System.out.println(); if (input.equals("q") || input.equals("'q'")) { - System.out.println(); throw new ExitException(); } else if (input.equals("m") || input.equals("'m'")) { - System.out.println(); throw new MenuException(); } return input; @@ -49,39 +51,23 @@ public String getPrompt(String prompt) throws IOException, ExitException { System.out.print(prompt); String input = super.readLine(); System.out.println(); - if (input.equals("q") || input.equals("'q'")) { - throw new ExitException(); - } return input; } - /** Used for non-menu inputs that allows validation */ - public String getPrompt(String prompt, Validate validation) throws IOException, ExitException { - while (true) { + /** Used for non-menu inputs that allows for input validation */ + public String getPrompt(String prompt, Validate validation) throws IOException, ExitException, MenuException { + int i = 0; + while (i++ < MAX_TRIES) { System.out.print(prompt); String input = super.readLine(); System.out.println(); - if (input.equals("q") || input.equals("'q'")) { - throw new ExitException(); - } Validated res = validation.validate(input); - if (res.errMsg.equals("")) { - return input; + if (res.valid) { + return res.validated; } - System.out.println(res.errMsg + "\n"); - } - } - - /** Used for multi-action prompts that allow adding to the database */ - public int getAddPrompt(String prompt) throws IOException, ExitException, MenuException { - String input = this.getMenuLine(prompt); - if (input.equals("a") || input.equals("'a'")) { - return 0; - } - try { - return Integer.parseInt(input); - } catch (NumberFormatException e) { - throw new NumberFormatException(); + System.out.println(res.errMsg); } + System.out.println("Too many attempts. Returning to the main menu..."); + throw new MenuException(); } } diff --git a/numa/src/main/java/numa/Validated.java b/numa/src/main/java/numa/Validated.java index f4ba603..e18a5cd 100644 --- a/numa/src/main/java/numa/Validated.java +++ b/numa/src/main/java/numa/Validated.java @@ -3,13 +3,20 @@ public class Validated { String validated; String errMsg; + boolean valid; - public Validated(String validated, String errMsg) { + public Validated(String validated, String errMsg, boolean valid) { this.validated = validated; this.errMsg = errMsg; + this.valid = valid; } } interface Validate { + /** + * Validation function format + * Returns true on proper validation and false otherwise + * public boolean validateX(); + */ public Validated validate(String input); -} +} \ No newline at end of file