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);
}
}
}
}