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

73 lines
2.5 KiB
Java
Raw Normal View History

2025-09-15 11:08:45 -04:00
/**
* license: GPLv3
* lodge.reservationsystem
*/
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-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-15 16:47:39 -04:00
public synchronized boolean add(final Reservation 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-15 16:47:39 -04:00
result = rsrv == null;
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-15 16:47:39 -04:00
result = reservation.checkValid();
2025-09-10 16:27:41 -04:00
if(result){
reservation.setReservation_number(AccountReservationList.reservationSerial(reservation));
2025-09-14 22:50:38 -04:00
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{
throw new IllegalArgumentException(String.format("error reservation invalid: %s", 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-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
}