update time function, rebased
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package lodge;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import lodge.reservationsystem.AccomodationManager;
|
||||
import lodge.reservationsystem.Account;
|
||||
import lodge.reservationsystem.Address;
|
||||
@@ -13,9 +16,9 @@ public final class TestReservations {
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
AccomodationManager mgr = new AccomodationManager();
|
||||
String home = System.getenv("HOMEDRIVE")+System.getenv("HOMEPATH");
|
||||
home= home.replace("\\", "/")
|
||||
+ "/workspace/reservationsystem/src/resources";
|
||||
String home = System.getenv("HOMEDRIVE") + System.getenv("HOMEPATH");
|
||||
home = home.replace("\\", "/")
|
||||
+ "/workspace/reservationsystem/src/resources";
|
||||
mgr.setDataStoreRoot(home);
|
||||
// 1. Get the list of loaded accounts from Manager
|
||||
|
||||
@@ -29,9 +32,9 @@ public final class TestReservations {
|
||||
// already exists on add action with the same account number, it is considered
|
||||
// an error)
|
||||
|
||||
Account acct = mgr.newAccount("767-456-7890",
|
||||
new Address("10 wilco ave", "wilco", "WY", "82801"),
|
||||
new EmailAddress("wilco@wyommin.net"));
|
||||
Account acct = mgr.newAccount("701-456-7890",
|
||||
new Address("10 wilco ave", "wilco", "WY", "82801"),
|
||||
new EmailAddress("wilco@wyommin.net"));
|
||||
mgr.AddAccount(acct);
|
||||
|
||||
// 4. Request that Manager updates specific account’s files with data stored in
|
||||
@@ -40,27 +43,57 @@ 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)
|
||||
|
||||
HotelReservation cabin = new HotelReservation();
|
||||
HotelReservation hotel = new HotelReservation();
|
||||
hotel.setPhysical_address(new Address("400 hotel ave", "Maryland City", "CA", "20723"));
|
||||
hotel.setMailing_address(new Address("400 hotel ave", "Maryland City", "MD", "20723"));
|
||||
hotel.setNumberOfBeds(3);
|
||||
hotel.setNumberOfFloors(1);
|
||||
hotel.setNumberOfBedRooms(2);
|
||||
hotel.setSquareFeet(450);
|
||||
boolean success1 = mgr.addReservation(mgr.retrieveAccount(acct.account_number()), hotel);
|
||||
assert success1;
|
||||
|
||||
mgr.UpdateAccount(mgr.retrieveAccount(acct.account_number()));
|
||||
|
||||
CabinReservation cabin = new CabinReservation();
|
||||
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);
|
||||
cabin.setNumberOfFloors(2);
|
||||
cabin.setNumberOfBedRooms(3);
|
||||
cabin.setSquareFeet(806);
|
||||
mgr.addReservation( mgr.retrieveAccount(acct.account_number()), cabin);
|
||||
Reservation rsrv = cabin;
|
||||
String reservationId = rsrv.getReservation_number();
|
||||
|
||||
mgr.UpdateAccount(mgr.retrieveAccount(acct.account_number()));
|
||||
boolean success2 = mgr.addReservation(mgr.retrieveAccount(acct.account_number()), cabin);
|
||||
assert success2;
|
||||
|
||||
HouseReservation house = new HouseReservation();
|
||||
house.setPhysical_address(new Address("3000 Osage ave", "GreenBelt", "MD", "20740"));
|
||||
house.setMailing_address(new Address("40012 College ave", "College Park", "MD", "20740"));
|
||||
house.setNumberOfBeds(4);
|
||||
house.setNumberOfFloors(3);
|
||||
house.setNumberOfBedRooms(3);
|
||||
house.setSquareFeet(1400);
|
||||
house.setReservation_start_date(ZonedDateTime.of(2025, 07, 05, 10, 0, 0, 0, ZoneId.of("UTC")));
|
||||
house.setReservation_end_date(ZonedDateTime.of(2025, 11, 30, 22, 0, 0, 0, ZoneId.of("UTC")));
|
||||
boolean success3 = mgr.addReservation(mgr.retrieveAccount(acct.account_number()), house);
|
||||
assert success3;
|
||||
|
||||
Reservation rsrv = (Reservation) cabin;
|
||||
try {
|
||||
assert mgr.addReservation(acct, cabin);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error: cabin already in list.");
|
||||
}
|
||||
|
||||
mgr.UpdateAccount(mgr.retrieveAccount(acct.account_number()));
|
||||
|
||||
// 6. Complete reservation that is associated with an account
|
||||
rsrv = mgr.retreiveReservation(reservationId);
|
||||
rsrv = mgr.retreiveReservation(cabin.getReservation_number());
|
||||
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
|
||||
rsrv = mgr.retreiveReservation(reservationId);
|
||||
rsrv = mgr.retreiveReservation(cabin.getReservation_number());
|
||||
rsrv.Cancel();
|
||||
mgr.UpdateAccount(mgr.retrieveAccount(acct.account_number()));
|
||||
/*
|
||||
|
||||
@@ -1,15 +1,8 @@
|
||||
package lodge.reservationsystem;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
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 {
|
||||
|
||||
private final AccountList account_list = new AccountList();
|
||||
@@ -27,93 +20,7 @@ public final class AccomodationManager {
|
||||
|
||||
// Load / Deserialize Account
|
||||
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":
|
||||
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);
|
||||
}
|
||||
}
|
||||
jsonReader.close();
|
||||
}
|
||||
DataRepository.LoadAccount(file);
|
||||
}
|
||||
|
||||
public final AccountList retrieveLoadedAccounts() {
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.util.ArrayList;
|
||||
public class AccountList extends ArrayList<Account> {
|
||||
|
||||
public static String accountSerial(String phone_number, Address mailing_address, EmailAddress email_address) {
|
||||
return String.format("A%09d", java.util.Objects.hash(phone_number, mailing_address, email_address));
|
||||
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 {
|
||||
|
||||
@@ -5,10 +5,13 @@ import java.util.ArrayList;
|
||||
public class AccountReservationList extends ArrayList<Reservation> {
|
||||
|
||||
private static String reservationSerial(Reservation reservation) {
|
||||
return String.format("R%010d", java.util.Objects.hash(reservation));
|
||||
return String.format("R%010d", Math.abs(java.util.Objects.hash(reservation.physical_address)));
|
||||
}
|
||||
|
||||
public boolean add(final Reservation reservation) {
|
||||
if (this.contains(reservation)){
|
||||
return false;
|
||||
}
|
||||
reservation.reservation_number = AccountReservationList.reservationSerial(reservation);
|
||||
return super.add(reservation);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
package lodge.reservationsystem;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
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;
|
||||
|
||||
import com.google.gson.stream.JsonReader;
|
||||
|
||||
final class DataRepository {
|
||||
// SINGLETON CLASS
|
||||
// hard code data store location for storage of
|
||||
@@ -55,5 +60,93 @@ final class DataRepository {
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void LoadAccount(Path file) throws IOException{
|
||||
/** @TODO finish loading Account */
|
||||
try (BufferedReader in = new BufferedReader(new FileReader(file.toFile(), StandardCharsets.UTF_8))) {
|
||||
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":
|
||||
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);
|
||||
}
|
||||
}
|
||||
jsonReader.close();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
public abstract class Reservation{
|
||||
char type;
|
||||
@@ -16,8 +16,8 @@ public abstract class Reservation{
|
||||
Address physical_address;
|
||||
Address mailing_address;
|
||||
|
||||
Timestamp reservation_start_date;
|
||||
Timestamp reservation_end_date;
|
||||
ZonedDateTime reservation_start_date;
|
||||
ZonedDateTime reservation_end_date;
|
||||
ReservationStatus reservation_status;
|
||||
|
||||
Boolean hasKitchenette;
|
||||
@@ -53,19 +53,19 @@ public abstract class Reservation{
|
||||
this.mailing_address = mailing_address;
|
||||
}
|
||||
|
||||
public Timestamp reservation_start_date() {
|
||||
public ZonedDateTime reservation_start_date() {
|
||||
return reservation_start_date;
|
||||
}
|
||||
|
||||
public void setReservation_start_date(Timestamp reservation_start_date) {
|
||||
public void setReservation_start_date(ZonedDateTime reservation_start_date) {
|
||||
this.reservation_start_date = reservation_start_date;
|
||||
}
|
||||
|
||||
public Timestamp reservation_end_date() {
|
||||
public ZonedDateTime reservation_end_date() {
|
||||
return reservation_end_date;
|
||||
}
|
||||
|
||||
public void setReservation_end_date(Timestamp reservation_end_date) {
|
||||
public void setReservation_end_date(ZonedDateTime reservation_end_date) {
|
||||
this.reservation_end_date = reservation_end_date;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user