From a3bd96e74982b93de539d03526acda51bf002183 Mon Sep 17 00:00:00 2001 From: Sherwin Price Date: Sat, 6 Sep 2025 19:21:01 -0400 Subject: [PATCH] finish deserialization. --- src/java/lodge/TestReservations.java | 2 +- .../AccomodationManager.java | 8 + .../AccountReservationList.java | 6 + src/java/lodge/reservationsystem/Address.java | 3 +- .../reservationsystem/CabinReservation.java | 19 ++- .../reservationsystem/DataRepository.java | 139 ++++++++++++++++-- .../reservationsystem/HotelReservation.java | 29 +++- .../reservationsystem/HouseReservation.java | 18 ++- .../lodge/reservationsystem/Reservation.java | 8 +- src/resources/acc-A1450981765.json | 1 - src/resources/rsv-R0123077641.json | 1 - src/resources/rsv-R0499811708.json | 1 - src/resources/rsv-R2042828431.json | 1 - 13 files changed, 204 insertions(+), 32 deletions(-) delete mode 100644 src/resources/acc-A1450981765.json delete mode 100644 src/resources/rsv-R0123077641.json delete mode 100644 src/resources/rsv-R0499811708.json delete mode 100644 src/resources/rsv-R2042828431.json diff --git a/src/java/lodge/TestReservations.java b/src/java/lodge/TestReservations.java index 7fd49c2..064a3dd 100644 --- a/src/java/lodge/TestReservations.java +++ b/src/java/lodge/TestReservations.java @@ -23,7 +23,7 @@ public final class TestReservations { // 2. Retrieve a loaded account object that matches a specific account number - mgr.retrieveAccount("A######"); + mgr.retrieveAccount("A1450981765"); // 3. Add new account object to the list managed by Manager (if account object // already exists on add action with the same account number, it is considered diff --git a/src/java/lodge/reservationsystem/AccomodationManager.java b/src/java/lodge/reservationsystem/AccomodationManager.java index b1bed6f..3c9f173 100644 --- a/src/java/lodge/reservationsystem/AccomodationManager.java +++ b/src/java/lodge/reservationsystem/AccomodationManager.java @@ -25,11 +25,19 @@ 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())); } // Load / Deserialize Account void load(Path file) throws Exception { AddAccount( DataRepository.LoadAccount(file) ); + Account account = DataRepository.LoadAccount(file); + if( account == null ) + { + System.out.println( String.format("%s", file.toString() )); + }else{ + account_list.add( account ); + } } public final AccountList retrieveLoadedAccounts() { diff --git a/src/java/lodge/reservationsystem/AccountReservationList.java b/src/java/lodge/reservationsystem/AccountReservationList.java index e6ef514..6a9aef0 100644 --- a/src/java/lodge/reservationsystem/AccountReservationList.java +++ b/src/java/lodge/reservationsystem/AccountReservationList.java @@ -9,6 +9,12 @@ public class AccountReservationList extends ArrayList { } public boolean add(final Reservation reservation) { + boolean result = true; + try { + result = reservation.checkValid(); + } catch (Exception e) { + return false; + } for(Reservation rsrv: this){ boolean result = reservation.getReservation_number().compareTo(rsrv.getReservation_number()) == 0; if(result){ diff --git a/src/java/lodge/reservationsystem/Address.java b/src/java/lodge/reservationsystem/Address.java index 1f7af37..aa26347 100644 --- a/src/java/lodge/reservationsystem/Address.java +++ b/src/java/lodge/reservationsystem/Address.java @@ -1,6 +1,6 @@ package lodge.reservationsystem; -public final class Address { +public final class Address{ String street; String city; @@ -103,5 +103,4 @@ public final class Address { sb.append("}}"); return sb.toString(); } - } \ No newline at end of file diff --git a/src/java/lodge/reservationsystem/CabinReservation.java b/src/java/lodge/reservationsystem/CabinReservation.java index a83f9f6..88f3f81 100644 --- a/src/java/lodge/reservationsystem/CabinReservation.java +++ b/src/java/lodge/reservationsystem/CabinReservation.java @@ -5,10 +5,17 @@ import java.time.temporal.ChronoUnit; public final class CabinReservation extends Reservation{ - public CabinReservation(Address physical_address) { + CabinReservation(){ + super(); + } + public CabinReservation(final Address physical_address) { numberOfBeds = 1; kitchen = KitchenEnum.Kitchenette; - this.physical_address = physical_address; + this.physical_address = new Address(); + this.physical_address.setStreet(physical_address.getStreet()); + this.physical_address.setCity(physical_address.getCity()); + this.physical_address.setState(physical_address.getState()); + this.physical_address.setZip(physical_address.getZip()); } public final String ReservationType() { @@ -16,8 +23,14 @@ public final class CabinReservation extends Reservation{ return "CabinReservation"; } - public boolean checkValid() throws IllegalArgumentException { + public boolean checkValid() throws IllegalArgumentException { boolean result = false; + if (physical_address == null) { + throw new IllegalArgumentException("not valid, physical_address"); + } + if (mailing_address == null) { + throw new IllegalArgumentException("not valid, mailing_address"); + } result = true; return result; } diff --git a/src/java/lodge/reservationsystem/DataRepository.java b/src/java/lodge/reservationsystem/DataRepository.java index ba5b978..1ec61c3 100644 --- a/src/java/lodge/reservationsystem/DataRepository.java +++ b/src/java/lodge/reservationsystem/DataRepository.java @@ -3,12 +3,16 @@ package lodge.reservationsystem; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; +import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; +import java.time.ZonedDateTime; + import com.google.gson.stream.JsonReader; @@ -62,6 +66,10 @@ final class DataRepository { }); } + public static Account LoadAccount(Path file) throws IOException { + Account ac = null; + try (BufferedReader in = new BufferedReader(new FileReader(file.toFile(), StandardCharsets.UTF_8))) { + JsonReader jsonReader = new JsonReader(in); /** * @param file * @throws IOException @@ -116,7 +124,7 @@ final class DataRepository { jsonReader.endObject(); break; case "reservation_list": - loadReservation(jsonReader, ac); + loadReservationRefList(jsonReader, ac); break; default: System.out.println(name); @@ -125,23 +133,136 @@ final class DataRepository { jsonReader.close(); return ac.account_number.length() > 8 ? ac : null; } - + return ac; } - static void loadReservation(final JsonReader rdr, final Account ac) throws IOException { - final AccountReservationList reservation_list = new AccountReservationList(); + static void loadReservationRefList(JsonReader rdr, Account ac) throws IOException { rdr.beginArray(); while (rdr.hasNext()) { rdr.beginObject(); - String name = rdr.nextName(); - final Reservation rsrv = new HouseReservation(new Address()); + String reservationType = rdr.nextName(); rdr.beginObject(); - name = rdr.nextName(); - rsrv.reservation_number = rdr.nextString(); + rdr.nextName(); + String reservationNumber = rdr.nextString(); + loadReservation(ac.reservation_list, reservationType, reservationNumber); rdr.endObject(); rdr.endObject(); - reservation_list.add(rsrv); } rdr.endArray(); } + + private static void loadReservation(AccountReservationList reservation_list, String reservationType, + String reservationNumber) throws IOException { + String filename = String.format("rsv-%s.json", reservationNumber); + Path basePath = Paths.get(getPath()); + Path resolvedPath = basePath.resolve(filename); + + try (BufferedReader in = new BufferedReader(new FileReader(resolvedPath.toFile(), StandardCharsets.UTF_8))) { + 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 { + if (rsrv.checkValid()) { + reservation_list.add(rsrv); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + + } + + } } diff --git a/src/java/lodge/reservationsystem/HotelReservation.java b/src/java/lodge/reservationsystem/HotelReservation.java index 74bf22b..c28cf2e 100644 --- a/src/java/lodge/reservationsystem/HotelReservation.java +++ b/src/java/lodge/reservationsystem/HotelReservation.java @@ -5,12 +5,21 @@ import java.time.temporal.ChronoUnit; public final class HotelReservation extends Reservation { - public HotelReservation(Address physical_address) { + HotelReservation() { + super(); + numberOfBeds = 2; + } + + public HotelReservation(final Address physical_address) { numberOfBeds = 2; numberOfBedRooms = 1; numberOfBathRooms = 1; kitchen = KitchenEnum.None; - this.physical_address = physical_address; + this.physical_address = new Address(); + this.physical_address.setStreet(physical_address.getStreet()); + this.physical_address.setCity(physical_address.getCity()); + this.physical_address.setState(physical_address.getState()); + this.physical_address.setZip(physical_address.getZip()); } public final String ReservationType() { @@ -29,6 +38,12 @@ public final class HotelReservation extends Reservation { if (numberOfBeds != 2) { throw new IllegalArgumentException("not valid, Beds"); } + if (physical_address == null) { + throw new IllegalArgumentException("not valid, physical_address"); + } + if (mailing_address == null) { + throw new IllegalArgumentException("not valid, mailing_address"); + } result = true; return result; } @@ -38,11 +53,11 @@ public final class HotelReservation extends Reservation { public float calculatePrice() { ZonedDateTime enddt = reservation_end_date.truncatedTo(ChronoUnit.DAYS); ZonedDateTime startdt = reservation_start_date.truncatedTo(ChronoUnit.DAYS); - long days = ChronoUnit.DAYS.between( startdt ,enddt); - days = ( days < 2 ) ? 1: days - 1; - price = (squareFeet > 900.0f) ? 120.0f + 15.0f : 120.0f; - if ( kitchen == KitchenEnum.FullKitchen ){ - price = price + 10.0f ; + long days = ChronoUnit.DAYS.between(startdt, enddt); + days = (days < 2) ? 1 : days - 1; + price = (squareFeet > 900.0f) ? 120.0f + 15.0f : 120.0f; + if (kitchen == KitchenEnum.FullKitchen) { + price = price + 10.0f; } price = price * days; price = price + 50.0f; diff --git a/src/java/lodge/reservationsystem/HouseReservation.java b/src/java/lodge/reservationsystem/HouseReservation.java index fb2a213..12867b2 100644 --- a/src/java/lodge/reservationsystem/HouseReservation.java +++ b/src/java/lodge/reservationsystem/HouseReservation.java @@ -5,13 +5,21 @@ import java.time.temporal.ChronoUnit; public final class HouseReservation extends Reservation { - public HouseReservation(Address physical_address) { + HouseReservation(){ + super(); + } + + public HouseReservation(final Address physical_address) { numberOfBeds = 2; numberOfBedRooms = 1; numberOfBathRooms = 1; numberOfFloors = 1; kitchen = KitchenEnum.FullKitchen; - this.physical_address = physical_address; + this.physical_address = new Address(); + this.physical_address.setStreet(physical_address.getStreet()); + this.physical_address.setCity(physical_address.getCity()); + this.physical_address.setState(physical_address.getState()); + this.physical_address.setZip(physical_address.getZip()); } public final String ReservationType() { @@ -21,6 +29,12 @@ public final class HouseReservation extends Reservation { public boolean checkValid() throws IllegalArgumentException { boolean result = false; + if (physical_address == null) { + throw new IllegalArgumentException("not valid, physical_address"); + } + if (mailing_address == null) { + throw new IllegalArgumentException("not valid, mailing_address"); + } result = true; return result; } diff --git a/src/java/lodge/reservationsystem/Reservation.java b/src/java/lodge/reservationsystem/Reservation.java index ffbc60b..fdd1c3c 100644 --- a/src/java/lodge/reservationsystem/Reservation.java +++ b/src/java/lodge/reservationsystem/Reservation.java @@ -28,7 +28,7 @@ public abstract class Reservation { Float price; - protected Reservation() { + Reservation() { numberOfBeds = 1; numberOfBedRooms = 1; numberOfBathRooms = 1; @@ -179,9 +179,9 @@ public abstract class Reservation { sb.append("\"reservation_number\": \"" + reservation_number + "\","); sb.append("\"reservation_status\": \"" + reservation_status + "\","); sb.append("\"reservation_start_date\": \"" + reservation_start_date + "\","); - sb.append("\"reservation_start_date\": \"" + reservation_end_date + "\","); - sb.append("\"physical_address\": \"" + physical_address + "\","); - sb.append("\"mailing_address\": \"" + mailing_address + "\","); + sb.append("\"reservation_end_date\": \"" + reservation_end_date + "\","); + sb.append("\"physical_address\": " + physical_address + ","); + sb.append("\"mailing_address\": " + mailing_address + ","); sb.append("\"kitchen\": \"" + kitchen + "\","); sb.append("\"numberOfBeds\": \"" + numberOfBeds + "\","); sb.append("\"numberOfBedRooms\": \"" + numberOfBedRooms + "\","); diff --git a/src/resources/acc-A1450981765.json b/src/resources/acc-A1450981765.json deleted file mode 100644 index 33b38e7..0000000 --- a/src/resources/acc-A1450981765.json +++ /dev/null @@ -1 +0,0 @@ -{ "Account":{"account_number": "A1450981765","phone_number": "701-456-7890","mailing_address": { "Address":{"street": "10 wilco ave","city": "wilco","state": "WY","zip": "82801"}},"email_address": { "EmailAddress":{"email": "wilco@wyommin.net"}},"reservation_list":[{"HotelReservation":{"reservation_number":"R0123077641"}},{"CabinReservation":{"reservation_number":"R2042828431"}},{"HouseReservation":{"reservation_number":"R0499811708"}}]}} \ No newline at end of file diff --git a/src/resources/rsv-R0123077641.json b/src/resources/rsv-R0123077641.json deleted file mode 100644 index fda783c..0000000 --- a/src/resources/rsv-R0123077641.json +++ /dev/null @@ -1 +0,0 @@ -{ "HotelReservation":{"reservation_type": "HotelReservation","reservation_number": "R0123077641","reservation_status": "Draft","reservation_start_date": "2025-07-05T10:00Z[UTC]","reservation_start_date": "2025-11-30T22:00Z[UTC]","physical_address": "{ "Address":{"street": "400 hotel ave","city": "Maryland City","state": "CA","zip": "20723"}}","mailing_address": "{ "Address":{"street": "400 hotel ave","city": "Maryland City","state": "MD","zip": "20723"}}","kitchen": "None","numberOfBeds": "2","numberOfBedRooms": "1","numberOfBathRooms": "1","numberOfFloors": "1","squareFeet": "450","price": "120.0"}} \ No newline at end of file diff --git a/src/resources/rsv-R0499811708.json b/src/resources/rsv-R0499811708.json deleted file mode 100644 index ff2d319..0000000 --- a/src/resources/rsv-R0499811708.json +++ /dev/null @@ -1 +0,0 @@ -{ "HouseReservation":{"reservation_type": "HouseReservation","reservation_number": "R0499811708","reservation_status": "Draft","reservation_start_date": "2025-09-05T10:00Z[UTC]","reservation_start_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 diff --git a/src/resources/rsv-R2042828431.json b/src/resources/rsv-R2042828431.json deleted file mode 100644 index 35accb8..0000000 --- a/src/resources/rsv-R2042828431.json +++ /dev/null @@ -1 +0,0 @@ -{ "CabinReservation":{"reservation_type": "CabinReservation","reservation_number": "R2042828431","reservation_status": "Draft","reservation_start_date": "2025-09-05T10:00Z[UTC]","reservation_start_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