Conditional-And: &&

In the confirmReservation() method in if else block code why we are declaring isConfirmed = true and isConfirmed = false ?

if I commented both I am getting the same expected output but didn't understand why we are declaring those?

public class Reservation {
  int guestCount;
  int restaurantCapacity;
  boolean isRestaurantOpen;
  boolean isConfirmed;
  
  public Reservation(int count, int capacity, boolean open) {
    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(3, 12, true);
    Reservation partyOfFour = new Reservation(4, 3, true);
    partyOfThree.confirmReservation();
    partyOfThree.informUser();
    partyOfFour.confirmReservation();
    partyOfFour.informUser();
  }
}

Well, if the restaurant is open, and you can fit people inside it, then the reservation isConfirmed. If either of those isn’t true, isConfirmed is false.

But what is happening when I commented those two ? means I am getting the same output when I commented.

Well yes, because setting isConfirmed doesn’t actually affect any of the code. But if you to build a more advanced version of this code, then it might.

1 Like