Shields92
Shields92

Reputation: 13

Boolean with multiple classes

I am trying to take a 3 class program and pass a boolean for reserving a room. I have driver program, building, room programs. I set the reserve to false and I can't figure out how to print out a text statement when it's already set to true. I think I am either doing the passing of the boolean through the classes from the driver wrong or missing something. I have played with reserveRoom in building class with an if statement to see if it's already true to print a statement and no matter which way I go it doesn't work.

Any help would be appreciated. Thank you.

From my driver program that sends the boolean to the building program

  System.out.print ("Which room would you like to reserve?");
  System.out.print (building);
  System.out.print ("reserve: ");
  reservNum = input.nextInt();

  building.reserveRoom(reserve, reservNum);

From my building class.

public void reserveRoom (boolean reserve, int count)
{
    //class constant

    //class variables

    /*****************************************************/

    room [count].updateReserve(reserve);

} // end 

From the room class.

    public void updateReserve(boolean newReserve)
{
    //class constant

    //class variables

    /*****************************************************/


    if (newReserve == false)
    {
        roomAvail = true;
    }
    else
    {
        roomAvail = false;
    }
} // END 

Upvotes: 0

Views: 420

Answers (2)

Swapnil Sawe
Swapnil Sawe

Reputation: 11

With whatever i could understand about your question this is what i came up with -

    public class Reservation {

    public static void main(String args[]) {
        Building building = new Building(20);
        boolean reserve= false;
         
        System.out.println("Which room would you like to reserve?");
  System.out.println(building);
  System.out.println("reserve: ");
  int reservNum = 2;

  building.reserveRoom(reserve, reservNum);
  System.out.println("Is Reserved?:"+building.getRoom(reservNum).getRoomAvail());
    }
    
}

class Building {
    
    Room room[];
    public Building(int numOfRooms) {
        room = new Room[numOfRooms];
        for(int i=0; i<numOfRooms; i++) {
            room[i] = new Room();
        }
    }
    public String toString() {
        return "This Building has "+room.length+"rooms";
    }
    public Room getRoom(int roomNum){
        return room[roomNum];
    }
public void reserveRoom (boolean reserve, int count)
{
    //class constant

    //class variables

    /*****************************************************/

    room [count].updateReserve(reserve);

} // end
}

class Room {
    boolean roomAvail;
    
    public boolean getRoomAvail() {
        return roomAvail;
    }
public void updateReserve(boolean newReserve)
{
    //class constant

    //class variables

    /*****************************************************/


    if (newReserve == false)
    {
        roomAvail = true;
    }
    else
    {
        roomAvail = false;
    }
} // END 
}

Upvotes: 0

SnowmanXL
SnowmanXL

Reputation: 844

Well, there is some information missing in your question, however it think you are looking for:

public void updateReserve(boolean newReserve) {
  if(newReserve && !roomAvail) {
    System.out.println("Sorry this room is taken")
 } else {
    roomAvail = !newReserve;
 }
}

Upvotes: 1

Related Questions