Conditional-Or: ||

Don't understand O/P can anyone explain, please?

public class Reservation {
  int guestCount;
  int restaurantCapacity;
  boolean isRestaurantOpen;
  boolean isConfirmed;
  
  public Reservation(int count, int capacity, boolean open) {
    // Write conditional statement below
    if(count<1 || count>8){
      System.out.println("Invalid reservation!");
    }
    
    guestCount = count;
		restaurantCapacity = capacity;
		isRestaurantOpen = open;
  }  
  
  public void confirmReservation() {
    if (restaurantCapacity >= guestCount && isRestaurantOpen) {
      System.out.println("Reservation confirmed");
      isConfirmed = true;
    } else {
      System.out.println("Reservation denied");
			isConfirmed = false;
    }
  }
  
  public void informUser() {
    System.out.println("Please enjoy your meal!");
  }
  
  public static void main(String[] args) {
    Reservation partyOfThree = new Reservation(0, 0, true);
    Reservation partyOfFour = new Reservation(0, 0, true);
    partyOfThree.confirmReservation();
    partyOfThree.informUser();
    partyOfFour.confirmReservation();
    partyOfFour.informUser();
  }
}

Output:
Invalid reservation!
Invalid reservation!
Reservation confirmed
Please enjoy your meal!
Reservation confirmed
Please enjoy your meal!

By O/P, I’m assuming you mean output, please correct me if I’m wrong.

We see that Invalid reservation! will be printed whenever a new Reservation object is created with a count value that is either less than 1 or greater than 8. When you create partyOfThree and partyOfFour, you pass the number 0 for the count parameter in the Reservation constructor. Since 0 < 1, Invalid reservation! is printed. This happens twice, with partyOfThree and partyOfFour.

Now that I’ve given you a walkthrough on part of the code, try doing the same for the rest to figure out why you are getting this output. I find it helpful to go line-by-line through the code in the order it would be executed. This means going to the main method, taking a look at the first line, and going to any other methods or constructors as needed if they are used in the main method.

Let me know if you have any other questions!

2 Likes

Just now completed the review section for this exercise and totally understood now.
Thank you so much! :smile:

1 Like