clean up.
This commit is contained in:
@@ -12,6 +12,7 @@ import lodge.reservationsystem.HotelReservation;
|
||||
import lodge.reservationsystem.HouseReservation;
|
||||
import lodge.reservationsystem.Reservation;
|
||||
import lodge.reservationsystem.ReservationStatusEnum;
|
||||
import lodge.reservationsystem.DuplicateObjectException;
|
||||
|
||||
public final class TestReservations {
|
||||
public static void main(String[] args) throws Exception {
|
||||
@@ -91,12 +92,12 @@ public final class TestReservations {
|
||||
Reservation rsrv = account.getReservation_list().getLast();
|
||||
|
||||
// 6. Complete reservation that is associated with an account
|
||||
rsrv = mgr.retreiveReservation(cabin.getReservation_number());
|
||||
rsrv = mgr.retreiveReservation(rsrv.getReservation_number());
|
||||
rsrv.Change(rsrv, ReservationStatusEnum.Completed);
|
||||
mgr.UpdateAccount(mgr.retrieveAccount(acct.account_number()));
|
||||
|
||||
// 7. Cancel reservation that is associated with an account
|
||||
rsrv = mgr.retreiveReservation(cabin.getReservation_number());
|
||||
rsrv = mgr.retreiveReservation(rsrv.getReservation_number());
|
||||
rsrv.Change(rsrv, ReservationStatusEnum.Canceled);
|
||||
mgr.UpdateAccount(mgr.retrieveAccount(acct.account_number()));
|
||||
/*
|
||||
|
||||
@@ -7,14 +7,13 @@ public final class AccomodationManager {
|
||||
|
||||
private final AccountList account_list = new AccountList();
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
AccomodationManager(){
|
||||
AccomodationManager() {
|
||||
};
|
||||
|
||||
public AccomodationManager(String home){
|
||||
|
||||
|
||||
public AccomodationManager(String home) {
|
||||
|
||||
setDataStoreRoot(home);
|
||||
};
|
||||
};
|
||||
|
||||
public final void setDataStoreRoot(String home) {
|
||||
DataRepository.setDataStoreRoot(home);
|
||||
@@ -30,13 +29,12 @@ public final class AccomodationManager {
|
||||
|
||||
// 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 );
|
||||
if (account == null) {
|
||||
System.out.println(String.format("%s", file.toString()));
|
||||
} else {
|
||||
account_list.add(account);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,8 +51,9 @@ public final class AccomodationManager {
|
||||
}
|
||||
|
||||
public void UpdateAccount(Account acct) throws Exception {
|
||||
|
||||
AccountList.save(account_list, acct);
|
||||
if( acct != null ){
|
||||
AccountList.save(account_list, acct);
|
||||
}
|
||||
}
|
||||
|
||||
public final Account newAccount(String phone_number, Address mailing_address, EmailAddress email_address)
|
||||
|
||||
@@ -55,7 +55,7 @@ public class Account {
|
||||
result = reservation_list.add(rsrv);
|
||||
|
||||
if (!result) {
|
||||
throw new ReservationException("Error Reservation already exists.");
|
||||
throw new DuplicateObjectException("Error Reservation already exists.");
|
||||
}
|
||||
} catch (ReservationException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.util.ArrayList;
|
||||
|
||||
public class AccountList extends ArrayList<Account> {
|
||||
|
||||
public AccountList(){
|
||||
public AccountList() {
|
||||
|
||||
}
|
||||
|
||||
@@ -13,7 +13,8 @@ public class AccountList extends ArrayList<Account> {
|
||||
return String.format("A%09d", Math.abs(java.util.Objects.hash(phone_number, mailing_address, email_address)));
|
||||
}
|
||||
|
||||
public static Account newAccount(String phone_number, Address mailing_address, EmailAddress email_address) throws Exception {
|
||||
public static Account newAccount(String phone_number, Address mailing_address, EmailAddress email_address)
|
||||
throws Exception {
|
||||
Account A = new Account(AccountList.accountSerial(phone_number, mailing_address, email_address),
|
||||
phone_number,
|
||||
mailing_address,
|
||||
@@ -21,20 +22,21 @@ 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 DuplicateObjectException, AccountException {
|
||||
String acctNumber = "";
|
||||
for(Account acct: account_list){
|
||||
if( acct.account_number() == account.account_number() ){
|
||||
acctNumber = acct.account_number();
|
||||
break;
|
||||
|
||||
// ** Add account throw error if account number is pre-existing. */
|
||||
public static Account addAccount(final AccountList account_list, final Account account)
|
||||
throws DuplicateObjectException, AccountException {
|
||||
try {
|
||||
for (Account acct : account_list) {
|
||||
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;
|
||||
}
|
||||
if (!acctNumber.isEmpty()) {
|
||||
throw new DuplicateObjectException("Account exists, duplicates not allowed.");
|
||||
} else {
|
||||
account_list.add(account);
|
||||
}
|
||||
account_list.add(account);
|
||||
return account;
|
||||
}
|
||||
|
||||
@@ -52,8 +54,8 @@ public class AccountList extends ArrayList<Account> {
|
||||
|
||||
// ** 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 ){
|
||||
for (Account acct : account_list) {
|
||||
if (acct.account_number() == account_number) {
|
||||
return acct;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +16,10 @@ public class AccountReservationList extends ArrayList<Reservation> {
|
||||
return false;
|
||||
}
|
||||
for(Reservation rsrv: this){
|
||||
boolean result = reservation.getReservation_number().compareTo(rsrv.getReservation_number()) == 0;
|
||||
if(result){
|
||||
return result;
|
||||
}
|
||||
result = reservation.getReservation_number().compareTo(rsrv.getReservation_number()) == 0;
|
||||
if(result){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
reservation.reservation_number = AccountReservationList.reservationSerial(reservation);
|
||||
return super.add(reservation);
|
||||
|
||||
@@ -3,7 +3,6 @@ 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;
|
||||
@@ -13,7 +12,6 @@ import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
|
||||
import com.google.gson.stream.JsonReader;
|
||||
|
||||
final class DataRepository {
|
||||
@@ -65,15 +63,7 @@ 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
|
||||
*/
|
||||
|
||||
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));
|
||||
@@ -133,7 +123,6 @@ final class DataRepository {
|
||||
jsonReader.close();
|
||||
return ac.account_number.length() > 8 ? ac : null;
|
||||
}
|
||||
return ac;
|
||||
}
|
||||
|
||||
static void loadReservationRefList(JsonReader rdr, Account ac) throws IOException {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package lodge.reservationsystem;
|
||||
|
||||
class DuplicateObjectException extends RuntimeException {
|
||||
public class DuplicateObjectException extends RuntimeException {
|
||||
public DuplicateObjectException() {
|
||||
super();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user