added serialization
This commit is contained in:
@@ -12,7 +12,8 @@ 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("\\", "/")
|
||||||
|
+ "/workspace/reservationsystem/src/resources";
|
||||||
mgr.setDataStoreRoot(home);
|
mgr.setDataStoreRoot(home);
|
||||||
// 1. Get the list of loaded accounts from Manager
|
// 1. Get the list of loaded accounts from Manager
|
||||||
|
|
||||||
|
|||||||
@@ -1,43 +1,61 @@
|
|||||||
package lodge.reservationsystem;
|
package lodge.reservationsystem;
|
||||||
|
|
||||||
import java.nio.file.FileVisitResult;
|
import java.io.BufferedReader;
|
||||||
import java.nio.file.Files;
|
import java.io.FileReader;
|
||||||
import java.nio.file.SimpleFileVisitor;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
public class AccomodationManager {
|
import com.google.gson.stream.JsonReader;
|
||||||
|
|
||||||
private AccountList account_list = new AccountList();
|
public final class AccomodationManager {
|
||||||
|
|
||||||
public void loadAll() throws Exception {
|
private final AccountList account_list = new AccountList();
|
||||||
|
|
||||||
|
public void setDataStoreRoot(String home) {
|
||||||
|
DataRepository.setDataStoreRoot(home);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void loadAll() throws Exception {
|
||||||
account_list.clear();
|
account_list.clear();
|
||||||
// walk directories
|
// walk directories
|
||||||
Path rootDir = Paths.get(DataRepository.getPath());
|
Path rootDir = Paths.get(DataRepository.getPath());
|
||||||
Files.walkFileTree(rootDir, new SimpleFileVisitor<Path>() {
|
DataRepository.WalkFileSystemTree(this, rootDir);
|
||||||
@Override
|
}
|
||||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
|
|
||||||
System.out.println("File: " + file.toAbsolutePath());
|
|
||||||
// load account number, and content
|
|
||||||
// load reservation
|
|
||||||
return FileVisitResult.CONTINUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
// Load / Deserialize Account
|
||||||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
|
void load(Path file) throws Exception {
|
||||||
System.out.println("Directory: " + dir.toAbsolutePath());
|
/** @TODO finish loading Account */
|
||||||
// prepare to load account number
|
try (BufferedReader in = new BufferedReader(new FileReader(file.toFile(), StandardCharsets.UTF_8))) {
|
||||||
return FileVisitResult.CONTINUE;
|
JsonReader jsonReader = new JsonReader(in);
|
||||||
|
jsonReader.beginObject();
|
||||||
|
while (jsonReader.hasNext()) {
|
||||||
|
String name = jsonReader.nextName();
|
||||||
|
switch (name) {
|
||||||
|
case "Account":
|
||||||
|
System.out.println("Load Account");
|
||||||
|
jsonReader.skipValue();
|
||||||
|
jsonReader.endObject();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println(name);
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
jsonReader.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public final AccountList retrieveLoadedAccounts() {
|
||||||
|
return account_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Account retrieveAccount(String acct_id) {
|
public Account retrieveAccount(String acct_id) {
|
||||||
return AccountList.retrieveAccount(account_list, acct_id);
|
return AccountList.retrieveAccount(account_list, acct_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddAccount(Account acct) throws Exception{
|
public void AddAccount(Account acct) throws Exception {
|
||||||
AccountList.addAccount(account_list, acct);
|
AccountList.addAccount(account_list, acct);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,7 +64,8 @@ public class AccomodationManager {
|
|||||||
AccountList.save(account_list, acct);
|
AccountList.save(account_list, acct);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final Account newAccount(String phone_number, Address mailing_address, EmailAddress email_address) throws Exception {
|
public final Account newAccount(String phone_number, Address mailing_address, EmailAddress email_address)
|
||||||
|
throws Exception {
|
||||||
Account acct = null;
|
Account acct = null;
|
||||||
try {
|
try {
|
||||||
acct = AccountList.newAccount(phone_number, mailing_address, email_address);
|
acct = AccountList.newAccount(phone_number, mailing_address, email_address);
|
||||||
@@ -58,40 +77,17 @@ public class AccomodationManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean addReservation(final Account account, final Reservation reservation) {
|
public boolean addReservation(final Account account, final Reservation reservation) {
|
||||||
return account.reservation_list.add(reservation);
|
return account.reservation_list.add(reservation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Reservation retreiveReservation(String reservation_number) {
|
public Reservation retreiveReservation(String reservation_number) {
|
||||||
for(Account acct: account_list){
|
for (Account acct : account_list) {
|
||||||
for(Reservation rsv: acct.reservation_list){
|
for (Reservation rsv : acct.reservation_list) {
|
||||||
if(rsv.reservation_number == reservation_number){
|
if (rsv.reservation_number == reservation_number) {
|
||||||
return rsv;
|
return rsv;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,7 @@ import java.nio.file.Path;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
public class Account {
|
public class Account {
|
||||||
String account_number;
|
String account_number="-99";
|
||||||
String phone_number;
|
String phone_number;
|
||||||
Address mailing_address;
|
Address mailing_address;
|
||||||
EmailAddress email_address;
|
EmailAddress email_address;
|
||||||
@@ -34,7 +34,8 @@ public class Account {
|
|||||||
sb.append("\"account_number\": \"" + account_number + "\",");
|
sb.append("\"account_number\": \"" + account_number + "\",");
|
||||||
sb.append("\"phone_number\": \"" + phone_number + "\",");
|
sb.append("\"phone_number\": \"" + phone_number + "\",");
|
||||||
sb.append("\"mailing_address\": " + mailing_address + ",");
|
sb.append("\"mailing_address\": " + mailing_address + ",");
|
||||||
sb.append("\"email_address\": " + email_address );
|
sb.append("\"email_address\": " + email_address + "," );
|
||||||
|
sb.append( this.reservation_list.toString());
|
||||||
sb.append("}}");
|
sb.append("}}");
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
@@ -47,7 +48,6 @@ public class Account {
|
|||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 String.format("A%010d", java.util.Objects.hash(phone_number, mailing_address, email_address));
|
return String.format("A%09d", 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 {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import java.util.ArrayList;
|
|||||||
public class AccountReservationList extends ArrayList<Reservation> {
|
public class AccountReservationList extends ArrayList<Reservation> {
|
||||||
|
|
||||||
private static String reservationSerial(Reservation reservation) {
|
private static String reservationSerial(Reservation reservation) {
|
||||||
return String.format("R%04d", java.util.Objects.hash(reservation));
|
return String.format("R%010d", java.util.Objects.hash(reservation));
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean add(final Reservation reservation) {
|
public boolean add(final Reservation reservation) {
|
||||||
@@ -16,11 +16,16 @@ public class AccountReservationList extends ArrayList<Reservation> {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("{ \"AccountReservationList\":{");
|
sb.append("\"AccountReservationList\":[");
|
||||||
for(int ix=0; ix < this.size() ; ++ix){
|
|
||||||
sb.append(String.format("{ \"%d\":\"%s\"},", ix, this.get(ix).reservation_number));
|
for(int ix=0; ix < this.size() ; ix++){
|
||||||
|
if( ( this.size() - 1 ) == ix ){
|
||||||
|
sb.append(String.format("{ \"%d\":\"%s\"}", ix, this.get(ix).reservation_number));
|
||||||
|
}else{
|
||||||
|
sb.append(String.format("{ \"%d\":\"%s\"},", ix, this.get(ix).reservation_number));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
sb.append("}}");
|
sb.append("]");
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,15 @@ package lodge.reservationsystem;
|
|||||||
|
|
||||||
public final class CabinReservation extends Reservation {
|
public final class CabinReservation extends Reservation {
|
||||||
|
|
||||||
|
final char type = 'C';
|
||||||
|
public final char ReservationType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
//calculate and return the reservation's price
|
//calculate and return the reservation's price
|
||||||
|
// Cabin price plus additional fee of $20 for full kitchen and $5 for each additional bathroom
|
||||||
public float calculatePrice() {
|
public float calculatePrice() {
|
||||||
return 0.0f;
|
price = (squareFeet > 900.0f) ? price + 15.0f : price;
|
||||||
|
return price;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
59
src/java/lodge/reservationsystem/DataRepository.java
Normal file
59
src/java/lodge/reservationsystem/DataRepository.java
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package lodge.reservationsystem;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.FileVisitResult;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.SimpleFileVisitor;
|
||||||
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
protected static DataRepository getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final static void setDataStoreRoot(final String direcoryPath) {
|
||||||
|
getInstance().directoryPath = direcoryPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final static String getPath() {
|
||||||
|
return getInstance().directoryPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
System.out.println("File: " + file.toAbsolutePath());
|
||||||
|
// load account number, and content
|
||||||
|
if (attrs.isRegularFile()) {
|
||||||
|
String namestring = file.getName(file.getNameCount() - 1).toString();
|
||||||
|
if (namestring.endsWith("json")) {
|
||||||
|
if (namestring.startsWith("acc")) { // * load Account *//
|
||||||
|
try {
|
||||||
|
manager.load(file);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// load reservation
|
||||||
|
return FileVisitResult.CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
|
||||||
|
System.out.println("Directory: " + dir.toAbsolutePath());
|
||||||
|
// prepare to load account number
|
||||||
|
return FileVisitResult.CONTINUE;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,8 +2,14 @@ package lodge.reservationsystem;
|
|||||||
|
|
||||||
public final class HotelReservation extends Reservation {
|
public final class HotelReservation extends Reservation {
|
||||||
|
|
||||||
|
final char type = 'H';
|
||||||
|
public final char ReservationType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
//calculate and return the reservation's price
|
//calculate and return the reservation's price
|
||||||
|
// Hotel price plus additional flat fee of $50 plus $10 for kitchenette
|
||||||
public float calculatePrice() {
|
public float calculatePrice() {
|
||||||
|
// flat fee
|
||||||
return 0.0f;
|
return 0.0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,15 @@
|
|||||||
package lodge.reservationsystem;
|
package lodge.reservationsystem;
|
||||||
|
|
||||||
public final class HouseReservation extends Reservation {
|
public final class HouseReservation extends Reservation {
|
||||||
//calculate and return the reservation's price
|
|
||||||
|
final char type = 'Z';
|
||||||
|
public final char ReservationType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculate and return the reservation's price
|
||||||
public float calculatePrice() {
|
public float calculatePrice() {
|
||||||
return 0.0f;
|
return 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -12,6 +12,7 @@ public abstract class Reservation {
|
|||||||
ZonedDateTime reservation_end_date;
|
ZonedDateTime reservation_end_date;
|
||||||
ReservationStatus reservation_status;
|
ReservationStatus reservation_status;
|
||||||
|
|
||||||
|
Boolean hasKitchenette;
|
||||||
Integer numberOfBeds;
|
Integer numberOfBeds;
|
||||||
Integer numberOfBedRooms;
|
Integer numberOfBedRooms;
|
||||||
Integer numberOfBathRooms;
|
Integer numberOfBathRooms;
|
||||||
@@ -117,11 +118,11 @@ public abstract class Reservation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Reservation() {
|
public Reservation() {
|
||||||
numberOfBeds = 0;
|
numberOfBeds = 1;
|
||||||
numberOfBedRooms = 0;
|
numberOfBedRooms = 1;
|
||||||
numberOfBathRooms = 0;
|
numberOfBathRooms = 1;
|
||||||
numberOfFloors = 0;
|
numberOfFloors = 1;
|
||||||
price = 0.0f;
|
price = 120.0f;
|
||||||
reservation_status = ReservationStatus.Draft;
|
reservation_status = ReservationStatus.Draft;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,7 +166,9 @@ public abstract class Reservation {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
sb.append("{ \"Reservation\":{");
|
sb.append("{ \"Reservation\":{");
|
||||||
|
sb.append("\"reservation_type\": \"" + ReservationType() + "\",");
|
||||||
sb.append("\"reservation_number\": \"" + reservation_number + "\",");
|
sb.append("\"reservation_number\": \"" + reservation_number + "\",");
|
||||||
sb.append("\"reservation_status\": \"" + reservation_status + "\",");
|
sb.append("\"reservation_status\": \"" + reservation_status + "\",");
|
||||||
sb.append("\"reservation_start_date\": \"" + reservation_start_date + "\",");
|
sb.append("\"reservation_start_date\": \"" + reservation_start_date + "\",");
|
||||||
@@ -191,5 +194,7 @@ public abstract class Reservation {
|
|||||||
return 0.0f;
|
return 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract char ReservationType();
|
||||||
public abstract float calculatePrice();
|
public abstract float calculatePrice();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
1
src/resources/acc-A1858063803.json
Normal file
1
src/resources/acc-A1858063803.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{ "Account":{"account_number": "A1858063803","phone_number": "767-456-7890","mailing_address": { "Address":{"street": "10 wilco ave","city": "wilco","state": "WY","zip": "82801"}},"email_address": { "EmailAdress":{"email": "wilco@wyommin.net"}},"AccountReservationList":[{ "0":"R0000000062"}]}}
|
||||||
1
src/resources/rsv-C0062.json
Normal file
1
src/resources/rsv-C0062.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"CabinReservation": ""}
|
||||||
Reference in New Issue
Block a user