finish deserialization.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -9,6 +9,12 @@ public class AccountReservationList extends ArrayList<Reservation> {
|
||||
}
|
||||
|
||||
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){
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 + "\",");
|
||||
|
||||
@@ -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"}}]}}
|
||||
@@ -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"}}
|
||||
@@ -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"}}
|
||||
@@ -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"}}
|
||||
Reference in New Issue
Block a user