FAQ: Conditional Operators - Review

I’m having trouble creating instances in main, here’s my code

Reservation burger = New Reservation(8, 12, true, true);

System.out.println(burger.restaurantCapacity);

any help would be appreciated.

You don’t need to mark isConfirmed in main() as it is classified in confirmReservation(). Your reservation should only read

Reservation burger = New Reservation(8, 12, true);

.

Hi! :wave:

I’m adding the following sample code in main():

  public static void main(String[] args) {
    // Create instances here
    Reservation individualReservation = new Reservation(1, 4, true);
    Reservation groupReservation = new Reservation(20, 8, true);
    System.out.println("For the individual reservation, status is " + individualReservation.confirmReservation());
    System.out.println(individualReservation.informUser());
  }

but I’m getting back the error:

Reservation.java:38: error: 'void' type not allowed here
    System.out.println("For the individual reservation, status is " + individualReservation.confirmReservation());
                                                                                                              ^
Reservation.java:39: error: 'void' type not allowed here
    System.out.println(individualReservation.informUser());
                                                       ^
2 errors

I’ve checked against other sample code and can’t understand what’s wrong. Why is main() complaining about a void type not allowed when neither confirmReservartion() nor informUser() accept parameters?

Thank you! :pray:

i am completely sure, but i guess what you doing is trying to print a print statement. callling individualReservation.confirmReservation() already prints out a statement but then again you are printing the print statement. maybe thats whats causing the errors.

This question if about the Review. Can someone show me what the code would look like where you create instances in the main() that run every possible conditional branch? I clicked on view solution so I could understand this, and it didn’t change the code at all it just showed the same code as before clicking view solution. Thank you for any help.

Why does the code print “Unable to confirm reservation, please contact restaurant.”
When isConfirmed is true?
I need a answer

You need to post your code. Line 27 will always default to false unless the variable is set, or it is unreachable for some reason.

Take a look at the order in which you call your functions. As @psmilliorn mentioned, the default value for a boolean is false, meaning isConfirmed is has a value of false before it is assigned a value in confirmReservation().

Yes, isConfirmed is true, but only after it is assigned that value when confirmReservation() is called. Now take a look at where you call informUser() in relation to confirmReservation(). Why might the message printed indicate that the reservation cannot be confirmed?

oh!
confirmReservation() and informUser() must switch places.
It didn’t change the variable when informUser() was invoked?

Yes!

If you walk through your code step by step, you’ll see that the value of isConfirmed was false when informUser() was called. It only becomes true when you call confirmReservation(), which happened after the call to informUser().

So, even though the final value of isConfirmed is true, its value when informUser() was called was false, meaning informUser() executed the if statement, not the else clause.

Hey Guys/Gals!

I’m brand new here to Codecademy, and I just wanted to post my snippet/screenshot of the instances I’ve created and would like some feedback on them!

So please, feel free to tell me what you think of my work, the more input, the better!! Much love and good luck out there!! :slight_smile:

1 Like

Hello, I am having some troubles with this exercise. It seems that it will always print Invalid reservation, to every instances I create.

For example:

Reservation mesa2 = new Reservation (2,10,true);
mesa2.confirmReservation();
mesa2.informUser();

It prints:

Invalid reservation!
Reservation confirmed
Please enjoy your meal!

Am I missing something? Thanks in advance for your help.

Welcome to the forums!

Can you post your full code so that we can see what’s going on? Please format it according to the guidelines in this topic.

public class Reservation {

  int guestCount;

  int restaurantCapacity;

  boolean isRestaurantOpen;

  boolean isConfirmed;

  

