updates cleanup.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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() + "";
|
||||
|
||||
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()));
|
||||
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()));
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -63,7 +63,7 @@ final class DataRepository {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public final static Account LoadAccount(final Path file) throws IOException {
|
||||
/** @TODO finish loading Account */
|
||||
try (BufferedReader in = new BufferedReader(new FileReader(file.toFile(), StandardCharsets.UTF_8));
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user