A Way For Learning

Hotel Program using OOPs Concepts in Java

No comments
/*
  the Hotel is an ArrayList of Reservations
  it allows a person to request a room and cancel their reservation
  also we can add more rooms
*/

import java.util.ArrayList;

public class Hotel{
    //instance variable, ArrayList tracks current reservations
    private ArrayList<Reservation> rooms;

    //constructors, can specify how many rooms to start with
    //default is 5 rooms
    public Hotel(){
  rooms = new ArrayList<Reservation>();
  rooms.ensureCapacity(5);
  for (int i = 0; i < 5; i++)
       rooms.add(null);
    }
    public Hotel(int numRooms){
  rooms = new ArrayList();
  rooms.ensureCapacity(numRooms);
  for(int i = 0; i< numRooms; i++)
       rooms.add(null);
    }

    //adds more rooms to the hotel, returns true on success
    public boolean buildRooms(int num){
  //make sure the parameter is valid
  if(num<=0)return false;

  //increase the capacity of the Vector
  rooms.ensureCapacity(rooms.size() + num);
  for(int i = 0; i < num; i++)
       rooms.add(null);
  //report succes
  return true;
    }

    //reserves and returns an available room
    //or returns -1 if the hotel is full
    public int reserveRoom(String person) {
      boolean emptyRoom = false;
      int i = 0;
      for ( i = 0; i < rooms.size(); i++) {
            if (rooms.get(i) == null) {
                    emptyRoom = true;
                    break;
            }
        }
        if (emptyRoom == true) {
          Reservation customer = new Reservation(person,i);
         //rooms.get(i) = customer;
          rooms.set(i, customer);
            return i;
        }
        return -1;
    }

    //reserves a particular room for this person
    //returns false on failure (eg. room is already reserved)
    public boolean reserveRoom(String person, int roomNum) {
      if(rooms.get(roomNum) == null){
         Reservation customer = new Reservation(person,roomNum);
          rooms.set(roomNum, customer);
        return true;
      }
      else {
        return false;
      }
    }

    //cancels all reservations by this person
    public void cancelReservations(String person) {
      for (int i = 0; i < rooms.size(); i++){
        if (rooms.get(i)!=null){
         if (rooms.get(i).getName().compareTo(person)==0) {
           rooms.set(i, null);
          }
        }
      }
    }

    //prints out all the current reservations to the screen
    //also should display the total number of reservations and vacancies
    public void printReservations() {
      for (int i = 0; i< rooms.size(); i++ ) {
        if (rooms.get(i)!=null){
        System.out.println(rooms.get(i).getName() +"-room number->"+ rooms.get(i).getRoom());
      }
      }

    }
}
........***
/*
  Reservation class, stores the person and room number in the Hotel
*/

public class Reservation{
    //instance variables
    private String name;
    private int roomNumber;

    //constructors, must supply the name, and optionally a room
    public Reservation(String person){
        this.name = person;

    }
    public Reservation(String person, int room){
        this.name = person;
        this.roomNumber = room;
    }

    //mutators, set the room number or name
    public void setRoom(int newroom){
       roomNumber = newroom;
    }
    public void setName(String newname){
       name = newname;
    }

    //accessors, return the room number or name
    public int getRoom(){
        return  roomNumber;
    }
    public String getName(){
         return  name;
    }
}

No comments :

Post a Comment