store/retrieve different types
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
| software | version |
|
||||
| -------- | ------- |
|
||||
| Java | 21 |
|
||||
| json lib | gson-2.13.1 |
|
||||
| VSCode | 1.103 |
|
||||
|
||||
###
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -3,8 +3,10 @@ package lodge;
|
||||
import lodge.reservationsystem.AccomodationManager;
|
||||
import lodge.reservationsystem.Account;
|
||||
import lodge.reservationsystem.Address;
|
||||
import lodge.reservationsystem.CabinReservation;
|
||||
import lodge.reservationsystem.EmailAddress;
|
||||
import lodge.reservationsystem.CabinReservation;
|
||||
import lodge.reservationsystem.HouseReservation;
|
||||
import lodge.reservationsystem.HotelReservation;
|
||||
import lodge.reservationsystem.Reservation;
|
||||
|
||||
public final class TestReservations {
|
||||
@@ -38,7 +40,7 @@ public final class TestReservations {
|
||||
// 5. Add draft lodging reservation to an account (if reservation object already
|
||||
// exists with the same reservation number, it is considered an error)
|
||||
|
||||
CabinReservation cabin = new CabinReservation();
|
||||
HotelReservation cabin = new HotelReservation();
|
||||
cabin.setPhysical_address(new Address("30 cabin ave", "Carnelian", "CA", "96140"));
|
||||
cabin.setMailing_address(new Address("40 cabin ave", "Carnelian Bay", "CA", "96140"));
|
||||
cabin.setNumberOfBeds(4);
|
||||
@@ -47,9 +49,11 @@ public final class TestReservations {
|
||||
cabin.setSquareFeet(806);
|
||||
mgr.addReservation( mgr.retrieveAccount(acct.account_number()), cabin);
|
||||
Reservation rsrv = cabin;
|
||||
String reservationId = rsrv.reservation_number();
|
||||
String reservationId = rsrv.getReservation_number();
|
||||
|
||||
mgr.UpdateAccount(mgr.retrieveAccount(acct.account_number()));
|
||||
|
||||
// 6. Complete reservation that is associated with an account
|
||||
rsrv = null;
|
||||
rsrv = mgr.retreiveReservation(reservationId);
|
||||
rsrv.Complete();
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
|
||||
public final class AccomodationManager {
|
||||
@@ -27,20 +29,87 @@ public final class AccomodationManager {
|
||||
void load(Path file) throws Exception {
|
||||
/** @TODO finish loading Account */
|
||||
try (BufferedReader in = new BufferedReader(new FileReader(file.toFile(), StandardCharsets.UTF_8))) {
|
||||
final Gson gson = new GsonBuilder()
|
||||
.create();
|
||||
|
||||
JsonReader jsonReader = new JsonReader(in);
|
||||
jsonReader.beginObject();
|
||||
Account ac = null;
|
||||
Address ad = null;
|
||||
while (jsonReader.hasNext()) {
|
||||
String name = jsonReader.nextName();
|
||||
System.out.println(String.format("Load Account %s", name));
|
||||
switch (name) {
|
||||
case "Account":
|
||||
System.out.println("Load Account");
|
||||
jsonReader.skipValue();
|
||||
jsonReader.beginObject();
|
||||
ac = new Account();
|
||||
break;
|
||||
case "account_number":
|
||||
ac.setAccount_number(jsonReader.nextString());
|
||||
break;
|
||||
case "phone_number":
|
||||
ac.setPhone_number(jsonReader.nextString());
|
||||
break;
|
||||
case "mailing_address":
|
||||
jsonReader.beginObject();
|
||||
break;
|
||||
case "Address":
|
||||
jsonReader.beginObject();
|
||||
System.out.println(jsonReader.nextName());
|
||||
ad = new Address();
|
||||
ad.setStreet(jsonReader.nextString());
|
||||
System.out.println(jsonReader.nextName());
|
||||
ad.setCity(jsonReader.nextString());
|
||||
System.out.println(jsonReader.nextName());
|
||||
ad.setState(jsonReader.nextString());
|
||||
System.out.println(jsonReader.nextName());
|
||||
ad.setZip(jsonReader.nextString());
|
||||
jsonReader.endObject();
|
||||
jsonReader.endObject();
|
||||
ac.setMailing_address(ad);
|
||||
break;
|
||||
case "email_address":
|
||||
jsonReader.beginObject();
|
||||
break;
|
||||
case "EmailAddress":
|
||||
jsonReader.beginObject();
|
||||
break;
|
||||
case "email":
|
||||
ac.setEmail_address(new EmailAddress(jsonReader.nextString()));
|
||||
jsonReader.endObject();
|
||||
jsonReader.endObject();
|
||||
break;
|
||||
case "reservation_list":
|
||||
jsonReader.beginArray();
|
||||
jsonReader.beginObject();
|
||||
break;
|
||||
case "CabinReservation":
|
||||
jsonReader.beginObject();
|
||||
name = jsonReader.nextName();
|
||||
CabinReservation cabin = new CabinReservation();
|
||||
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();
|
||||
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();
|
||||
hotel.setMailing_address(ad);
|
||||
hotel.setPhysical_address(ad);
|
||||
hotel.reservation_number = jsonReader.nextString();
|
||||
break;
|
||||
default:
|
||||
System.out.println(name);
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
jsonReader.close();
|
||||
@@ -77,13 +146,13 @@ public final class AccomodationManager {
|
||||
}
|
||||
|
||||
public boolean addReservation(final Account account, final Reservation reservation) {
|
||||
return account.reservation_list.add(reservation);
|
||||
return account.add(reservation);
|
||||
}
|
||||
|
||||
public Reservation retreiveReservation(String reservation_number) {
|
||||
for (Account acct : account_list) {
|
||||
for (Reservation rsv : acct.reservation_list) {
|
||||
if (rsv.reservation_number == reservation_number) {
|
||||
if (rsv.getReservation_number() == reservation_number) {
|
||||
return rsv;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ public class Account {
|
||||
Address mailing_address;
|
||||
EmailAddress email_address;
|
||||
|
||||
AccountReservationList reservation_list=new AccountReservationList();
|
||||
AccountReservationList reservation_list = new AccountReservationList();;
|
||||
|
||||
public Account(
|
||||
String account_number,
|
||||
@@ -26,6 +26,10 @@ public class Account {
|
||||
this.email_address = email_address;
|
||||
}
|
||||
|
||||
public Account(){
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
@@ -40,6 +44,10 @@ public class Account {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public boolean add(Reservation rsrv){
|
||||
return reservation_list.add(rsrv);
|
||||
}
|
||||
|
||||
// @TODO Write Account out in JSON
|
||||
public static void Write(Account acct) throws IOException {
|
||||
String dataRoot = DataRepository.getPath();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package lodge.reservationsystem;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class AccountList extends ArrayList<Account> {
|
||||
@@ -17,7 +18,7 @@ public class AccountList extends ArrayList<Account> {
|
||||
return A;
|
||||
}
|
||||
// ** Add account throw error if account number is pre-existing. */
|
||||
public static Account addAccount(AccountList account_list, Account account) throws NullPointerException, AccountException {
|
||||
public static Account addAccount(final AccountList account_list, final Account account) throws NullPointerException, AccountException {
|
||||
String acctNumber = "";
|
||||
for(Account acct: account_list){
|
||||
if( acct.account_number() == account.account_number() ){
|
||||
@@ -34,19 +35,19 @@ public class AccountList extends ArrayList<Account> {
|
||||
}
|
||||
|
||||
// save account in edit status
|
||||
public void save(Account acct) {
|
||||
|
||||
public static void save(final Account acct) throws IOException {
|
||||
Account.Write(acct);
|
||||
}
|
||||
|
||||
// save accounts in edit status
|
||||
public static void save(AccountList account_list, Account acct) throws Exception {
|
||||
public static void save(final AccountList account_list, final Account acct) throws Exception {
|
||||
for (Account account : account_list) {
|
||||
Account.Write(account);
|
||||
AccountList.save(account);
|
||||
}
|
||||
}
|
||||
|
||||
// ** Find account return null not-existing. */
|
||||
public static Account retrieveAccount(AccountList account_list, String account_number) {
|
||||
public static Account retrieveAccount(final AccountList account_list, String account_number) {
|
||||
for(Account acct: account_list){
|
||||
if( acct.account_number() == account_number ){
|
||||
return acct;
|
||||
|
||||
@@ -16,13 +16,15 @@ public class AccountReservationList extends ArrayList<Reservation> {
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("\"AccountReservationList\":[");
|
||||
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("{ \"%d\":\"%s\"}", ix, this.get(ix).reservation_number));
|
||||
sb.append(String.format("{\"%s\":{\"reservation_number\":\"%s\"}}", name, this.get(ix).getReservation_number()));
|
||||
}else{
|
||||
sb.append(String.format("{ \"%d\":\"%s\"},", ix, this.get(ix).reservation_number));
|
||||
sb.append(String.format("{\"%s\":{\"reservation_number\":\"%s\"}},", name, this.get(ix).getReservation_number()));
|
||||
}
|
||||
}
|
||||
sb.append("]");
|
||||
|
||||
@@ -14,6 +14,9 @@ public final class Address {
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
protected Address() {
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package lodge.reservationsystem;
|
||||
|
||||
public final class CabinReservation extends Reservation {
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
final char type = 'C';
|
||||
public final char ReservationType() {
|
||||
return type;
|
||||
import com.google.gson.InstanceCreator;
|
||||
|
||||
public final class CabinReservation extends Reservation implements InstanceCreator<Reservation>{
|
||||
|
||||
public final String ReservationType() {
|
||||
type='C';
|
||||
return "CabinReservation";
|
||||
}
|
||||
//calculate and return the reservation's price
|
||||
// Cabin price plus additional fee of $20 for full kitchen and $5 for each additional bathroom
|
||||
@@ -13,4 +17,9 @@ public final class CabinReservation extends Reservation {
|
||||
return price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reservation createInstance(Type type) {
|
||||
System.out.println( String.format( "%s.", this.type));
|
||||
return new CabinReservation();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public class EmailAddress{
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{ \"EmailAdress\":{");
|
||||
sb.append("{ \"EmailAddress\":{");
|
||||
sb.append("\"email\": \"" + email_address + "\"");
|
||||
sb.append("}}");
|
||||
return sb.toString();
|
||||
|
||||
@@ -1,15 +1,26 @@
|
||||
package lodge.reservationsystem;
|
||||
|
||||
public final class HotelReservation extends Reservation {
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
final char type = 'H';
|
||||
public final char ReservationType() {
|
||||
return type;
|
||||
import com.google.gson.InstanceCreator;
|
||||
|
||||
public final class HotelReservation extends Reservation implements InstanceCreator<Reservation>{
|
||||
|
||||
public final String ReservationType() {
|
||||
type = 'H';
|
||||
return "HotelReservation";
|
||||
}
|
||||
|
||||
//calculate and return the reservation's price
|
||||
// Hotel price plus additional flat fee of $50 plus $10 for kitchenette
|
||||
public float calculatePrice() {
|
||||
// flat fee
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reservation createInstance(Type type) {
|
||||
System.out.println( String.format( "%s.", this.type));
|
||||
return (Reservation) new HotelReservation();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,14 @@
|
||||
package lodge.reservationsystem;
|
||||
|
||||
public final class HouseReservation extends Reservation {
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
final char type = 'Z';
|
||||
public final char ReservationType() {
|
||||
return type;
|
||||
import com.google.gson.InstanceCreator;
|
||||
|
||||
public final class HouseReservation extends Reservation implements InstanceCreator<Reservation>{
|
||||
|
||||
public final String ReservationType() {
|
||||
type = 'Z';
|
||||
return "HouseReservation";
|
||||
}
|
||||
|
||||
// calculate and return the reservation's price
|
||||
@@ -12,4 +16,9 @@ public final class HouseReservation extends Reservation {
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reservation createInstance(Type type) {
|
||||
System.out.println( String.format( "%s.", this.type));
|
||||
return new HouseReservation();
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,23 @@
|
||||
package lodge.reservationsystem;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
public abstract class Reservation{
|
||||
char type;
|
||||
String reservation_number;
|
||||
Address physical_address;
|
||||
Address mailing_address;
|
||||
|
||||
ZonedDateTime reservation_start_date;
|
||||
ZonedDateTime reservation_end_date;
|
||||
Timestamp reservation_start_date;
|
||||
Timestamp reservation_end_date;
|
||||
ReservationStatus reservation_status;
|
||||
|
||||
Boolean hasKitchenette;
|
||||
@@ -21,7 +29,7 @@ public abstract class Reservation {
|
||||
|
||||
Float price;
|
||||
|
||||
public String reservation_number() {
|
||||
public String getReservation_number() {
|
||||
return reservation_number;
|
||||
}
|
||||
|
||||
@@ -45,19 +53,19 @@ public abstract class Reservation {
|
||||
this.mailing_address = mailing_address;
|
||||
}
|
||||
|
||||
public ZonedDateTime reservation_start_date() {
|
||||
public Timestamp reservation_start_date() {
|
||||
return reservation_start_date;
|
||||
}
|
||||
|
||||
public void setReservation_start_date(ZonedDateTime reservation_start_date) {
|
||||
public void setReservation_start_date(Timestamp reservation_start_date) {
|
||||
this.reservation_start_date = reservation_start_date;
|
||||
}
|
||||
|
||||
public ZonedDateTime reservation_end_date() {
|
||||
public Timestamp reservation_end_date() {
|
||||
return reservation_end_date;
|
||||
}
|
||||
|
||||
public void setReservation_end_date(ZonedDateTime reservation_end_date) {
|
||||
public void setReservation_end_date(Timestamp reservation_end_date) {
|
||||
this.reservation_end_date = reservation_end_date;
|
||||
}
|
||||
|
||||
@@ -186,15 +194,36 @@ public abstract class Reservation {
|
||||
}
|
||||
|
||||
// @TODO write reservation out in json
|
||||
public static void Write(Reservation reservation) throws IOException {
|
||||
public void Write(Reservation reservation) throws IOException {
|
||||
String dataRoot = DataRepository.getPath();
|
||||
dataRoot = dataRoot + "/rsv-" + reservation.reservation_number + ".json";
|
||||
Path path = Paths.get(dataRoot);
|
||||
|
||||
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
|
||||
writer.write(super.toString());
|
||||
writer.flush();
|
||||
}
|
||||
}
|
||||
|
||||
// @TODO read reservation out in json
|
||||
public void Read(Reservation reservation) throws IOException {
|
||||
String dataRoot = DataRepository.getPath();
|
||||
dataRoot = dataRoot + "/rsv-" + reservation.reservation_number + ".json";
|
||||
Path path = Paths.get(dataRoot);
|
||||
|
||||
try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
|
||||
reader.lines();
|
||||
}
|
||||
}
|
||||
|
||||
public float calculatePricePerNight() {
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
public abstract char ReservationType();
|
||||
public abstract String ReservationType();
|
||||
|
||||
public abstract float calculatePrice();
|
||||
|
||||
public abstract Reservation createInstance(Type type);
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +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"}]}}
|
||||
{ "Account":{"account_number": "A1858063803","phone_number": "767-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":"R0000000062"}}]}}
|
||||
@@ -1 +1,5 @@
|
||||
{"CabinReservation": ""}
|
||||
{"CabinReservation": {
|
||||
|
||||
"reservation_number":"R000000062"
|
||||
|
||||
}}
|
||||
Reference in New Issue
Block a user