Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Commit

Permalink
finish major parts of both portals
Browse files Browse the repository at this point in the history
  • Loading branch information
L23de committed May 4, 2022
1 parent 8b99c1f commit 3238446
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 195 deletions.
213 changes: 104 additions & 109 deletions db/aggregate.sql

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions db/function/sign_lease_helper.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
create or replace FUNCTION sign_lease_helper (
propId IN lease.prop_id%type,
aptId IN lease.apt%type,
termLength IN lease.term_length%type
) RETURN number
IS
amt number;
valid number;
rent_amount lease.rent_amount%type;
leaseId number;
BEGIN
-- Ensures that the lease does not exist the same time as another
SELECT COUNT(*) into amt
FROM lease
WHERE
prop_id = propId and apt = aptId AND
CURRENT_TIMESTAMP <= ADD_MONTHS(start_date, term_length);

-- Ensures that the apartment is a valid apartment
SELECT COUNT(*) into valid
FROM apartment
WHERE prop_id = propId and apt = aptId;

IF amt <> 0 THEN
return -1;
ELSIF valid = 0 THEN
return -2;
END IF;

SELECT RENT into rent_amount
FROM apartment where prop_id = propId and apt = aptId;

INSERT INTO lease(prop_id, apt, start_date, term_length, rent_amount)
VALUES(propId, aptId, CURRENT_TIMESTAMP, termLength, rent_amount) RETURNING id into leaseId;
commit;
return leaseId;
END;
4 changes: 3 additions & 1 deletion db/procedures/add_ach.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE OR REPLACE PROCEDURE add_ach (
create or replace PROCEDURE add_ach (
person_id payment_method.person_id%type,
acct_num ach.acct_num%type,
routing_num ach.routing_num%type,
Expand All @@ -12,3 +12,5 @@ CREATE OR REPLACE PROCEDURE add_ach (
INSERT INTO payment_method(person_id, card_id, venmo_id, ach_id) VALUES(person_id, NULL, NULL, ach_id);
END;



4 changes: 3 additions & 1 deletion db/procedures/add_card.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE OR REPLACE PROCEDURE add_card (
create or replace PROCEDURE add_card (
person_id payment_method.person_id%type,
first_name pay_card.first_name%type,
last_name pay_card.last_name%type,
Expand All @@ -14,3 +14,5 @@ CREATE OR REPLACE PROCEDURE add_card (
INSERT INTO payment_method(person_id, card_id, venmo_id, ach_id) VALUES(person_id, card_id, NULL, NULL);
END;



4 changes: 3 additions & 1 deletion db/procedures/add_person_to_lease.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE OR REPLACE PROCEDURE add_person_to_lease (
create or replace PROCEDURE add_person_to_lease (
pid IN person.id%type,
lid IN lease.id%type,
success OUT number
Expand All @@ -20,3 +20,5 @@ BEGIN
INSERT INTO person_on_lease VALUES(lid, pid);
success := 0;
END;


7 changes: 6 additions & 1 deletion db/procedures/add_pet.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
CREATE OR REPLACE PROCEDURE add_pet (
create or replace PROCEDURE add_pet (
animal pet.animal%type,
pet_name pet.pet_name%type,
lease_id lease.id%type
) IS
pet_id pet.id%type;
BEGIN
INSERT INTO pet(animal, pet_name) VALUES(animal, pet_name);
commit;
SELECT MAX(id) into pet_id from pet;
INSERT INTO pet_on_lease(lease_id, pet_id) VALUES(lease_id, pet_id);
commit;
END;



35 changes: 25 additions & 10 deletions db/procedures/add_venmo.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
CREATE OR REPLACE PROCEDURE add_venmo (
person_id payment_method.person_id%type,
handle venmo.handle%type
) IS
venmo_id payment_method.venmo_id%type;
BEGIN
INSERT INTO venmo(handle) VALUES(handle);
SELECT MAX(id) into venmo_id from venmo;
INSERT INTO payment_method(person_id, card_id, venmo_id, ach_id) VALU ES(person_id, NULL, venmo_id, NULL);
END;
create or replace PROCEDURE make_payment (
amen_id IN amenity_payment.amenity_id%type,
lease_id IN lease_payment.lease_id%type,
pay_method IN payment_method.id%type,
pay_amt IN amenity_payment.pay_amt%type,
payer IN person.id%type,
memo IN amenity_payment.memo%type,
ret OUT NUMBER
)
IS
BEGIN
IF amen_id <> -1 THEN
INSERT INTO amenity_payment(amenity_id, date_paid, pay_method_id, pay_amt, payer, memo)
VALUES(amen_id, CURRENT_TIMESTAMP, pay_method, pay_amt, payer, memo);
ret := 0;
ELSIF lease_id <> -1 THEN
INSERT INTO lease_payment(lease_id, date_paid, pay_method_id, pay_amt, payer, memo)
VALUES(lease_id, CURRENT_TIMESTAMP, pay_method, pay_amt, payer, memo);
ret := 0;
ELSE
ret := 1;
END IF;
END;


7 changes: 6 additions & 1 deletion db/procedures/bulk_add_apts.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
-- Used by NUMA to add apartments for a new property
-- Do last

CREATE OR REPLACE PROCEDURE
CREATE OR REPLACE PROCEDURE bulk_add (
propId property.id%type,
maxLetter char,
maxNumber number,

)
53 changes: 17 additions & 36 deletions db/procedures/sign_lease.sql
Original file line number Diff line number Diff line change
@@ -1,46 +1,27 @@
CREATE OR REPLACE PROCEDURE sign_lease (
create or replace PROCEDURE sign_lease (
personId IN payment_method.person_id%type,
propId IN lease.prop_id%type,
apt IN lease.apt%type,
aptId IN lease.apt%type,
termLength IN lease.term_length%type,
success OUT number
ssn IN renter_info.ssn%type,
success OUT number
) IS
amt number;
valid number;
rent_amount lease.rent_amount%type;
lease_id lease.id%type;
leaseId lease.id%type;
BEGIN
-- Ensures that the lease does not exist the same time as another
SELECT COUNT(*) into amt
FROM lease
WHERE
prop_id = propId and apt = apt AND
CURRENT_TIMESTAMP <= ADD_MONTHS(start_date, term_length);
leaseId := sign_lease_helper(propId, aptId, termLength);
dbms_output.put_line(leaseId);

-- Ensures that the apartment is a valid apartment
SELECT COUNT(*) into valid
FROM apartment
WHERE prop_id = propId and apt = apt;

IF amt <> 0 THEN
success := -1;
if leaseId > 0 then
INSERT INTO renter_info(person_id, ssn) VALUES(personId, ssn);
commit;
INSERT INTO person_on_lease VALUES(leaseId, personId);
commit;
success := 0;
return;
ELSIF valid <> 0 THEN
success := -2;
return;
END IF;

SELECT RENT into rent_amount
FROM apartment where prop_id = propId and apt = apt;

INSERT INTO lease(prop_id, apt, start_date, term_length, rent_amount)
VALUES(propId, apt, CURRENT_TIMESTAMP, termLength, rent_amount);

SELECT MAX(id) into lease_id
FROM lease;
end if;

INSERT INTO person_on_lease VALUES(lease_id, personId);

success := 0;
success := -1;
return;
END;


11 changes: 5 additions & 6 deletions db/procedures/update_apt.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- Used to update an apartment's specs (Maybe due to renovation or rent hike)
-- Make sure that the apartment is out of lease

CREATE OR REPLACE PROCEDURE update_apt (
create or replace PROCEDURE update_apt (
propId IN apartment.prop_id%type,
apt IN apartment.apt%type,
sqFeet IN apartment.square_footage%type,
Expand All @@ -19,12 +16,12 @@ BEGIN
WHERE
prop_id = propId AND apt = apt AND
ADD_MONTHS(start_date, term_length) < CURRENT_TIMESTAMP;

IF amt <> 0 THEN
success := -1;
return;
END IF;

UPDATE apartment
SET
square_footage = sqFeet,
Expand All @@ -35,3 +32,5 @@ BEGIN

success := 0;
END;


2 changes: 1 addition & 1 deletion db/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ CREATE TABLE pay_card (
first_name VARCHAR2(255) NOT NULL,
last_name VARCHAR2(255) NOT NULL,
card_num CHAR(19) NOT NULL
CHECK (REGEXP_LIKE(card_num, '\d{19}')),
CHECK (REGEXP_LIKE(card_num, '\d{16}')),
exp_date CHAR(7) NOT NULL
CHECK (exp_date LIKE '__/____'),
cvv CHAR(3) NOT NULL
Expand Down
26 changes: 6 additions & 20 deletions numa/src/main/java/numa/Portals/ManagementPortal.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package numa.Portals;

import java.io.IOException;
import java.lang.reflect.Array;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
Expand All @@ -11,12 +10,9 @@
import java.sql.Types;
import java.util.ArrayList;

import javax.naming.spi.DirStateFactory.Result;

import numa.Reader;
import numa.Exceptions.*;


public class ManagementPortal extends Portal {
final String BOLD_ON = "\033[1m";
final String BOLD_OFF = "\033[0m";
Expand Down Expand Up @@ -93,7 +89,7 @@ private void properties() throws SQLException, IOException, ExitException, MenuE

Property tmp = new Property(id, street, city, state, zip);
props.add(tmp);
outStr += System.out.format(
outStr += String.format(
"[%d] %s\n",
++counter, tmp.toString()
);
Expand Down Expand Up @@ -126,15 +122,15 @@ private void properties() throws SQLException, IOException, ExitException, MenuE
ResultSet aptRes = getApts.executeQuery();
ArrayList<Apartment> aptList = new ArrayList<Apartment>();

counter = 0;


System.out.format(
"%s%s Apartments:%s\n",
BOLD_ON,
props.get(counter - 1).toString(),
BOLD_OFF
);


counter = 0;
while (aptRes.next()) {
int prop_id = aptRes.getInt("prop_id");
String apt = aptRes.getString("apt");
Expand Down Expand Up @@ -212,19 +208,9 @@ private void people() throws IOException, ExitException, MenuException, SQLExcep
if (choice == 1 || choice == 2) break;
}

String sqlPrefix = String.format(
"select " +
"person.id as id, first_name, last_name, ssn, age, phone_number, email, credit_score, apt " +
"from " +
"(((person left outer join renter_info on renter_info.person_id = person.id) " +
"natural left outer join person_on_lease) " +
"left outer join lease on lease_id = lease.id)" +
"natural left outer join apartment "
);

switch (choice) {
case 1: Person.searchName(sqlPrefix, conn, input, true); break;
case 2: Person.searchId(sqlPrefix, conn, input); break;
case 1: Person.searchName(conn, input, true); break;
case 2: Person.searchId(conn, input); break;

}
}
Expand Down
29 changes: 22 additions & 7 deletions numa/src/main/java/numa/Portals/Objects.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ class Person implements Objects {
String email;
int credit_score;

static String sqlPrefix = String.format(
"select " +
"person.id as id, first_name, last_name, ssn, age, phone_number, email, credit_score, apt " +
"from " +
"(((person left outer join renter_info on renter_info.person_id = person.id) " +
"natural left outer join person_on_lease) " +
"left outer join lease on lease_id = lease.id)" +
"natural left outer join apartment "
);

public Person(boolean valid) {
this.id = -1;
}
Expand Down Expand Up @@ -260,10 +270,10 @@ public String toString() {
return outStr;
}

public static void searchName(String prefix, Connection conn, Reader input, boolean verbose) throws SQLException, IOException, ExitException {
prefix += "where first_name = ? or last_name = ?";
public static void searchName(Connection conn, Reader input, boolean verbose) throws SQLException, IOException, ExitException {
String stmt = sqlPrefix + "where first_name = ? or last_name = ?";
try (
PreparedStatement searchName = conn.prepareStatement(prefix);
PreparedStatement searchName = conn.prepareStatement(stmt);
) {
String name = input.getPrompt("First or Last Name: ");
name = name.substring(0, 1).toUpperCase() + name.substring(1);
Expand All @@ -272,6 +282,7 @@ public static void searchName(String prefix, Connection conn, Reader input, bool
searchName.setString(2, name);

ResultSet people = searchName.executeQuery();
Person tmp = null;
while (people.next()) {
int id = people.getInt("id");
String first = people.getString("first_name");
Expand All @@ -283,21 +294,25 @@ public static void searchName(String prefix, Connection conn, Reader input, bool
int credit_score = people.getInt("credit_score");
String apt = people.getString("apt");

Person tmp = new Person(id, first, last, ssn, age, phone_num, email, credit_score);
tmp = new Person(id, first, last, ssn, age, phone_num, email, credit_score);

System.out.println(tmp.toString());
if (apt != null) {
System.out.format("Apartment Leased: %s\n", apt);
}
System.out.println();
}

if (tmp == null) {
System.out.println("No person found");
}
}
}

public static void searchId(String prefix, Connection conn, Reader input) throws IOException, ExitException, MenuException, SQLException, TooManyTriesException {
prefix += "where person.id = ?";
public static void searchId(Connection conn, Reader input) throws IOException, ExitException, MenuException, SQLException, TooManyTriesException {
String stmt = sqlPrefix + "where person.id = ?";
try (
PreparedStatement searchName = conn.prepareStatement(prefix);
PreparedStatement searchName = conn.prepareStatement(stmt);
) {
int id = input.getMenuInt("Person ID: ");
searchName.setInt(1, id);
Expand Down
2 changes: 1 addition & 1 deletion numa/src/main/java/numa/Portals/ResidentPortal.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public void makePayment() throws SQLException, MenuException, IOException {
System.out.println(outStr);
}

String in = input.getPrompt("Payments are made in full. For multiple payments, separate numbers with commas\nPay for: ");
String in = input.getPrompt("Payments are made in full. Pay for: ");
String[] choices = in.split(",");
ArrayList<String> errors = new ArrayList<String>();

Expand Down

0 comments on commit 3238446

Please sign in to comment.