2025-08-28 21:08:16 -04:00
|
|
|
package lodge.reservationsystem;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
2025-08-29 10:32:10 -04:00
|
|
|
public class AccountReservationList extends ArrayList<Reservation> {
|
2025-08-28 21:08:16 -04:00
|
|
|
|
2025-08-29 10:32:10 -04:00
|
|
|
private static String reservationSerial(Reservation reservation) {
|
2025-09-03 08:56:57 -04:00
|
|
|
return String.format("R%010d", Math.abs(java.util.Objects.hash(reservation.physical_address)));
|
2025-08-29 10:32:10 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-10 16:08:11 -04:00
|
|
|
@Override
|
2025-08-29 10:32:10 -04:00
|
|
|
public boolean add(final Reservation reservation) {
|
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());
|
|
|
|
|
if( rsrv != null ){
|
|
|
|
|
result = false;
|
|
|
|
|
throw new DuplicateObjectException(String.format("Error Duplicate: Reservation %s", rsrv.getReservation_number()));
|
|
|
|
|
}
|
2025-09-06 19:21:01 -04:00
|
|
|
try {
|
2025-09-10 15:44:29 -04:00
|
|
|
result = reservation.checkValid();
|
2025-09-10 16:27:41 -04:00
|
|
|
} catch (ReservationException ex) {
|
2025-09-06 19:21:01 -04:00
|
|
|
return false;
|
2025-09-10 15:44:29 -04:00
|
|
|
}
|
2025-09-10 16:27:41 -04:00
|
|
|
if(result){
|
|
|
|
|
reservation.setReservation_number(AccountReservationList.reservationSerial(reservation));
|
|
|
|
|
result = super.add(reservation);
|
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-08-31 18:59:49 -04:00
|
|
|
sb.append("\"reservation_list\":[");
|
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) {
|
|
|
|
|
for(Reservation rsrv: this){
|
|
|
|
|
if( rsrv.getReservation_number().compareTo(reservation_number) == 0 ){
|
|
|
|
|
return rsrv;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2025-09-10 16:08:11 -04:00
|
|
|
|
|
|
|
|
public void update(AccountReservationList incoming_reservation_list) {
|
|
|
|
|
for (Reservation rsrv : incoming_reservation_list) {
|
|
|
|
|
Reservation onListRsrv = find(rsrv.getReservation_number());
|
|
|
|
|
if( onListRsrv != null ){
|
|
|
|
|
onListRsrv.update(rsrv);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-28 21:08:16 -04:00
|
|
|
}
|