0

So, there is a class Hotel, that contains 20 Rooms in form of a matrix 4x5 (4 floors, 5 rooms on every floor). The class Room has the properties:

floorNumber(int),  
roomNumber(int),  
numberOfBeds(int),  
occupation(boolean)
etc.

For occupation, true means busy, and false means free.

One of methods I have to implement in Hotel is the one that reserves a room

reserve(int floorNumber, int roomNumber) 

This method should check if occupation is true or false.
If it is true, then reservation fails, and if it is false, I should set occupation to true, with method

setOccupation(boolean t).  

Also, method reserve return boolean (true or false), depending on whether reservation succeeded or not. In that method, you guess, is problem with scope of one variable. So there it is:

public boolean reserve(int floorNumber, int roomNumber){
    boolean flag = false;
    for ( int i = 0; i < 5; i++){
      if(rooms[floorNumber][i].getRoomNumber() == roomNumber){//every element in matrix rooms has this property: rooms[floorNumber][some_number_from_1_to_5]
        if (rooms[floorNumber][i].getOccupancy() == false){
          rooms[floorNumber][i].setOccupancy(true);
          flag = true;
        }
        else
          flag = false;
      }
    }
    return flag;
  }

The problem is, when I set (in first line) flag to true, function returns true, and when I set flag to false, function returns false.
The reason I have to assign some value to flag in first line is because compiler shows:

Error: variable flag might not have been initialized

So, the problem is that it seems like method never executes code with for loop.
I know that variables defined in loop don't exist outside loop, but those defined outside should change their values in loop. Like in this question here: Java - Access variable inside and outside of for-loop

Community
  • 1
  • 1
misty
  • 123
  • 13
  • 3
    Your code doesn't compile. `boolean flag false;` is invalid. Fix that statement, and recompile. – JB Nizet Apr 03 '16 at 18:35
  • Sorry, I edited queston, it does compile now. Originally, it says `boolean flag = false;` – misty Apr 03 '16 at 18:37
  • Why are you doing a loop here anyway? It is a 4 x 5 array. You know the floor and room number (e.g., 2, 3). Get the room from the rooms array directly (e.g., Room room = rooms[2][3]). – KevinO Apr 03 '16 at 18:38
  • @RealSkeptic you're right. I misread the question. Sorry. – JB Nizet Apr 03 '16 at 18:40
  • 1
    @LazyLady use your debugger, and execute your code step by step. If it never changes the flag, there is a simple explanation: the expression `rooms[floorNumber][i].getRoomNumber() == roomNumber` is never true. – JB Nizet Apr 03 '16 at 18:42
  • @KevinO Rooms can't have same numbers on every floor, so number in another brackets is not room number. – misty Apr 03 '16 at 18:45
  • @JB Nizet I will try that. – misty Apr 03 '16 at 18:48

2 Answers2

1

There is a simpler way to accomplish what you want to do. You don't need a boolean flag at all; you can just return true immediately on success or return false if the entire loop executed without finding a room.

public boolean reserve(int floorNumber, int roomNumber){
  for (int i = 0; i < 5; i++) {
    //every element in matrix rooms has this property:
    //rooms[floorNumber][some_number_from_1_to_5]
    if (rooms[floorNumber][i].getRoomNumber() == roomNumber){
      if (rooms[floorNumber][i].getOccupancy() == false){
        rooms[floorNumber][i].setOccupancy(true);
        return true;
      }
    }
  }
  return false;
}

But if you insist on applying your original approach that uses a flag, then: First give it a value of false (in case no room succeeded). When we find an unoccupied room (successful), set it to true. If we find an occupied room, don't touch the flag value.

public boolean reserve(int floorNumber, int roomNumber){
  boolean flag = false;
  for (int i = 0; i < 5; i++) {
    //every element in matrix rooms has this property:
    //rooms[floorNumber][some_number_from_1_to_5]
    if (rooms[floorNumber][i].getRoomNumber() == roomNumber){
      if (rooms[floorNumber][i].getOccupancy() == false){
        rooms[floorNumber][i].setOccupancy(true);
        flag = true;
      } // else DO NOTHING
    }
  }
  return flag;
}
Nayuki
  • 17,911
  • 6
  • 53
  • 80
1

I found what the problem was.
It was actually index floorNumber in matrix rooms[floorNumber][] that goes from 0 to 3 (there are 4 floors), of course.
But in real life, floor numbers go from 1, and I passed argument to

reserve(int floorNumber,int roomNumber)

without considering that.
So, I just decremented floorNumber by 1 in body of method, and it works now.

misty
  • 123
  • 13