lalalollipopman
lalalollipopman

Reputation: 1

How would I link together methods from different classes in java?

This is the code for one of the methods I wrote:

public void createReservation (String guestName, String roomType) {
  Reservation roomReservation;
  Room roomAvailability;
  roomAvailability = findAvailableRoom(hotelRooms, roomType);
  if(roomAvailability != null) {
  roomReservation = new Reservation(roomAvailability, guestName);
  }
  else {
    System.out.println("A room of this type is not available.");
  }
}

The error is coming from line 4. The error says that the symbol cannot be found. Although I think I know why, I am not entirely sure how to fix the problem.

findAvailableRoom is a method from a different class and I am trying to transfer it into this class. I thought that writing it like that would suffice but it doesn't seem to have worked.

This is the method that I am referring to. It is in a different class.

public Room findAvailableRoom (Room [] roomList, String desiredType) {
    for (int i = 0; i < roomList.length; i++) {
      if (roomList[i].getAvailability() == true && roomList[i].getType() == desiredType) {
      return roomList[i];
      }
  }
    return null;
  }

Any help?

Upvotes: 0

Views: 54

Answers (1)

Przemysław Moskal
Przemysław Moskal

Reputation: 3609

To achieve what you're asking, you need an instance of the object of which field is needed. Another possibility is to make that field static.

Upvotes: 1

Related Questions