mardi 22 octobre 2019

JAVA BEGINNER: IF statement not working as intended - body of code not being called when it should?

A couple weeks new at coding! I've been assigned to make a method where the user will be able to book an 'appointment' between 9am to 5pm. I have made an boolean Array of size 8 named availability (0 = 9am, 1 = 10am, 2 = 11am, 3 = 12am ... 8 = 17pm.) True means the time is available, false means the time is not available.

The method has the params of description, hour and duration. My method IS able to book an appointment at a chosen time.

However, lets say, I booked an appointment at 9am with a duration of 1. --> 0 is now false. Next, I booked an appointment at 11am with a duration of 1. --> 2 is now false. I then book an appointment at 10am with a duration of 2. --> 1 (10am) should stay true, as a user shouldn't be able to book for 2 hours if the next hour (11am) is already booked, right?

I attempted to make this occur in my code, but to no avail. My code still books 10am and sets the slot to false! From what I've gathered, I believe the issue is line 96, the if statement checking if any of the slots during the duration are false.

What I've attempted is making a boolean variable set as true at the start of the code , and if the if statement finds a false availability, set the check variable as false. This stops the next block of code, where an IF statement checks if 'check' is true (if all the slots during the duration are true, as it wasn't set false).

public boolean makeAppointment(String desc, int duration, int hour) {
  boolean check = true;
  if (!((hour + (duration - 1) > 17))) { // it'll be 10
    int booleanHour = hour - 9; // it'll be 1

    if (booleanHour <= availability.length && booleanHour >= 0 && duration > 0 && duration <= 4) { // This 4 is a placeholder, subject to change.  


      for (int i = booleanHour; i < booleanHour + duration; i++) { 
        if (availability[i] == false) { // This isn't working? Confusion. 
          System.out.println( // response to if false
            "This has hour been booked, please choose another time. We open at 9am, and close at 5pm.");
          System.out.println(check);
          check = false;
          return false;
        } 
        if (check) {
          for (int b = booleanHour; b < booleanHour + duration; b++) {
            availability[b] = false;
            Appointment appoint = new Appointment(desc, duration, b + 9);
            schedule.put(b + 9, appoint);
          }
          return true;
        }

      }
    } else {
      System.out.println("The max duration of a         meeting is 4 hours.");
    }
  }
  return false;
}

This is my test code as well.

Day day5 = new Day();
    day5.makeAppointment("hello", 1, 9);
    assertEquals(day5.getAvailability(0), false);

    day5.makeAppointment("hello", 1, 11);
    assertEquals(day5.getAvailability(2), false);

    day5.makeAppointment("hello", 2, 10);
    assertEquals(day5.getAvailability(1), true);

I have been trying to find the issue and fix it without seeking outside support, but it's come to the point where staring at a computer screen isn't a good way of learning, and having someone who can point out an issue which I can learn from is the best option!

If I do get a response, thank you! :) If anything about this post isn't what is allowed, sorry, first time!

Aucun commentaire:

Enregistrer un commentaire