update deserialization

This commit is contained in:
2025-09-04 22:34:26 -04:00
parent 99fe3e5283
commit 65d97f904e
14 changed files with 161 additions and 46 deletions

View File

@@ -7,7 +7,7 @@ public final class AccomodationManager {
private final AccountList account_list = new AccountList();
public void setDataStoreRoot(String home) {
public final void setDataStoreRoot(String home) {
DataRepository.setDataStoreRoot(home);
}

View File

@@ -5,6 +5,10 @@ import java.util.ArrayList;
public class AccountList extends ArrayList<Account> {
public AccountList(){
}
public static String accountSerial(String phone_number, Address mailing_address, EmailAddress email_address) {
return String.format("A%09d", Math.abs(java.util.Objects.hash(phone_number, mailing_address, email_address)));
}
@@ -18,7 +22,7 @@ public class AccountList extends ArrayList<Account> {
return A;
}
// ** Add account throw error if account number is pre-existing. */
public static Account addAccount(final AccountList account_list, final Account account) throws NullPointerException, AccountException {
public static Account addAccount(final AccountList account_list, final Account account) throws DuplicateObjectException, AccountException {
String acctNumber = "";
for(Account acct: account_list){
if( acct.account_number() == account.account_number() ){
@@ -27,7 +31,7 @@ public class AccountList extends ArrayList<Account> {
}
}
if (!acctNumber.isEmpty()) {
throw new AccountException("Account exists, duplicates not allowed.");
throw new DuplicateObjectException("Account exists, duplicates not allowed.");
} else {
account_list.add(account);
}

View File

@@ -28,6 +28,7 @@ public final class CabinReservation extends Reservation{
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 + 20.0f ;

View File

@@ -10,6 +10,7 @@ import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
final class DataRepository {
@@ -19,7 +20,7 @@ final class DataRepository {
private String directoryPath;
private static final DataRepository instance = new DataRepository();
protected static DataRepository getInstance() {
protected final static DataRepository getInstance() {
return instance;
}
@@ -31,7 +32,7 @@ final class DataRepository {
return getInstance().directoryPath;
}
public static void WalkFileSystemTree(final AccomodationManager manager, Path rootDir) throws IOException{
public static void WalkFileSystemTree(final AccomodationManager manager, Path rootDir) throws IOException {
Files.walkFileTree(rootDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
@@ -62,8 +63,9 @@ final class DataRepository {
});
}
public static void LoadAccount(Path file) throws IOException{
/** @TODO finish loading Account */
public static void LoadAccount(Path file) throws IOException {
/** @TODO finish loading Account */
final Gson gson = new Gson();
try (BufferedReader in = new BufferedReader(new FileReader(file.toFile(), StandardCharsets.UTF_8))) {
JsonReader jsonReader = new JsonReader(in);
jsonReader.beginObject();
@@ -113,33 +115,7 @@ final class DataRepository {
jsonReader.endObject();
break;
case "reservation_list":
jsonReader.beginArray();
jsonReader.beginObject();
break;
case "CabinReservation":
jsonReader.beginObject();
name = jsonReader.nextName();
CabinReservation cabin = new CabinReservation(ad);
cabin.setMailing_address(ad);
cabin.setPhysical_address(ad);
cabin.reservation_number = jsonReader.nextString();
break;
case "HouseReservation":
jsonReader.beginObject();
name = jsonReader.nextName();
HouseReservation house = new HouseReservation(ad);
house.setMailing_address(ad);
house.setPhysical_address(ad);
house.reservation_number = jsonReader.nextString();
break;
case "HotelReservation":
jsonReader.beginObject();
name = jsonReader.nextName();
HotelReservation hotel = new HotelReservation(ad);
hotel.setMailing_address(ad);
hotel.setPhysical_address(ad);
hotel.reservation_number = jsonReader.nextString();
loadReservation(jsonReader, ac);
break;
default:
System.out.println(name);
@@ -148,5 +124,22 @@ final class DataRepository {
jsonReader.close();
}
}
}
static void loadReservation(JsonReader rdr, Account ac) throws IOException {
AccountReservationList reservation_list = new AccountReservationList();
rdr.beginArray();
while (rdr.hasNext()) {
rdr.beginObject();
String name = rdr.nextName();
Reservation rsrv = new HouseReservation(new Address());
rdr.beginObject();
name = rdr.nextName();
rsrv.reservation_number = rdr.nextString();
rdr.endObject();
rdr.endObject();
reservation_list.add(rsrv);
}
rdr.endArray();
}
}

View File

@@ -0,0 +1,12 @@
package lodge.reservationsystem;
class DuplicateObjectException extends RuntimeException {
public DuplicateObjectException() {
super();
}
public DuplicateObjectException(String message) {
super(message);
}
}

View File

@@ -39,6 +39,7 @@ public final class HotelReservation extends Reservation {
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 ;

View File

@@ -1,5 +1,8 @@
package lodge.reservationsystem;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
public final class HouseReservation extends Reservation {
public HouseReservation(Address physical_address) {
@@ -22,8 +25,19 @@ public final class HouseReservation extends Reservation {
return result;
}
public float getPricePerNight(){
price = (squareFeet > 900.0f) ? 120.0f + 15.0f : 120.0f;
return price;
}
// calculate and return the reservation's price
// Hotel price plus additional flat fee of $50 plus $10 for kitchenette
public float calculatePrice() {
return 0.0f;
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 = getPricePerNight() * days;
return price;
}
}

View File

@@ -0,0 +1,12 @@
package lodge.reservationsystem;
class IllegalOperationException extends RuntimeException {
public IllegalOperationException () {
super();
}
public IllegalOperationException (String message) {
super(message);
}
}

View File

@@ -2,5 +2,4 @@ package lodge.reservationsystem;
public enum KitchenEnum {
None, Kitchenette, FullKitchen;
}

View File

@@ -6,7 +6,6 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.time.ZonedDateTime;
public abstract class Reservation {
@@ -20,6 +19,7 @@ public abstract class Reservation {
ReservationStatusEnum reservation_status;
KitchenEnum kitchen;
Integer numberOfBeds;
Integer numberOfBedRooms;
Integer numberOfBathRooms;
@@ -88,6 +88,15 @@ public abstract class Reservation {
this.reservation_status = reservation_status;
}
public KitchenEnum getKitchen() {
return kitchen;
}
public void setKitchen(KitchenEnum kitchen) {
this.kitchen = kitchen;
}
public Integer numberOfBeds() {
return numberOfBeds;
}
@@ -173,6 +182,7 @@ public abstract class Reservation {
sb.append("\"reservation_start_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 + "\",");
sb.append("\"numberOfBathRooms\": \"" + numberOfBathRooms + "\",");
@@ -195,14 +205,14 @@ public abstract class Reservation {
}
}
public void Change(Reservation reservation, ReservationStatusEnum newStatus) throws IllegalStateException {
public void Change(Reservation reservation, ReservationStatusEnum newStatus) throws IllegalOperationException {
try {
if (reservation.reservation_status == ReservationStatusEnum.Completed) {
if (newStatus == ReservationStatusEnum.Canceled) {
throw new IllegalStateException ("Invalid Change, reservation has completed.");
throw new IllegalOperationException ("Invalid Change, reservation has completed.");
}
if (ZonedDateTime.now().compareTo(this.reservation_start_date) > -1) {
throw new IllegalStateException ("Invalid Change, reservation started.");
throw new IllegalOperationException ("Invalid Change, reservation started.");
}
}
@@ -210,7 +220,7 @@ public abstract class Reservation {
this.setPrice(this.calculatePrice());
}
reservation.setReservation_status(newStatus);
} catch (IllegalStateException e) {
} catch (IllegalOperationException e) {
e.printStackTrace();
}
}