Ad
Code
Diff
  • import java.util.HashMap;
    import java.util.Map;
    
    class ReservationSystem {
      private static final int MAX_CAPACITY = 12;
      private final Map<String, Integer> availabilityByDate = new HashMap<>();
      private final Map<String, Integer> reservations = new HashMap<>();
    
      public boolean request(String date, String name, String phone, int size) {
        int available = availabilityByDate.getOrDefault(date, MAX_CAPACITY);
        if (size < 1 || available < size) {
          return false;
        }
        reservations.put(date+name+phone, size);
        availabilityByDate.put(date, available - size);
        return true;
      }
    
      public void cancel(String date, String name, String phone) {
        var size = reservations.remove(date+ name+phone);
        if (size != null) {
          availabilityByDate.computeIfPresent(date, (d, c) -> c + size);
        }
      }
    }
    • import java.util.HashMap;
    • import java.util.Map;
    • class ReservationSystem {
    • private static final int MAX_CAPACITY = 12;
    • private final Map<String, Integer> availabilityByDate = new HashMap<>();
    • private final Map<Reservation, Integer> reservations = new HashMap<>();
    • private final Map<String, Integer> reservations = new HashMap<>();
    • public boolean request(String date, String name, String phone, int size) {
    • int available = availabilityByDate.getOrDefault(date, MAX_CAPACITY);
    • if (size < 1 || available < size) {
    • return false;
    • }
    • reservations.put(new Reservation(date, name, phone), size);
    • reservations.put(date+name+phone, size);
    • availabilityByDate.put(date, available - size);
    • return true;
    • }
    • public void cancel(String date, String name, String phone) {
    • var size = reservations.remove(new Reservation(date, name, phone));
    • var size = reservations.remove(date+ name+phone);
    • if (size != null) {
    • availabilityByDate.computeIfPresent(date, (d, c) -> c + size);
    • }
    • }
    • private record Reservation(String date, String name, String phone) {}
    • }