updates cleanup.

This commit is contained in:
2025-09-10 15:44:29 -04:00
parent 10035bc4c6
commit cacb97221c
13 changed files with 181 additions and 144 deletions

View File

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

View File

@@ -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());
}
}

View File

@@ -6,7 +6,7 @@ import java.util.ArrayList;
public class AccountList extends ArrayList<Account> {
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<Account> {
}
// ** 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;
}
}

View File

@@ -11,17 +11,17 @@ public class AccountReservationList extends ArrayList<Reservation> {
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<Reservation> {
StringBuilder sb = new StringBuilder();
sb.append("\"reservation_list\":[");
for(int ix=0; ix < this.size() ; ix++){
String name = this.get(ix).ReservationType() + "";
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()));
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;
}
}

View File

@@ -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);

View File

@@ -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());
}
}

View File

@@ -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) {

View File

@@ -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);

View File

@@ -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) {
}
}

0
src/resources/acc-A1450981765.json Normal file → Executable file
View File

0
src/resources/rsv-R0123077641.json Normal file → Executable file
View File

2
src/resources/rsv-R0499811708.json Normal file → Executable file
View File

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

2
src/resources/rsv-R2042828431.json Normal file → Executable file
View File

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