Having some trouble with arrays for my hotel class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GesterX
    New Member
    • Nov 2007
    • 19

    #16
    No worries now guys - i had a brain wave!
    I amde a new variable of type Occupier and used that to access the Occupier class!
    Thanks anyway

    Comment

    • GesterX
      New Member
      • Nov 2007
      • 19

      #17
      Right guys i have compiled all of my classes - my next problem comes with testing one of the methods! If i wasn't such a perfectionist i could let this go but i want this to work!

      I have a method called checkIn. After creating a hotel object and some occupier objects you use the method to check occupiers into the hotel.

      The method has two parameters. group is to declare which occupier is checking in. Preference tells the hotel if the guest wants a seaview or not.

      Upon running the method here is what should happen:
      If the occupier wants a seaview they will be assigned to the first room available with a seaview.
      If all of the seaview rooms are taken they will be put in the first room available without a seaview.
      The method then returns the roomnumber the occupier was set to.
      If no vacant rooms were found the method returns -1.

      Now onto my code. here is the code for finding a vacant room.
      [code=java]
      public int findVacantRoom( )
      {
      for (int index = 0; index < Rooms.length; index++)
      if (Rooms[index].getOccupier() == null){
      return index;
      }
      int indx = -1;
      return indx;
      }
      [/code]

      Here is the code for finding a room with a seaview (only even rooms have a seaview hence the index +=2
      [code=java]
      public int findVacantSeaVi ew()
      {
      for (int index = 0; index < Rooms.length; index+=2)
      if (Rooms[index].getOccupier() == null){
      return index;
      }
      int indx = -1;
      return indx;
      }
      [/code]

      And here is the checkin method
      [code=java]
      public int checkIn(Occupie r group, boolean preference)
      {
      int rm = findVacantSeaVi ew();
      int rm2 = findVacantRoom( );
      int rm3 = findVacantRoom( );
      int rm4 = -1;

      if (preference == true){
      Rooms[rm].setOccupier(gr oup);
      FreeRooms -=1
      return rm;
      }

      else if (findVacantSeaV iew() == -1){
      Rooms[rm2].setOccupier(gr oup);
      FreeRooms -=1
      return rm2;
      }

      else if (preference == false){
      Rooms[rm3].setOccupier(gr oup);
      FreeRooms -=1
      return rm3;
      }
      else {return rm4;}
      }
      [/code]

      The problem occurs when the hotel is full or the guest wants a seaview but has to be assigned a vacant room without a seaview instead. The program is supposed to return -1 but instead crashes and brings up the error:
      java.lang.Array IndexOutOfBound sException: -1

      Any help with this will be greatly appreciated!

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #18
        It's your findVacantSeaVi ew method that's stealing the cookies.

        That index can go over the size of the array thus throwing the outofbounds thing at you.
        Shouldn't a room have a isSeaView property which you can check in your findVacantSeaVi ew method so that your loop simply has something like
        [CODE=java]if((Rooms[i].isSeaView()) && (Rooms[i].getOccupier() == null)) {
        //found it
        }[/CODE]

        Comment

        • GesterX
          New Member
          • Nov 2007
          • 19

          #19
          I inserted your code into my method and I still get the same error.
          I should have mentioned that the findVacantRoom and findVacantSeavi ew methods both work when i run them and they even return -1 if there are no rooms available.
          I think it's something to do with the checkIn method
          When the error occurs line:
          [code=java]
          Rooms[rm].setOccupier(gr oup);
          [/code]
          is highlighted in the editor.

          EDIT - is it possibly becasue the program is trying to find room -1?
          If so how would I edit it so that it isnt?

          Thanks

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #20
            Originally posted by GesterX
            I inserted your code into my method and I still get the same error.
            I should have mentioned that the findVacantRoom and findVacantSeavi ew methods both work when i run them and they even return -1 if there are no rooms available.
            I think it's something to do with the checkIn method
            When the error occurs line:
            [code=java]
            Rooms[rm].setOccupier(gr oup);
            [/code]
            is highlighted in the editor.

            EDIT - is it possibly becasue the program is trying to find room -1?
            If so how would I edit it so that it isnt?

            Thanks
            Where did you plug-in my code? It was not meant as a plug-in.
            You have to have the boolean isSeaVies property in your Room class and the getter for it for that code to work with your code.

            Comment

            • GesterX
              New Member
              • Nov 2007
              • 19

              #21
              Yes i should have made that clear.

              The room class has the method hasSeaView. The value of seaView is determined on construction of the hotel in a private method in the constructor which makes all even rooms have a seaview.
              I put your code into my findVacantSeavi ew method put used my method "hasSeaView " instead of your "isSeaView" but still got the same error.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #22
                Originally posted by GesterX
                Yes i should have made that clear.

                The room class has the method hasSeaView. The value of seaView is determined on construction of the hotel in a private method in the constructor which makes all even rooms have a seaview.
                I put your code into my findVacantSeavi ew method put used my method "hasSeaView " instead of your "isSeaView" but still got the same error.
                Erm, which same error? Can you post the code you used and the exact error that you got?

                Comment

                • GesterX
                  New Member
                  • Nov 2007
                  • 19

                  #23
                  Originally posted by r035198x
                  Erm, which same error? Can you post the code you used and the exact error that you got?
                  My code for the hotel class is:
                  [code=java]
                  public class Hotel {
                  public static final int ROOM_PRICE = 20;
                  public static final int SEAVIEW_SUPPLEM ENT = 5;
                  public static final int SINGLE_OCCUPIER _SUPPLEMENT = 5;
                  public static final int DINNER_PRICE = 9;
                  private Room[] Rooms;
                  private int Profit;
                  private int FreeRooms;

                  public Hotel(int totalRooms)
                  {
                  Profit = 0;
                  FreeRooms = totalRooms;
                  setRoom();

                  }

                  private void setRoom()
                  {
                  Rooms = new Room[FreeRooms];
                  for (int i = 0; i < Rooms.length; i++)
                  Rooms[i] = new Room(i%2 == 0);
                  }

                  public int findVacantRoom( )
                  {
                  for (int index = 0; index < Rooms.length; index++)
                  if (Rooms[index].getOccupier() == null){
                  return index;
                  }
                  return -1;
                  }

                  public int findVacantSeaVi ew()
                  {
                  for (int index = 0; index < Rooms.length; index++)
                  if((Rooms[index].hasSeaView()) && (Rooms[index].getOccupier() == null)){
                  return index;
                  }
                  return -1;
                  }

                  public int checkIn(Occupie r group, boolean preference)
                  {
                  int rm = findVacantSeaVi ew();
                  int rm2 = findVacantRoom( );
                  int rm3 = findVacantRoom( );

                  if (preference == true){
                  Rooms[rm].setOccupier(gr oup);
                  FreeRooms -=1;
                  return rm;
                  }

                  else if (findVacantSeaV iew() == -1){
                  Rooms[rm2].setOccupier(gr oup);
                  FreeRooms -=1;
                  return rm2;
                  }

                  else if (preference == false){
                  Rooms[rm3].setOccupier(gr oup);
                  FreeRooms -=1;
                  return rm3;
                  }
                  return -1;
                  }

                  public int getBill(int roomNumber)
                  {
                  int total = 0;
                  Occupier occupant = Rooms[roomNumber].getOccupier();
                  int nightsStayed = occupant.getLen gthOfStay();
                  int numberPpl = occupant.getNum InGroup();
                  if (Rooms[roomNumber].hasSeaView()== true){
                  total += nightsStayed * SEAVIEW_SUPPLEM ENT;
                  }

                  if(numberPpl == 1){
                  total += nightsStayed * SINGLE_OCCUPIER _SUPPLEMENT;
                  }

                  total += nightsStayed * numberPpl;
                  total += occupant.getNum Dinners() * DINNER_PRICE;
                  return total;
                  }

                  public void checkOut(int roomNumber)
                  {
                  Profit += getBill(roomNum ber);
                  Rooms[roomNumber].setOccupier(nu ll);
                  }

                  public void updateOvernight ()
                  {
                  int index = 0;
                  Occupier occupant = Rooms[index].getOccupier();
                  while (index < Rooms.length){
                  occupant.stayNi ght();
                  }
                  }

                  public void eatDinner(int roomNumber)
                  {
                  Occupier occupant = Rooms[roomNumber].getOccupier();
                  int dinners = occupant.getNum Dinners();
                  int numberInGroup = occupant.getNum InGroup();
                  dinners += numberInGroup;
                  dinners = occupant.getNum Dinners();
                  }

                  public int getTotalIncome( )
                  {
                  return Profit;
                  }

                  public int getNumFreeRooms ()
                  {
                  return FreeRooms;
                  }

                  }

                  [/code]

                  And the room class:
                  [code=java]
                  public class Room
                  {

                  private boolean seaview;
                  private Occupier guest;

                  /**
                  * Constructor for objects of class Room
                  */

                  public Room(boolean seaView)
                  {
                  seaview = seaView;
                  guest = null;
                  }

                  /**
                  * Set whether the room has a sea view.
                  */

                  public boolean hasSeaView()
                  {
                  return seaview;
                  }

                  /**
                  * Set the Occupier of the room
                  */

                  public void setOccupier(Occ upier newOccupier)
                  {
                  guest = newOccupier;
                  }

                  /**
                  * Return the Occupier
                  */

                  public Occupier getOccupier()
                  {
                  return guest;
                  }
                  }
                  [/code]

                  And the occupier class:
                  [code=java]
                  public class Occupier
                  {

                  private int numberInGroup;
                  private int nightsStayed;
                  private int nightsEaten;

                  /**
                  * Constructor for objects of class Occupier
                  */

                  public Occupier(int groupNumber)
                  {
                  numberInGroup = groupNumber;
                  }

                  /**
                  * Returns the number of nights the group has stayed at the hotel
                  */

                  public int getLengthOfStay ()
                  {
                  return nightsStayed;
                  }

                  /**
                  * Returns the number of people in the group
                  */

                  public int getNumInGroup()
                  {
                  return numberInGroup;
                  }

                  /**
                  * Return the number of dinners the group has eaten
                  */

                  public int getNumDinners()
                  {
                  return nightsEaten;
                  }

                  /**
                  * Add one to the number of nights the group has stayed
                  */

                  public void stayNight()
                  {
                  nightsStayed += 1;
                  }

                  /**
                  * Add one to the number of times the group has eaten dinner
                  */

                  public void eatDinner()
                  {
                  nightsEaten += 1;
                  }
                  }
                  [/code]

                  The code does compile. When every room in the hotel has an occupier the program should return -1. But instead i get the following error:
                  java.lang.Array IndexOutOfBound sException: -1

                  Line 50 of the hotel class is highlighted when this error occurs. As i said before the findVacantRoom and findVacantSeaVi ew methods work individually so the error is in the checkIn method.

                  Thanks and sorry about being unclear with anything before.

                  Comment

                  • GesterX
                    New Member
                    • Nov 2007
                    • 19

                    #24
                    Sorry if you started working on this already but i figured it out my self.
                    I modified the checkIn method to take into account when the hotel is full as follows
                    [code=java]
                    public int checkIn(Occupie r group, boolean preference)
                    {
                    int rm = findVacantSeaVi ew();
                    int rm2 = findVacantRoom( );
                    int rm3 = findVacantRoom( );

                    if ((preference == true)&&(FreeRoo ms > 0)){ // The guest wants a sea view.
                    Rooms[rm].setOccupier(gr oup); // Find a room with a sea view.
                    FreeRooms -=1; // Subtract one from the number of free rooms in the hotel
                    return rm; // return the room number
                    }

                    else if ((preference == true) && (findVacantSeaV iew() == -1) && (FreeRooms > 0)){
                    Rooms[rm2].setOccupier(gr oup);
                    FreeRooms -=1;
                    return rm2;
                    }

                    else if ((preference == false) && (FreeRooms > 0)){
                    Rooms[rm3].setOccupier(gr oup);
                    FreeRooms -=1;
                    return rm3;
                    }

                    else if ((preference == true) && (FreeRooms == 0)){
                    return -1;
                    }

                    else if (FreeRooms == 0){
                    return -1;
                    }

                    return -1;
                    }
                    [/code]

                    Thanks for your guidance.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #25
                      In future the exception trace is usually a give-away. It usually has the exact line number where the exception was thrown at.

                      Comment

                      Working...