0

I'm trying to build a method that checks wether a room is already booked at a certain time and if it is, it's supposed to tell the user that the room is already booked. The problem is that the method ALWAYS removes a room that has a booking, even if the room is free at the selected time. So if I book the room from 2pm to 3pm and then try to book it from 4pm to 5pm it still removes the room from my available rooms. Somehow my if statement seems to be always true even though it shouldn't be.

Explanation: The user is supposed to enter the date via console (I'm still a beginner and this is a project for college, I'm sorry if there are any studid mistakes, were not using a database yet either). The dates get saved in dateB (beginning of booking) and dateE(end). I'm saving all my rooms which have been created before in an ArrayList and then save them in a second one (availabeRooms) so that I can remove the ones that have already been booked at the requested time. This all works fine except for the if statement which is always true for some reason. It's supposed to remove only the rooms which have been booked at this specific time already.

    Date dateB = null;
    Date dateE = null;
    System.out.println("Please enter the beginning of your booking (yyyy-MM-dd HH:mm): ");
    String enter = sc.nextLine();
    try
    {
        dateB = f.parse(enter);
    }catch(
    ParseException e)
    {
        e.printStackTrace();
    }
    System.out.println("Please enter the end of your booking: ");
    try
    {
        dateE = f.parse(sc.nextLine());
    }catch(
    ParseException e)
    {
        e.printStackTrace();
    }
     ArrayList<Room> availabeRooms = new ArrayList<Room>(); 
        for(Room r : rc.getRoomContainer()){
            availableRooms.add(r);
        }   
        for (Booking bk : this.bookings) {
            if (!dateB.before(bk.getBeginning()) && !dateE.before(bk.getBeginning()) 
                || !dateB.after(bk.getEnd()) && !dateE.after(bk.getEnd())) {
                for (Room r : rc.getRoomContainer()) {
                    if (bk.getRoom().equals(r)) {
                        availableRooms.remove(r);
                }
            }

        }
    }
Rugo
  • 349
  • 1
  • 6
  • 14

2 Answers2

0

You have your if conditions all wrong, for instance if a room is booked for today and I ask for a room tomorrow then the first part of the condition !dateB.before(bk.getBeginning()) && !dateE.before(bk.getBeginning() will return true which is not what you want. Using negations often makes things harder

Look at it this way, a room is not available if the requested date time (dateB-DateE) somehow overlaps an existing booking. So I would create a private method that takes a date and a booking and returns true if the date is between getBeginning() and getEnd() and then call that method in the if (...) clause for both dateB and dateE to determine if a booking exists for that time period.

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
0

Supposing the intended logic is to remove any room from availableRooms, if Booking date interval overlaps with dateB, dateE interval, the if block currently has wrong logic.

I assumed that end dates are always larger than or equal to begin dates. To check for two intervals that overlap, this approach can be used (also, the redundant inner for cycle can be removed):

    for (Booking bk : this.bookings) {
        if (!dateB.after(bk.getEnd()) && !dateE.before(bk.getBeginning())) {
            availableRooms.remove(bk.getRoom());
        }
    }

Credits: I used the short solution from this answer.

Edvins
  • 406
  • 5
  • 9