finish deserialization.

This commit is contained in:
2025-09-06 19:21:01 -04:00
parent 6a0001d43b
commit a3bd96e749
13 changed files with 204 additions and 32 deletions

View File

@@ -23,7 +23,7 @@ public final class TestReservations {
// 2. Retrieve a loaded account object that matches a specific account number // 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 // 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 // already exists on add action with the same account number, it is considered

View File

@@ -25,11 +25,19 @@ public final class AccomodationManager {
// walk directories // walk directories
Path rootDir = Paths.get(DataRepository.getPath()); Path rootDir = Paths.get(DataRepository.getPath());
DataRepository.WalkFileSystemTree(this, rootDir); DataRepository.WalkFileSystemTree(this, rootDir);
System.out.println(String.format("Accounts Loaded %d", account_list.size()));
} }
// Load / Deserialize Account // Load / Deserialize Account
void load(Path file) throws Exception { void load(Path file) throws Exception {
AddAccount( DataRepository.LoadAccount(file) ); 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() { public final AccountList retrieveLoadedAccounts() {

View File

@@ -9,6 +9,12 @@ public class AccountReservationList extends ArrayList<Reservation> {
} }
public boolean add(final Reservation reservation) { public boolean add(final Reservation reservation) {
boolean result = true;
try {
result = reservation.checkValid();
} catch (Exception e) {
return false;
}
for(Reservation rsrv: this){ for(Reservation rsrv: this){
boolean result = reservation.getReservation_number().compareTo(rsrv.getReservation_number()) == 0; boolean result = reservation.getReservation_number().compareTo(rsrv.getReservation_number()) == 0;
if(result){ if(result){

View File

@@ -1,6 +1,6 @@
package lodge.reservationsystem; package lodge.reservationsystem;
public final class Address { public final class Address{
String street; String street;
String city; String city;
@@ -103,5 +103,4 @@ public final class Address {
sb.append("}}"); sb.append("}}");
return sb.toString(); return sb.toString();
} }
} }

View File

@@ -5,10 +5,17 @@ import java.time.temporal.ChronoUnit;
public final class CabinReservation extends Reservation{ public final class CabinReservation extends Reservation{
public CabinReservation(Address physical_address) { CabinReservation(){
super();
}
public CabinReservation(final Address physical_address) {
numberOfBeds = 1; numberOfBeds = 1;
kitchen = KitchenEnum.Kitchenette; 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() { public final String ReservationType() {
@@ -16,8 +23,14 @@ public final class CabinReservation extends Reservation{
return "CabinReservation"; return "CabinReservation";
} }
public boolean checkValid() throws IllegalArgumentException { public boolean checkValid() throws IllegalArgumentException {
boolean result = false; 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; result = true;
return result; return result;
} }

View File

@@ -3,12 +3,16 @@ package lodge.reservationsystem;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult; import java.nio.file.FileVisitResult;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor; import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.time.ZonedDateTime;
import com.google.gson.stream.JsonReader; 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 * @param file
* @throws IOException * @throws IOException
@@ -116,7 +124,7 @@ final class DataRepository {
jsonReader.endObject(); jsonReader.endObject();
break; break;
case "reservation_list": case "reservation_list":
loadReservation(jsonReader, ac); loadReservationRefList(jsonReader, ac);
break; break;
default: default:
System.out.println(name); System.out.println(name);
@@ -125,23 +133,136 @@ final class DataRepository {
jsonReader.close(); jsonReader.close();
return ac.account_number.length() > 8 ? ac : null; return ac.account_number.length() > 8 ? ac : null;
} }
return ac;
} }
static void loadReservation(final JsonReader rdr, final Account ac) throws IOException { static void loadReservationRefList(JsonReader rdr, Account ac) throws IOException {
final AccountReservationList reservation_list = new AccountReservationList();
rdr.beginArray(); rdr.beginArray();
while (rdr.hasNext()) { while (rdr.hasNext()) {
rdr.beginObject(); rdr.beginObject();
String name = rdr.nextName(); String reservationType = rdr.nextName();
final Reservation rsrv = new HouseReservation(new Address());
rdr.beginObject(); rdr.beginObject();
name = rdr.nextName(); rdr.nextName();
rsrv.reservation_number = rdr.nextString(); String reservationNumber = rdr.nextString();
loadReservation(ac.reservation_list, reservationType, reservationNumber);
rdr.endObject(); rdr.endObject();
rdr.endObject(); rdr.endObject();
reservation_list.add(rsrv);
} }
rdr.endArray(); 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();
}
}
}
}
} }

View File

@@ -5,12 +5,21 @@ import java.time.temporal.ChronoUnit;
public final class HotelReservation extends Reservation { public final class HotelReservation extends Reservation {
public HotelReservation(Address physical_address) { HotelReservation() {
super();
numberOfBeds = 2;
}
public HotelReservation(final Address physical_address) {
numberOfBeds = 2; numberOfBeds = 2;
numberOfBedRooms = 1; numberOfBedRooms = 1;
numberOfBathRooms = 1; numberOfBathRooms = 1;
kitchen = KitchenEnum.None; 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() { public final String ReservationType() {
@@ -29,6 +38,12 @@ public final class HotelReservation extends Reservation {
if (numberOfBeds != 2) { if (numberOfBeds != 2) {
throw new IllegalArgumentException("not valid, Beds"); 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; result = true;
return result; return result;
} }
@@ -38,11 +53,11 @@ public final class HotelReservation extends Reservation {
public float calculatePrice() { public float calculatePrice() {
ZonedDateTime enddt = reservation_end_date.truncatedTo(ChronoUnit.DAYS); ZonedDateTime enddt = reservation_end_date.truncatedTo(ChronoUnit.DAYS);
ZonedDateTime startdt = reservation_start_date.truncatedTo(ChronoUnit.DAYS); ZonedDateTime startdt = reservation_start_date.truncatedTo(ChronoUnit.DAYS);
long days = ChronoUnit.DAYS.between( startdt ,enddt); long days = ChronoUnit.DAYS.between(startdt, enddt);
days = ( days < 2 ) ? 1: days - 1; days = (days < 2) ? 1 : days - 1;
price = (squareFeet > 900.0f) ? 120.0f + 15.0f : 120.0f; price = (squareFeet > 900.0f) ? 120.0f + 15.0f : 120.0f;
if ( kitchen == KitchenEnum.FullKitchen ){ if (kitchen == KitchenEnum.FullKitchen) {
price = price + 10.0f ; price = price + 10.0f;
} }
price = price * days; price = price * days;
price = price + 50.0f; price = price + 50.0f;

View File

@@ -5,13 +5,21 @@ import java.time.temporal.ChronoUnit;
public final class HouseReservation extends Reservation { public final class HouseReservation extends Reservation {
public HouseReservation(Address physical_address) { HouseReservation(){
super();
}
public HouseReservation(final Address physical_address) {
numberOfBeds = 2; numberOfBeds = 2;
numberOfBedRooms = 1; numberOfBedRooms = 1;
numberOfBathRooms = 1; numberOfBathRooms = 1;
numberOfFloors = 1; numberOfFloors = 1;
kitchen = KitchenEnum.FullKitchen; 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() { public final String ReservationType() {
@@ -21,6 +29,12 @@ public final class HouseReservation extends Reservation {
public boolean checkValid() throws IllegalArgumentException { public boolean checkValid() throws IllegalArgumentException {
boolean result = false; 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; result = true;
return result; return result;
} }

View File

@@ -28,7 +28,7 @@ public abstract class Reservation {
Float price; Float price;
protected Reservation() { Reservation() {
numberOfBeds = 1; numberOfBeds = 1;
numberOfBedRooms = 1; numberOfBedRooms = 1;
numberOfBathRooms = 1; numberOfBathRooms = 1;
@@ -179,9 +179,9 @@ public abstract class Reservation {
sb.append("\"reservation_number\": \"" + reservation_number + "\","); sb.append("\"reservation_number\": \"" + reservation_number + "\",");
sb.append("\"reservation_status\": \"" + reservation_status + "\","); sb.append("\"reservation_status\": \"" + reservation_status + "\",");
sb.append("\"reservation_start_date\": \"" + reservation_start_date + "\","); sb.append("\"reservation_start_date\": \"" + reservation_start_date + "\",");
sb.append("\"reservation_start_date\": \"" + reservation_end_date + "\","); sb.append("\"reservation_end_date\": \"" + reservation_end_date + "\",");
sb.append("\"physical_address\": \"" + physical_address + "\","); sb.append("\"physical_address\": " + physical_address + ",");
sb.append("\"mailing_address\": \"" + mailing_address + "\","); sb.append("\"mailing_address\": " + mailing_address + ",");
sb.append("\"kitchen\": \"" + kitchen + "\","); sb.append("\"kitchen\": \"" + kitchen + "\",");
sb.append("\"numberOfBeds\": \"" + numberOfBeds + "\","); sb.append("\"numberOfBeds\": \"" + numberOfBeds + "\",");
sb.append("\"numberOfBedRooms\": \"" + numberOfBedRooms + "\","); sb.append("\"numberOfBedRooms\": \"" + numberOfBedRooms + "\",");

View File

@@ -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"}}]}}

View File

@@ -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"}}

View File

@@ -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"}}

View File

@@ -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"}}