title: hotel management system design in java
body: <p>I am designing a simple hotel management system in Java but I am stuck in my design.</p>
  
  <p>In my main function, I create 2 Rooms and one guest. I want the guest class to be able to <code>bookRoom</code> and for a Room to check if it <code>isAvailable</code>.</p>
  
  <p>Since I am not using any databases, I am storing my rooms in a HashMap where the key is the Room and the value is a Boolean indicating whether it's booked or not.</p>
  
  <p>How would my room class get access to that hashmap? or is that a bad way of designing/thinking about the problem?</p>
  
  <p>similarly, how would my guest class have access to <code>bookRoom</code> which should called <code>isAvailable</code> which is a Room method.</p>
  
  <p>any suggestions would be greately appreciated!</p>
  
  <p>Guest</p>
  
  <pre><code>public class Guest {
  
    String name;
    boolean checkedIn;
  
    public Guest(String name) {
      this.name = name;
    }
  
    public void checkIn() {
      setCheckedIn(true);
    }
  
    public void checkOut() {
      setCheckedIn(false);
    }
  
    public boolean getStatus() {
      return checkedIn;
    }
  
    public void setCheckedIn(boolean checkedIn) {
      this.checkedIn = checkedIn;
    }
  
    public void bookRoom(String type) {
      // check if room of type x is available
  
      // if yes, create a guest booking
  
      // if no, return
    }
  }
  
  </code></pre>
  
  <p>Room</p>
  
  <pre><code>public class Room {
  
    public enum roomType {
      ONE_BEDRM, TWO_BEDRM, THREE_BEDRM, PENTHOUSE
    }
  
    public int roomNum;
    public double price;
    public roomType type;
  
    public Room(int roomNum, double price, roomType type) {
      this.roomNum = roomNum;
      this.price = price;
      this.type = type;
    }
  
    public boolean isAvailable(Room room) {
      boolean available = availability.get(room); // problem here obviously cause hashmap is defined in main
      if (available) {
        return true;
      }
      return false;
    }
  
    public int getRoomNumber() {
      return roomNum;
    }
  }
  
  </code></pre>
  
  <p>main</p>
  
  <pre><code>    public static void main(String[] args) throws Exception {
  
          Guest mike = new Guest("mike");
          mike.checkIn();
          mike.getStatus();
  
          HashMap&lt;Room, Boolean&gt; availability = new HashMap&lt;&gt;();
  
          Room one_bedroom1 = new Room(1001, 100.00, Room.roomType.ONE_BEDRM);
          Room two_bedroom1 = new Room(2001, 200.00, Room.roomType.TWO_BEDRM);
  
          availability.put(one_bedroom1, true);
          availability.put(two_bedroom1, true);
  
      }
  
  </code></pre>
  
  <p><strong>=========================SOLUTION=========================</strong></p>
  
  <p>Thanks to everyone who helped in the comments! Here are my edits that now work as expected:</p>
  
  <p>Room</p>
  
  <pre><code>public class Room {
  
    public enum roomType {
      ONE_BEDRM, TWO_BEDRM, THREE_BEDRM, PENTHOUSE
    }
  
    private boolean available;
    private Guest occupant;
    private int roomNum;
    private double price;
    private roomType type;
  
    public Room(int roomNum, double price, roomType type) {
      this.roomNum = roomNum;
      this.price = price;
      this.type = type;
    }
  
    public boolean isAvailable() {
      return this.available;
    }
  
    public boolean getAvailable() {
      return available;
    }
  
    public void setAvailable(boolean available) {
      this.available = available;
    }
  
    public double getPrice() {
      return price;
    }
  
    public void setPrice(double price) {
      this.price = price;
    }
  
    public roomType getRoomType() {
      return type;
    }
  
    public void setRoomType(roomType type) {
      this.type = type;
    }
  
    public int getRoomNumber() {
      return roomNum;
    }
  
    public void setRoomNumber(int roomNum) {
      this.roomNum = roomNum;
    }
  
    public Guest getGuest() {
      return occupant;
    }
  
    public void setGuest(Guest occupant) {
      this.occupant = occupant;
    }
  
  }
  </code></pre>
  
  <p>Guest</p>
  
  <pre><code>public class Guest {
  
    private String name;
    private boolean checkedIn;
    private Room room;
  
    public Guest(String name) {
      this.name = name;
    }
  
    public void checkIn(Room room) {
      if (this.getStatus() == false) {
        this.setCheckedIn(true);
        this.setRoom(room);
      }
    }
  
    public void checkOut() {
      if (this.getStatus() == true) {
        this.setCheckedIn(false);
        this.setRoom(null);
      }
    }
  
    public boolean getStatus() {
      return checkedIn;
    }
  
    public String getName() {
      return name;
    }
  
    public void setName(String name) {
      this.name = name;
    }
  
    public Room getRoom() {
      return room;
    }
  
    public void setRoom(Room room) {
      this.room = room;
    }
  
    public void setCheckedIn(boolean checkedIn) {
      this.checkedIn = checkedIn;
    }
  
    public void getRoomInfo(Room room) {
      System.out.println("Room # : " + room.getRoomNumber());
      System.out.println("Room price : " + room.getPrice());
      System.out.println("Room type : " + room.getRoomType());
      System.out.println("Room occupant : " + room.getGuest().getName());
    }
  }
  </code></pre>
  
  <p>Main</p>
  
  <pre><code>    public static void main(String[] args) throws Exception {
  
          Guest mike = new Guest("mike");
  
          Room one_bedroom1 = new Room(1001, 100.00, Room.roomType.ONE_BEDRM);
          Room two_bedroom1 = new Room(2001, 200.00, Room.roomType.TWO_BEDRM);
  
          one_bedroom1.setAvailable(true);
          two_bedroom1.setAvailable(true);
  
          System.out.println(one_bedroom1.isAvailable());
  
          mike.checkIn(one_bedroom1);
          one_bedroom1.setGuest(mike);
          mike.getRoomInfo(one_bedroom1);
  
      }
  </code></pre>