简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM