2025-09-15 11:08:45 -04:00
|
|
|
/**
|
|
|
|
|
* license: GPLv3
|
|
|
|
|
* lodge.reservationsystem
|
|
|
|
|
*/
|
2025-09-18 12:18:33 -04:00
|
|
|
package lodge.data;
|
2025-08-28 21:08:16 -04:00
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
2025-09-18 12:18:33 -04:00
|
|
|
import lodge.reservationsystem.IReservation;
|
|
|
|
|
|
|
|
|
|
class AccountReservationList extends ArrayList<IReservation> {
|
2025-08-28 21:08:16 -04:00
|
|
|
|
2025-08-29 10:32:10 -04:00
|
|
|
private static String reservationSerial(Reservation reservation) {
|
2025-09-10 17:54:48 -04:00
|
|
|
return String.format("R%010d", Math.abs(java.util.Objects.hash(reservation.getPhysical_address())));
|
2025-08-29 10:32:10 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-10 16:08:11 -04:00
|
|
|
@Override
|
2025-09-18 12:18:33 -04:00
|
|
|
public synchronized boolean add(final IReservation reservation) throws RuntimeException{
|
2025-09-06 19:21:01 -04:00
|
|
|
boolean result = true;
|
2025-09-10 16:27:41 -04:00
|
|
|
Reservation rsrv = this.find(reservation.getReservation_number());
|
2025-09-16 20:53:58 -04:00
|
|
|
result = rsrv == null;
|
2025-09-15 16:47:39 -04:00
|
|
|
if( !result ){
|
2025-09-13 20:33:29 -04:00
|
|
|
throw new DuplicateObjectException(String.format("Error No Dups, Reservation exists: %s.", rsrv.getReservation_number()));
|
2025-09-10 16:27:41 -04:00
|
|
|
}
|
2025-09-18 12:18:33 -04:00
|
|
|
result = ((IReservation)reservation).checkValid();
|
2025-09-15 16:47:39 -04:00
|
|
|
|
2025-09-10 16:27:41 -04:00
|
|
|
if(result){
|
2025-09-18 12:18:33 -04:00
|
|
|
((Reservation)reservation).setReservation_number(AccountReservationList.reservationSerial((Reservation)reservation));
|
|
|
|
|
((Reservation)reservation).setPrice(reservation.calculatePrice());
|
2025-09-10 16:27:41 -04:00
|
|
|
result = super.add(reservation);
|
2025-09-15 16:47:39 -04:00
|
|
|
}else{
|
2025-09-18 12:18:33 -04:00
|
|
|
throw new IllegalArgumentException(String.format("error reservation invalid: %s", ((Reservation)reservation).getReservation_number()));
|
2025-09-03 08:56:57 -04:00
|
|
|
}
|
2025-09-10 16:27:41 -04:00
|
|
|
return result;
|
2025-08-29 10:32:10 -04:00
|
|
|
}
|
2025-08-28 21:08:16 -04:00
|
|
|
|
2025-08-29 10:32:10 -04:00
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2025-09-18 12:18:33 -04:00
|
|
|
sb.append("\"reservations\":[");
|
2025-08-30 11:26:49 -04:00
|
|
|
|
2025-09-10 15:44:29 -04:00
|
|
|
for (int ix = 0; ix < this.size(); ix++) {
|
|
|
|
|
String name = this.get(ix).ReservationType() + "";
|
|
|
|
|
|
|
|
|
|
if ((this.size() - 1) == ix) {
|
|
|
|
|
sb.append(String.format("{\"%s\":{\"reservation_number\":\"%s\"}}", name,
|
|
|
|
|
this.get(ix).getReservation_number()));
|
|
|
|
|
} else {
|
|
|
|
|
sb.append(String.format("{\"%s\":{\"reservation_number\":\"%s\"}},", name,
|
|
|
|
|
this.get(ix).getReservation_number()));
|
2025-08-30 11:26:49 -04:00
|
|
|
}
|
2025-08-29 10:32:10 -04:00
|
|
|
}
|
2025-08-30 11:26:49 -04:00
|
|
|
sb.append("]");
|
2025-08-29 10:32:10 -04:00
|
|
|
return sb.toString();
|
2025-08-28 21:08:16 -04:00
|
|
|
}
|
2025-09-10 15:44:29 -04:00
|
|
|
|
|
|
|
|
public Reservation find(String reservation_number) {
|
2025-09-18 12:18:33 -04:00
|
|
|
for(IReservation rsrv: this){
|
2025-09-10 15:44:29 -04:00
|
|
|
if( rsrv.getReservation_number().compareTo(reservation_number) == 0 ){
|
2025-09-18 12:18:33 -04:00
|
|
|
return (Reservation)rsrv;
|
2025-09-10 15:44:29 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2025-09-10 16:08:11 -04:00
|
|
|
|
|
|
|
|
public void update(AccountReservationList incoming_reservation_list) {
|
2025-09-18 12:18:33 -04:00
|
|
|
for (IReservation rsrv : incoming_reservation_list) {
|
2025-09-10 16:08:11 -04:00
|
|
|
Reservation onListRsrv = find(rsrv.getReservation_number());
|
|
|
|
|
if( onListRsrv != null ){
|
2025-09-18 12:18:33 -04:00
|
|
|
onListRsrv.update((Reservation)rsrv);
|
2025-09-10 16:08:11 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-28 21:08:16 -04:00
|
|
|
}
|