Advertisement
kitlolz012

bookform.java ( anti book multi multi )

Nov 23rd, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1.  private void canSubmitBooking(String userId, String hotelId, String fullName, String checkInDate, String checkOutDate,
  2.                                   int adults, int children, String contactNumber, String email, String roomPreference,
  3.                                   String yourGcashName, String yourGcashNumber, String referenceNumber) {
  4.  
  5.         DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
  6.  
  7.         DatabaseReference userBookingsRef = databaseReference.child("booking_info");
  8.         userBookingsRef.orderByChild("userId").equalTo(userId).addListenerForSingleValueEvent(new ValueEventListener() {
  9.             @Override
  10.             public void onDataChange(DataSnapshot dataSnapshot) {
  11.                 boolean canSubmit = true;
  12.                 for (DataSnapshot bookingSnapshot : dataSnapshot.getChildren()) {
  13.                     String existingCheckInDate = bookingSnapshot.child("checkInDate").getValue(String.class);
  14.                     String existingCheckOutDate = bookingSnapshot.child("checkOutDate").getValue(String.class);
  15.  
  16.                     SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
  17.                     Date currentDate = new Date();
  18.                     Date existingCheckIn = null;
  19.                     Date existingCheckOut = null;
  20.  
  21.                     try {
  22.                         existingCheckIn = sdf.parse(existingCheckInDate);
  23.                         existingCheckOut = sdf.parse(existingCheckOutDate);
  24.                     } catch (ParseException e) {
  25.                         e.printStackTrace();
  26.                     }
  27.                     if (existingCheckIn != null && existingCheckOut != null) {
  28.                         // Check if the new booking completely falls outside the range of existing booking
  29.                         if (!(existingCheckOut.before(existingCheckIn) && existingCheckIn.before(existingCheckOut) ||
  30.                                 existingCheckIn.after(existingCheckOut) && existingCheckIn.after(existingCheckIn))) {
  31.                             canSubmit = false;
  32.                             break;
  33.                         }
  34.                     }
  35.  
  36.                     if (existingCheckOut != null && currentDate.before(existingCheckOut)) {
  37.                         canSubmit = false;
  38.                         break;
  39.                     }
  40.                 }
  41.  
  42.                 if (canSubmit) {
  43.                     saveBooking(userId, hotelId, fullName, checkInDate, checkOutDate, adults, children, contactNumber, email, roomPreference, yourGcashName, yourGcashNumber, referenceNumber, timestamp);
  44.                 } else {
  45.                     Toast.makeText(BookForm.this, "You cannot submit a new booking until your current booking has ended.", Toast.LENGTH_SHORT).show();
  46.                 }
  47.             }
  48.  
  49.             @Override
  50.             public void onCancelled(DatabaseError databaseError) {
  51.                 Log.e("BookForm", "Database error: " + databaseError.getMessage());
  52.             }
  53.         });
  54.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement