I have an use case where I want to see how many slots have been used for a date in a restaurant. The slot can be deducted based either number of diners or per booking. Also I have to cater a case where a booking is made for multiple dates and then i have to consider those dates as well when checking how many slots have been used
Here's is what classes I have and the model I have created
RestaurantBookingStat - This represents a booking made for a restaurant on a date with total customers for a booking and number of days (in case someone books for multiple days - corporate booking) Not adding getter and setter to avoid verbosity
public class RestaurantBookStat {
private LocalDate startDate;
private int dinersInBooking;
private int numberOfDays;
}
SlotDeductionType: Enum represents whether to deduct based on booking or diner
public enum SlotDeductionType {
DEDUCT_PER_DINER,
DEDUCT_PER_BOOKING
}
SlotDeductionMode - Represents if I just just consider startDate or also numberOfDays to expand all possible startdate
public enum SlotDeductionMode {
DEDUCT_FROM_START_DATE_FOR_BOOKING,
DEDUCT_FROM_EACH_START_DATE_BASED_ON_NUMBER_OF_DAYS
}
I am trying to calculate slot used for a restaurant based on deduction mode and deductionType and so far this is what I have (just for showing purpose. Otherwise everything is in spring and not all in same class)
public static void main(String args[]){
RestaurantBookStat bookingStat1 = new RestaurantBookStat();
bookingStat1.setStartDate(LocalDate.of(2021, 9, 13));
bookingStat1.setDinersInBooking(2);
bookingStat1.setNumberOfDays(3);
RestaurantBookStat bookingStat2 = new RestaurantBookStat();
bookingStat2.setStartDate(LocalDate.of(2021, 9, 12));
bookingStat2.setDinersInBooking(3);
bookingStat2.setNumberOfDays(2);
RestaurantBookStat bookingStat3 = new RestaurantBookStat();
bookingStat3.setStartDate(LocalDate.of(2021, 9, 13));
bookingStat3.setDinersInBooking(1);
bookingStat3.setNumberOfDays(1);
List<RestaurantBookStat> bookingStats = List.of(bookingStat1, bookingStat2, bookingStat3);
System.out.println(getSlotUsedForEachDate(bookingStats, DEDUCT_FROM_START_DATE_FOR_BOOKING, DEDUCT_PER_DINER));
System.out.println(getSlotUsedForEachDate(bookingStats, DEDUCT_FROM_START_DATE_FOR_BOOKING, DEDUCT_PER_BOOKING));
}
public static Map<LocalDate, Long> getSlotUsedForEachDate(List<RestaurantBookStat> bookingStats, SlotDeductionMode slotDeductionMode, SlotDeductionType deductionType){
if(DEDUCT_PER_DINER == deductionType && DEDUCT_FROM_START_DATE_FOR_BOOKING == slotDeductionMode){
return bookingStats.stream()
.collect(
Collectors.groupingBy(
RestaurantBookStat::getStartDate,
Collectors.summingLong(RestaurantBookStat::getDinersInBooking)));
// Output {2021-09-13=3, 2021-09-12=3} Correct
}else if (DEDUCT_PER_BOOKING == deductionType && DEDUCT_FROM_START_DATE_FOR_BOOKING == slotDeductionMode){
return bookingStats.stream()
.collect(
Collectors.groupingBy(
RestaurantBookStat::getStartDate,
Collectors.counting()));
//Output {2021-09-13=2, 2021-09-12=1} Correct
}else if (DEDUCT_PER_DINER == deductionType && DEDUCT_FROM_EACH_START_DATE_BASED_ON_NUMBER_OF_DAYS == slotDeductionMode){
//for each date and number of days, expand the dates and create additional bookings stats for expanded dates and then use the list
//Expected Output {12-09-2021=3, 13-09-2021=6, 14-09-2021=3}
return null; //Todo
}
// not showing all other scenarios as rest are all variations of the above cases
throw new UnsupportedOperationException();
}
I have difficulty in the logic to expand dates and have them part of the slot used for each date. Any pointers will be helpful