From cacb97221c86fd26f9bdbf2e2418d8cfa903f9ab Mon Sep 17 00:00:00 2001 From: Sherwin Price Date: Wed, 10 Sep 2025 15:44:29 -0400 Subject: [PATCH] updates cleanup. --- .../AccomodationManager.java | 21 +- src/java/lodge/reservationsystem/Account.java | 25 ++- .../lodge/reservationsystem/AccountList.java | 32 ++- .../AccountReservationList.java | 44 +++-- .../reservationsystem/CabinReservation.java | 2 + .../reservationsystem/DataRepository.java | 186 +++++++++--------- .../reservationsystem/HotelReservation.java | 2 + .../reservationsystem/HouseReservation.java | 1 + .../lodge/reservationsystem/Reservation.java | 8 +- src/resources/acc-A1450981765.json | 0 src/resources/rsv-R0123077641.json | 0 src/resources/rsv-R0499811708.json | 2 +- src/resources/rsv-R2042828431.json | 2 +- 13 files changed, 181 insertions(+), 144 deletions(-) mode change 100644 => 100755 src/resources/acc-A1450981765.json mode change 100644 => 100755 src/resources/rsv-R0123077641.json mode change 100644 => 100755 src/resources/rsv-R0499811708.json mode change 100644 => 100755 src/resources/rsv-R2042828431.json diff --git a/src/java/lodge/reservationsystem/AccomodationManager.java b/src/java/lodge/reservationsystem/AccomodationManager.java index 824f166..2420d3f 100644 --- a/src/java/lodge/reservationsystem/AccomodationManager.java +++ b/src/java/lodge/reservationsystem/AccomodationManager.java @@ -7,7 +7,8 @@ public final class AccomodationManager { private final AccountList account_list = new AccountList(); - AccomodationManager() { + @SuppressWarnings("unused") + private AccomodationManager() { }; public AccomodationManager(String home) { @@ -24,7 +25,7 @@ public final class AccomodationManager { // walk directories Path rootDir = Paths.get(DataRepository.getPath()); DataRepository.WalkFileSystemTree(this, rootDir); - System.out.println(String.format("Accounts Loaded %d", account_list.size())); + System.out.println(String.format("%s LoadAll Accounts %d", "Deserializing", account_list.size())); } // Load / Deserialize Account @@ -43,16 +44,16 @@ public final class AccomodationManager { } public Account retrieveAccount(String acct_id) { - return AccountList.retrieveAccount(account_list, acct_id); + return account_list.find(acct_id); } - public void AddAccount(Account acct) throws Exception { - AccountList.addAccount(account_list, acct); + public synchronized void AddAccount(final Account acct) throws Exception { + account_list.add(acct); } - public void UpdateAccount(Account acct) throws Exception { + public synchronized void UpdateAccount(final Account acct) throws Exception { if( acct != null ){ - AccountList.save(account_list, acct); + account_list.save(acct); } } @@ -62,9 +63,9 @@ public final class AccomodationManager { try { acct = AccountList.newAccount(phone_number, mailing_address, email_address); } catch (Exception e) { - e.printStackTrace(); + System.out.println(e.toString()); } - AccountList.save(account_list, acct); + account_list.save(acct); return acct; } @@ -76,7 +77,7 @@ public final class AccomodationManager { public Reservation retreiveReservation(String reservation_number) { for (Account acct : account_list) { for (Reservation rsv : acct.reservation_list) { - if (rsv.getReservation_number() == reservation_number) { + if (rsv.getReservation_number().compareTo(reservation_number)==0) { return rsv; } } diff --git a/src/java/lodge/reservationsystem/Account.java b/src/java/lodge/reservationsystem/Account.java index 48f2bbe..50af596 100644 --- a/src/java/lodge/reservationsystem/Account.java +++ b/src/java/lodge/reservationsystem/Account.java @@ -13,10 +13,9 @@ public class Account { Address mailing_address; EmailAddress email_address; - AccountReservationList reservation_list = new AccountReservationList();; + @SuppressWarnings("unused") + protected Account() { - public AccountReservationList getReservation_list() { - return reservation_list; } public Account( @@ -30,8 +29,10 @@ public class Account { this.email_address = email_address; } - public Account() { + AccountReservationList reservation_list = new AccountReservationList();; + public AccountReservationList getReservation_list() { + return reservation_list; } @Override @@ -50,6 +51,9 @@ public class Account { public boolean add(Reservation rsrv) throws ReservationException { boolean result = false; + if (rsrv == null) { + return false; + } try { rsrv.checkValid(); result = reservation_list.add(rsrv); @@ -57,10 +61,10 @@ public class Account { if (!result) { throw new DuplicateObjectException("Error Reservation already exists."); } + } catch (DuplicateObjectException e) { + System.out.println(String.format("%s", e.getMessage())); } catch (ReservationException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); + System.out.println(e.toString()); } finally { } return result; @@ -137,4 +141,11 @@ public class Account { return false; return true; } + + public void update(Account acct) { + this.setEmail_address(acct.email_address); + this.setPhone_number(acct.phone_number); + this.setMailing_address(acct.mailing_address); + this.getReservation_list().update(acct.getReservation_list()); + } } \ No newline at end of file diff --git a/src/java/lodge/reservationsystem/AccountList.java b/src/java/lodge/reservationsystem/AccountList.java index 31e2620..a34d916 100644 --- a/src/java/lodge/reservationsystem/AccountList.java +++ b/src/java/lodge/reservationsystem/AccountList.java @@ -6,7 +6,7 @@ import java.util.ArrayList; public class AccountList extends ArrayList { public AccountList() { - + super(); } public static String accountSerial(String phone_number, Address mailing_address, EmailAddress email_address) { @@ -24,38 +24,34 @@ public class AccountList extends ArrayList { } // ** Add account throw error if account number is pre-existing. */ - public static Account addAccount(final AccountList account_list, final Account account) - throws DuplicateObjectException, AccountException { + @Override + public synchronized boolean add(final Account account) + throws DuplicateObjectException { try { - for (Account acct : account_list) { + for (Account acct : this) { if (acct.account_number().compareTo(account.account_number()) == 0) { throw new DuplicateObjectException( String.format("Account %s exists, duplicates not allowed.", acct.account_number())); } } } catch (DuplicateObjectException e) { - return null; + return false; } - account_list.add(account); - return account; - } - - // save account in edit status - public static void save(final Account acct) throws IOException { - Account.Write(acct); + return super.add(account); } // save accounts in edit status - public static void save(final AccountList account_list, final Account acct) throws Exception { - for (Account account : account_list) { - AccountList.save(account); + public void save(final Account acct) throws IOException { + if( acct == null ){ + return; } + Account.Write(acct); } // ** Find account return null not-existing. */ - public static Account retrieveAccount(final AccountList account_list, String account_number) { - for (Account acct : account_list) { - if (acct.account_number() == account_number) { + public final Account find(String account_number) { + for (Account acct : this) { + if (acct.account_number().compareTo(account_number) == 0) { return acct; } } diff --git a/src/java/lodge/reservationsystem/AccountReservationList.java b/src/java/lodge/reservationsystem/AccountReservationList.java index 1579163..761d91e 100644 --- a/src/java/lodge/reservationsystem/AccountReservationList.java +++ b/src/java/lodge/reservationsystem/AccountReservationList.java @@ -11,17 +11,17 @@ public class AccountReservationList extends ArrayList { public boolean add(final Reservation reservation) { boolean result = true; try { - result = reservation.checkValid(); + result = reservation.checkValid(); } catch (Exception e) { return false; - } - for(Reservation rsrv: this){ + } + for (Reservation rsrv : this) { result = reservation.getReservation_number().compareTo(rsrv.getReservation_number()) == 0; - if(result){ + if (result) { return false; } } - reservation.reservation_number = AccountReservationList.reservationSerial(reservation); + reservation.setReservation_number(AccountReservationList.reservationSerial(reservation)); return super.add(reservation); } @@ -30,16 +30,36 @@ public class AccountReservationList extends ArrayList { StringBuilder sb = new StringBuilder(); sb.append("\"reservation_list\":["); - for(int ix=0; ix < this.size() ; ix++){ - String name = this.get(ix).ReservationType() + ""; - - if( ( this.size() - 1 ) == ix ){ - sb.append(String.format("{\"%s\":{\"reservation_number\":\"%s\"}}", name, this.get(ix).getReservation_number())); - }else{ - sb.append(String.format("{\"%s\":{\"reservation_number\":\"%s\"}},", name, this.get(ix).getReservation_number())); + for (int ix = 0; ix < this.size(); ix++) { + String name = this.get(ix).ReservationType() + ""; + + if ((this.size() - 1) == ix) { + sb.append(String.format("{\"%s\":{\"reservation_number\":\"%s\"}}", name, + this.get(ix).getReservation_number())); + } else { + sb.append(String.format("{\"%s\":{\"reservation_number\":\"%s\"}},", name, + this.get(ix).getReservation_number())); } } sb.append("]"); return sb.toString(); } + + public void update(AccountReservationList incoming_reservation_list) { + for (Reservation rsrv : incoming_reservation_list) { + Reservation onListRsrv = find(rsrv.getReservation_number()); + if( onListRsrv != null ){ + onListRsrv.update(rsrv); + } + } + } + + public Reservation find(String reservation_number) { + for(Reservation rsrv: this){ + if( rsrv.getReservation_number().compareTo(reservation_number) == 0 ){ + return rsrv; + } + } + return null; + } } diff --git a/src/java/lodge/reservationsystem/CabinReservation.java b/src/java/lodge/reservationsystem/CabinReservation.java index 88f3f81..b3b97f7 100644 --- a/src/java/lodge/reservationsystem/CabinReservation.java +++ b/src/java/lodge/reservationsystem/CabinReservation.java @@ -23,6 +23,7 @@ public final class CabinReservation extends Reservation{ return "CabinReservation"; } + @Override public boolean checkValid() throws IllegalArgumentException { boolean result = false; if (physical_address == null) { @@ -37,6 +38,7 @@ public final class CabinReservation extends Reservation{ //calculate and return the reservation's price // Cabin price plus additional fee of $20 for full kitchen and $5 for each additional bathroom + @Override public float calculatePrice() { ZonedDateTime enddt = reservation_end_date.truncatedTo(ChronoUnit.DAYS); ZonedDateTime startdt = reservation_start_date.truncatedTo(ChronoUnit.DAYS); diff --git a/src/java/lodge/reservationsystem/DataRepository.java b/src/java/lodge/reservationsystem/DataRepository.java index 4055fc8..ec64698 100644 --- a/src/java/lodge/reservationsystem/DataRepository.java +++ b/src/java/lodge/reservationsystem/DataRepository.java @@ -63,7 +63,7 @@ final class DataRepository { } }); } - + public final static Account LoadAccount(final Path file) throws IOException { /** @TODO finish loading Account */ try (BufferedReader in = new BufferedReader(new FileReader(file.toFile(), StandardCharsets.UTF_8)); @@ -150,103 +150,103 @@ final class DataRepository { try (JsonReader jsonReader = new JsonReader(in)) { jsonReader.beginObject(); Reservation rsrv = null; - while (jsonReader.hasNext()) { - String name = jsonReader.nextName(); - switch (name) { - case "HotelReservation": - jsonReader.beginObject(); - rsrv = new HotelReservation(); - break; - case "HouseReservation": - jsonReader.beginObject(); - rsrv = new HouseReservation(); - break; - case "CabinReservation": - jsonReader.beginObject(); - rsrv = new CabinReservation(); - break; - case "physical_address": - jsonReader.beginObject(); - jsonReader.nextName(); - jsonReader.beginObject(); - jsonReader.nextName(); - Address adP = new Address(); - adP.setStreet(jsonReader.nextString()); - jsonReader.nextName(); - adP.setCity(jsonReader.nextString()); - jsonReader.nextName(); - adP.setState(jsonReader.nextString()); - jsonReader.nextName(); - adP.setZip(jsonReader.nextString()); - jsonReader.endObject(); - jsonReader.endObject(); - rsrv.setPhysical_address(adP); - break; - case "mailing_address": - jsonReader.beginObject(); - jsonReader.nextName(); - jsonReader.beginObject(); - jsonReader.nextName(); - Address adM = new Address(); - adM.setStreet(jsonReader.nextString()); - jsonReader.nextName(); - adM.setCity(jsonReader.nextString()); - jsonReader.nextName(); - adM.setState(jsonReader.nextString()); - jsonReader.nextName(); - adM.setZip(jsonReader.nextString()); - jsonReader.endObject(); - jsonReader.endObject(); - rsrv.setMailing_address(adM); - break; - case "reservation_type": - jsonReader.nextString(); - break; - case "reservation_number": - rsrv.setReservation_number(jsonReader.nextString()); - break; - case "reservation_status": - rsrv.setReservation_status(ReservationStatusEnum.valueOf(jsonReader.nextString())); - break; - case "kitchen": - rsrv.setKitchen(KitchenEnum.valueOf(jsonReader.nextString())); - break; - case "numberOfBeds": - rsrv.setNumberOfBeds(Integer.valueOf(jsonReader.nextString())); - break; - case "numberOfBedRooms": - rsrv.setNumberOfBedRooms(Integer.valueOf(jsonReader.nextString())); - break; - case "numberOfBathRooms": - rsrv.setNumberOfBathRooms(Integer.valueOf(jsonReader.nextString())); - break; - case "numberOfFloors": - rsrv.setNumberOfFloors(Integer.valueOf(jsonReader.nextString())); - break; - case "squareFeet": - rsrv.setSquareFeet(Integer.valueOf(jsonReader.nextString())); - break; - case "price": - rsrv.setPrice(Float.valueOf(jsonReader.nextString())); - break; - case "reservation_start_date": - rsrv.setReservation_start_date(ZonedDateTime.parse(jsonReader.nextString())); - break; - case "reservation_end_date": - rsrv.setReservation_end_date(ZonedDateTime.parse(jsonReader.nextString())); - break; - - default: - System.out.println(name); - } - } - try { + while (jsonReader.hasNext()) { + String name = jsonReader.nextName(); + switch (name) { + case "HotelReservation": + jsonReader.beginObject(); + rsrv = new HotelReservation(); + break; + case "HouseReservation": + jsonReader.beginObject(); + rsrv = new HouseReservation(); + break; + case "CabinReservation": + jsonReader.beginObject(); + rsrv = new CabinReservation(); + break; + case "physical_address": + jsonReader.beginObject(); + jsonReader.nextName(); + jsonReader.beginObject(); + jsonReader.nextName(); + Address adP = new Address(); + adP.setStreet(jsonReader.nextString()); + jsonReader.nextName(); + adP.setCity(jsonReader.nextString()); + jsonReader.nextName(); + adP.setState(jsonReader.nextString()); + jsonReader.nextName(); + adP.setZip(jsonReader.nextString()); + jsonReader.endObject(); + jsonReader.endObject(); + rsrv.setPhysical_address(adP); + break; + case "mailing_address": + jsonReader.beginObject(); + jsonReader.nextName(); + jsonReader.beginObject(); + jsonReader.nextName(); + Address adM = new Address(); + adM.setStreet(jsonReader.nextString()); + jsonReader.nextName(); + adM.setCity(jsonReader.nextString()); + jsonReader.nextName(); + adM.setState(jsonReader.nextString()); + jsonReader.nextName(); + adM.setZip(jsonReader.nextString()); + jsonReader.endObject(); + jsonReader.endObject(); + rsrv.setMailing_address(adM); + break; + case "reservation_type": + jsonReader.nextString(); + break; + case "reservation_number": + rsrv.setReservation_number(jsonReader.nextString()); + break; + case "reservation_status": + rsrv.setReservation_status(ReservationStatusEnum.valueOf(jsonReader.nextString())); + break; + case "kitchen": + rsrv.setKitchen(KitchenEnum.valueOf(jsonReader.nextString())); + break; + case "numberOfBeds": + rsrv.setNumberOfBeds(Integer.valueOf(jsonReader.nextString())); + break; + case "numberOfBedRooms": + rsrv.setNumberOfBedRooms(Integer.valueOf(jsonReader.nextString())); + break; + case "numberOfBathRooms": + rsrv.setNumberOfBathRooms(Integer.valueOf(jsonReader.nextString())); + break; + case "numberOfFloors": + rsrv.setNumberOfFloors(Integer.valueOf(jsonReader.nextString())); + break; + case "squareFeet": + rsrv.setSquareFeet(Integer.valueOf(jsonReader.nextString())); + break; + case "price": + rsrv.setPrice(Float.valueOf(jsonReader.nextString())); + break; + case "reservation_start_date": + rsrv.setReservation_start_date(ZonedDateTime.parse(jsonReader.nextString())); + break; + case "reservation_end_date": + rsrv.setReservation_end_date(ZonedDateTime.parse(jsonReader.nextString())); + break; + + default: + System.out.println(name); + } + } + if (rsrv.checkValid()) { reservation_list.add(rsrv); } } catch (Exception e) { - e.printStackTrace(); + System.out.println(e.toString()); } } diff --git a/src/java/lodge/reservationsystem/HotelReservation.java b/src/java/lodge/reservationsystem/HotelReservation.java index c28cf2e..c6b6c3e 100644 --- a/src/java/lodge/reservationsystem/HotelReservation.java +++ b/src/java/lodge/reservationsystem/HotelReservation.java @@ -22,11 +22,13 @@ public final class HotelReservation extends Reservation { this.physical_address.setZip(physical_address.getZip()); } + @Override public final String ReservationType() { type = 'H'; return "HotelReservation"; } + @Override public boolean checkValid() throws IllegalArgumentException { boolean result = false; if (numberOfBathRooms != 1) { diff --git a/src/java/lodge/reservationsystem/HouseReservation.java b/src/java/lodge/reservationsystem/HouseReservation.java index 12867b2..dce9265 100644 --- a/src/java/lodge/reservationsystem/HouseReservation.java +++ b/src/java/lodge/reservationsystem/HouseReservation.java @@ -46,6 +46,7 @@ public final class HouseReservation extends Reservation { // calculate and return the reservation's price // Hotel price plus additional flat fee of $50 plus $10 for kitchenette + @Override public float calculatePrice() { ZonedDateTime enddt = reservation_end_date.truncatedTo(ChronoUnit.DAYS); ZonedDateTime startdt = reservation_start_date.truncatedTo(ChronoUnit.DAYS); diff --git a/src/java/lodge/reservationsystem/Reservation.java b/src/java/lodge/reservationsystem/Reservation.java index fdd1c3c..8bbbae6 100644 --- a/src/java/lodge/reservationsystem/Reservation.java +++ b/src/java/lodge/reservationsystem/Reservation.java @@ -221,7 +221,7 @@ public abstract class Reservation { } reservation.setReservation_status(newStatus); } catch (IllegalOperationException e) { - e.printStackTrace(); + System.out.println(e.toString()); } } @@ -233,5 +233,9 @@ public abstract class Reservation { public abstract float calculatePrice(); - public abstract boolean checkValid() throws Exception; + public abstract boolean checkValid() throws ReservationException; + + public void update(Reservation rsrv) { + + } } diff --git a/src/resources/acc-A1450981765.json b/src/resources/acc-A1450981765.json old mode 100644 new mode 100755 diff --git a/src/resources/rsv-R0123077641.json b/src/resources/rsv-R0123077641.json old mode 100644 new mode 100755 diff --git a/src/resources/rsv-R0499811708.json b/src/resources/rsv-R0499811708.json old mode 100644 new mode 100755 index e3889e1..d0f5f7e --- a/src/resources/rsv-R0499811708.json +++ b/src/resources/rsv-R0499811708.json @@ -1 +1 @@ -{ "HouseReservation":{"reservation_type": "HouseReservation","reservation_number": "R0499811708","reservation_status": "Draft","reservation_start_date": "2025-09-05T10:00Z[UTC]","reservation_end_date": "2025-11-30T22:00Z[UTC]","physical_address": { "Address":{"street": "3000 Osage ave","city": "GreenBelt","state": "MD","zip": "20740"}},"mailing_address": { "Address":{"street": "40012 College ave","city": "College Park","state": "MD","zip": "20740"}},"kitchen": "FullKitchen","numberOfBeds": "4","numberOfBedRooms": "3","numberOfBathRooms": "1","numberOfFloors": "3","squareFeet": "1400","price": "120.0"}} \ No newline at end of file +{ "HouseReservation":{"reservation_type": "HouseReservation","reservation_number": "R0499811708","reservation_status": "Completed","reservation_start_date": "2025-09-05T10:00Z[UTC]","reservation_end_date": "2025-11-30T22:00Z[UTC]","physical_address": { "Address":{"street": "3000 Osage ave","city": "GreenBelt","state": "MD","zip": "20740"}},"mailing_address": { "Address":{"street": "40012 College ave","city": "College Park","state": "MD","zip": "20740"}},"kitchen": "FullKitchen","numberOfBeds": "4","numberOfBedRooms": "3","numberOfBathRooms": "1","numberOfFloors": "3","squareFeet": "1400","price": "11475.0"}} \ No newline at end of file diff --git a/src/resources/rsv-R2042828431.json b/src/resources/rsv-R2042828431.json old mode 100644 new mode 100755 index bcbe1e9..5186fa7 --- a/src/resources/rsv-R2042828431.json +++ b/src/resources/rsv-R2042828431.json @@ -1 +1 @@ -{ "CabinReservation":{"reservation_type": "CabinReservation","reservation_number": "R2042828431","reservation_status": "Completed","reservation_start_date": "2025-09-05T10:00Z[UTC]","reservation_end_date": "2025-11-30T22:00Z[UTC]","physical_address": { "Address":{"street": "30 cabin ave","city": "Carnelian","state": "CA","zip": "96140"}},"mailing_address": { "Address":{"street": "40 cabin ave","city": "Carnelian Bay","state": "CA","zip": "96140"}},"kitchen": "Kitchenette","numberOfBeds": "4","numberOfBedRooms": "3","numberOfBathRooms": "1","numberOfFloors": "2","squareFeet": "806","price": "10200.0"}} \ No newline at end of file +{ "CabinReservation":{"reservation_type": "CabinReservation","reservation_number": "R2042828431","reservation_status": "Draft","reservation_start_date": "2025-09-05T10:00Z[UTC]","reservation_end_date": "2025-11-30T22:00Z[UTC]","physical_address": { "Address":{"street": "30 cabin ave","city": "Carnelian","state": "CA","zip": "96140"}},"mailing_address": { "Address":{"street": "40 cabin ave","city": "Carnelian Bay","state": "CA","zip": "96140"}},"kitchen": "Kitchenette","numberOfBeds": "4","numberOfBedRooms": "3","numberOfBathRooms": "1","numberOfFloors": "2","squareFeet": "806","price": "120.0"}} \ No newline at end of file