update deserialization
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 ;
|
||||
|
||||
@@ -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{
|
||||
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);
|
||||
@@ -149,4 +125,21 @@ final class DataRepository {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package lodge.reservationsystem;
|
||||
|
||||
class DuplicateObjectException extends RuntimeException {
|
||||
public DuplicateObjectException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public DuplicateObjectException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 ;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package lodge.reservationsystem;
|
||||
|
||||
class IllegalOperationException extends RuntimeException {
|
||||
public IllegalOperationException () {
|
||||
super();
|
||||
}
|
||||
|
||||
public IllegalOperationException (String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,5 +2,4 @@ package lodge.reservationsystem;
|
||||
|
||||
public enum KitchenEnum {
|
||||
None, Kitchenette, FullKitchen;
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,36 @@
|
||||
{ "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"}}]}}
|
||||
{
|
||||
"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 +1,18 @@
|
||||
{ "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"}}","numberOfBeds": "2","numberOfBedRooms": "1","numberOfBathRooms": "1","numberOfFloors": "1","squareFeet": "450","price": "120.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 +1 @@
|
||||
{ "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"}}","numberOfBeds": "4","numberOfBedRooms": "3","numberOfBathRooms": "1","numberOfFloors": "3","squareFeet": "1400","price": "120.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 +1,18 @@
|
||||
{ "CabinReservation":{"reservation_type": "CabinReservation","reservation_number": "R2042828431","reservation_status": "Completed","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"}}","numberOfBeds": "4","numberOfBedRooms": "3","numberOfBathRooms": "1","numberOfFloors": "2","squareFeet": "806","price": "10320.0"}}
|
||||
{
|
||||
"CabinReservation": {
|
||||
"reservation_type": "CabinReservation",
|
||||
"reservation_number": "R2042828431",
|
||||
"reservation_status": "Completed",
|
||||
"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": "10200.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user