Files
reservationsystem/src/java/lodge/reservationsystem/AccountReservationList.java

66 lines
2.1 KiB
Java
Raw Normal View History

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
}
public boolean add(final Reservation reservation) {
2025-09-06 19:21:01 -04:00
boolean result = true;
try {
2025-09-10 15:44:29 -04:00
result = reservation.checkValid();
2025-09-06 19:21:01 -04:00
} catch (Exception e) {
return false;
2025-09-10 15:44:29 -04:00
}
for (Reservation rsrv : this) {
2025-09-06 20:22:27 -04:00
result = reservation.getReservation_number().compareTo(rsrv.getReservation_number()) == 0;
2025-09-10 15:44:29 -04:00
if (result) {
2025-09-06 20:22:27 -04:00
return false;
}
2025-09-03 08:56:57 -04:00
}
2025-09-10 15:44:29 -04:00
reservation.setReservation_number(AccountReservationList.reservationSerial(reservation));
2025-09-05 10:26:49 -04:00
return super.add(reservation);
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 void update(AccountReservationList incoming_reservation_list) {
for (Reservation rsrv : incoming_reservation_list) {
Reservation onListRsrv = find(rsrv.getReservation_number());
if( onListRsrv != null ){
onListRsrv.update(rsrv);
}
}
}
public Reservation find(String reservation_number) {
for(Reservation rsrv: this){
if( rsrv.getReservation_number().compareTo(reservation_number) == 0 ){
return rsrv;
}
}
return null;
}
2025-08-28 21:08:16 -04:00
}