write account, reservation

This commit is contained in:
2025-08-29 10:32:10 -04:00
parent 052391d7a0
commit dda4495a21
7 changed files with 76 additions and 48 deletions

View File

@@ -1,12 +1,9 @@
package lodge; package lodge;
import java.net.URI;
import lodge.reservationsystem.AccomodationManager; import lodge.reservationsystem.AccomodationManager;
import lodge.reservationsystem.Account; import lodge.reservationsystem.Account;
import lodge.reservationsystem.Address; import lodge.reservationsystem.Address;
import lodge.reservationsystem.CabinReservation; import lodge.reservationsystem.CabinReservation;
import lodge.reservationsystem.DataRepository;
import lodge.reservationsystem.EmailAddress; import lodge.reservationsystem.EmailAddress;
import lodge.reservationsystem.Reservation; import lodge.reservationsystem.Reservation;
@@ -16,8 +13,7 @@ public final class TestReservations {
AccomodationManager mgr = new AccomodationManager(); AccomodationManager mgr = new AccomodationManager();
String home = System.getenv("HOMEDRIVE")+System.getenv("HOMEPATH"); String home = System.getenv("HOMEDRIVE")+System.getenv("HOMEPATH");
home= home.replace("\\", "/") + "/data"; home= home.replace("\\", "/") + "/data";
DataRepository.setDataStoreRoot(home); mgr.setDataStoreRoot(home);
// 1. Get the list of loaded accounts from Manager // 1. Get the list of loaded accounts from Manager
mgr.loadAll(); mgr.loadAll();
@@ -49,20 +45,20 @@ public final class TestReservations {
cabin.setNumberOfBedRooms(3); cabin.setNumberOfBedRooms(3);
cabin.setSquareFeet(806); cabin.setSquareFeet(806);
mgr.addReservation( mgr.retrieveAccount(acct.account_number()), cabin); mgr.addReservation( mgr.retrieveAccount(acct.account_number()), cabin);
Reservation rsrv = cabin;
String reservationId = rsrv.reservation_number();
// 6. Complete reservation that is associated with an account // 6. Complete reservation that is associated with an account
Reservation rsrv = mgr.retreiveReservation("R######"); rsrv = null;
rsrv = mgr.retreiveReservation(reservationId);
rsrv.Complete(); rsrv.Complete();
//mgr.UpdateAccount(mgr.retrieveAccount(acct.account_number())); mgr.UpdateAccount(mgr.retrieveAccount(acct.account_number()));
/* // 7. Cancel reservation that is associated with an account
* // 7. Cancel reservation that is associated with an account rsrv = mgr.retreiveReservation(reservationId);
* if (acct != null) { rsrv.Cancel();
* rsrv = acct.retrieve("?######"); mgr.UpdateAccount(mgr.retrieveAccount(acct.account_number()));
* if (rsrv != null) /*
* rsrv.Cancel();
* }
* *
* if (rsrv != null) { * if (rsrv != null) {
* // 8. Change reservation values that can be changed (if reservation is * // 8. Change reservation values that can be changed (if reservation is

View File

@@ -20,7 +20,7 @@ public class AccomodationManager {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
System.out.println("File: " + file.toAbsolutePath()); System.out.println("File: " + file.toAbsolutePath());
// load account number, and content // load account number, and content
// load reservation files // load reservation
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
@@ -57,13 +57,41 @@ public class AccomodationManager {
return acct; return acct;
} }
public void addReservation(Account account, CabinReservation cabin) { public boolean addReservation(final Account account, final Reservation reservation) {
// TODO Auto-generated method stub return account.reservation_list.add(reservation);
throw new UnsupportedOperationException("Unimplemented method 'addReservation'");
} }
public Reservation retreiveReservation(String string) { public Reservation retreiveReservation(String reservation_number) {
// TODO Auto-generated method stub for(Account acct: account_list){
throw new UnsupportedOperationException("Unimplemented method 'retreiveReservation'"); for(Reservation rsv: acct.reservation_list){
if(rsv.reservation_number == reservation_number){
return rsv;
}
}
}
return null;
}
public void setDataStoreRoot(String home) {
DataRepository.setDataStoreRoot(home);
}
}
final class DataRepository {
// SINGLETON CLASS
// hard code data store location for storage of
// account data files on filesystem
private String directoryPath;
private static final DataRepository instance = new DataRepository();
static DataRepository getInstance() {
return instance;
}
final static void setDataStoreRoot(final String direcoryPath){
getInstance().directoryPath = direcoryPath;
}
final static String getPath() {
return getInstance().directoryPath;
} }
} }

View File

@@ -13,6 +13,8 @@ public class Account {
Address mailing_address; Address mailing_address;
EmailAddress email_address; EmailAddress email_address;
AccountReservationList reservation_list=new AccountReservationList();
public Account( public Account(
String account_number, String account_number,
String phone_number, String phone_number,
@@ -40,11 +42,12 @@ public class Account {
// @TODO Write Account out in JSON // @TODO Write Account out in JSON
public static void Write(Account acct) throws IOException { public static void Write(Account acct) throws IOException {
String dataRoot = DataRepository.getPath(); String dataRoot = DataRepository.getPath();
dataRoot = dataRoot + "/" + acct.account_number + ".json"; dataRoot = dataRoot + "/acc-" + acct.account_number + ".json";
Path path = Paths.get(dataRoot); Path path = Paths.get(dataRoot);
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) { try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
writer.write(acct.toString()); writer.write(acct.toString());
writer.write(acct.reservation_list.toString());
writer.flush(); writer.flush();
} }
} }

View File

@@ -5,7 +5,7 @@ import java.util.ArrayList;
public class AccountList extends ArrayList<Account> { public class AccountList extends ArrayList<Account> {
public static String accountSerial(String phone_number, Address mailing_address, EmailAddress email_address) { public static String accountSerial(String phone_number, Address mailing_address, EmailAddress email_address) {
return "A" + java.util.Objects.hash(phone_number, mailing_address, email_address); return String.format("A%010d", 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 {

View File

@@ -2,11 +2,25 @@ package lodge.reservationsystem;
import java.util.ArrayList; import java.util.ArrayList;
public class AccountReservationList { public class AccountReservationList extends ArrayList<Reservation> {
ArrayList<Reservation> reservation_list = new ArrayList<Reservation>(); private static String reservationSerial(Reservation reservation) {
return String.format("R%04d", java.util.Objects.hash(reservation));
}
public void addReservation(Reservation reservation) { public boolean add(final Reservation reservation) {
reservation_list.add(reservation); reservation.reservation_number = AccountReservationList.reservationSerial(reservation);
return super.add(reservation);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{ \"AccountReservationList\":{");
for(int ix=0; ix < this.size() ; ++ix){
sb.append(String.format("{ \"%d\":\"%s\"},", ix, this.get(ix).reservation_number));
}
sb.append("}}");
return sb.toString();
} }
} }

View File

@@ -1,19 +0,0 @@
package lodge.reservationsystem;
public final class DataRepository {
// hard code data store location for storage of
// account data files on filesystem
private String directoryPath;
private static final DataRepository instance = new DataRepository();
public static DataRepository getInstance() {
return instance;
}
public static void setDataStoreRoot(String direcoryPath){
getInstance().directoryPath = direcoryPath;
}
public static String getPath() {
return getInstance().directoryPath;
}
}

View File

@@ -1,5 +1,6 @@
package lodge.reservationsystem; package lodge.reservationsystem;
import java.io.IOException;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
public abstract class Reservation { public abstract class Reservation {
@@ -180,6 +181,11 @@ public abstract class Reservation {
sb.append("}}"); sb.append("}}");
return sb.toString(); return sb.toString();
} }
//@TODO write reservation out in json
public static void Write(Reservation reservation) throws IOException {
}
public float calculatePricePerNight() { public float calculatePricePerNight() {
return 0.0f; return 0.0f;