  public Reservation(int count, int capacity, boolean open) {

    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() {

    if (!isConfirmed) {

      System.out.println("Unable to confirm reservation, please contact restaurant.");

    } else {

      System.out.println("Please enjoy your meal!");

    }

  }

  

  public static void main(String[] args) {

    // Create instances here

    Reservation mesa2 = new Reservation (2,10,true);

    Reservation mesa12 = new Reservation (12,10,true);

    Reservation mesa4 = new Reservation (4,10,false);

    mesa2.confirmReservation();

    mesa2.informUser();

    //mesa12.confirmReservation();

    //mesa12.informUser();

    //mesa4.confirmReservation();

    //mesa4.informUser();

  }

}`Preformatted text`

Here it is. Thank you :slight_smile:

Is it because it prints for the last class (this case mesa4)?

No. If you take a look at the if statement that causes Invalid reservation! to be printed, you can see that it checks if count is either less than 1 or greater than 8.

You tried to create a reservation for 12 people on the following line.

However, this causes the if statement to evaluate to true (because 12 > 8) and therefore causes Invalid reservation! to be printed. This if statement is evaluated every time you try to create a new reservation, including the time above where you tried to make a reservation for 12 people.

I get it. But if I create all the classes I want to study and print after that, the “Invalid reservation” is the first print i get. Shouldn’t it appear only when I print for the second and third case? In my mind if I create all classes (eg: mesa2, mesa4, …) before I print the results, the message “Invalide reservation” should only appear after I get the valid results for the first class (mesa2).

public class Reservation {
  int guestCount;
  int restaurantCapacity;
  boolean isRestaurantOpen;
  boolean isConfirmed;

  public Reservation(int count, int capacity, boolean open) {
    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() {
    if (!isConfirmed) {
      System.out.println("Unable to confirm reservation, 
                          please contact restaurant.");
    } else {
      System.out.println("Please enjoy your meal!");
    }
  }

  public static void main(String[] args) {
    // Create instances here
    Reservation mesa2 = new Reservation (2,10,true);
    Reservation mesa12 = new Reservation (12,10,true);
    Reservation mesa4 = new Reservation (4,10,false);

    mesa2.confirmReservation();
    mesa2.informUser();

    //mesa12.confirmReservation();
    //mesa12.informUser();
    //mesa4.confirmReservation();
    //mesa4.informUser();
  }
}

I’ve reformatted your code here so it’s a bit easier to understand what’s going on. We know that the main method is automatically called when we run our code. That means this is the first line that is executed.

Reservation mesa2 = new Reservation (2,10,true);

We create a new instance of Reservation named mesa2. When we create this instance, the Reservation() constructor is run. if (count < 1 || count > 8) is false for this instance because count = 2 in this case. Therefore, its instance variables get assigned values and we move on to the next line of code in the main method.

The next line is as follows.

Reservation mesa12 = new Reservation (12,10,true);

This time, we are also creating another instance of Reservation, this time named mesa12. When we create this instance, the Reservation() constructor is run again. if (count < 1 || count > 8) is true this time because count = 12. Therefore, the message "Invalid reservation!" is printed out, its instance variables get assigned values, and we continue to the next line of code in the main method.


Because if (count < 1 || count > 8) is in your constructor, it runs every time a new object is created. You are creating mesa12 before you call instance methods on mesa2, that’s why you’re getting Invalid reservation! first.

1 Like

Thank you so much! I was having the same exact issue, but your suggestion helped me through.

Hey! With the full code I’ve written 3 different instances and I was wondering, for the last instance we’re Ive put false for the boolean isRestaurantOpen;

  • If I would want to print a message saying Restaurant is closed before partyOf10.informUser(); I could manually write System.out.println(“Restaurant is closed.”);
  • But if I would want this line to print automatically, I thought that I could add an additional constructor() ← I think thats the correct term? - which I did and you can see it in the code as isRestaurantOpen() but for the if-then-else statement Im getting an error message saying that } else { is an illegal start of type?

public class Reservation {
int guestCount;
int restaurantCapacity;
boolean isRestaurantOpen;
boolean isConfirmed;

public Reservation(int count, int capacity, boolean open) {
if (count < 1 || count >= 8) {
System.out.println(“Invalid reservation!”);
}
guestCount = count;
restaurantCapacity = capacity;
isRestaurantOpen = open;
}

public void isRestaurantOpen() {
if (isRestaurantOpen = true)
System.out.println(“Welcome”);
} else {
System.out.println(“Restaurant is Closed”);
}

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() {
if (!isConfirmed) {
System.out.println(“Unable to confirm reservation, please contact restaurant.”);
} else {
System.out.println(“Please enjoy your meal!”);
}
}

public static void main(String args) {
Reservation partyOfSix = new Reservation (8, 5, true);
partyOfSix.confirmReservation();
partyOfSix.informUser();

Reservation partyOf3 = new Reservation (3, 10, true);
partyOf3.confirmReservation();
partyOf3.informUser();

Reservation partyOf10 = new Reservation (10, 20, false);
partyOf10.confirmReservation();
partyOf10.informUser();

}
}

Thanks for any replies =)