Having some trouble with arrays for my hotel class

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

    Having some trouble with arrays for my hotel class

    First of all I'm new to this site but it certainly looks like a place that i will be visiting more often!

    Onto my problem.
    I am creating a Hotel Bussiness project in java using BlueJ

    The classes are as follows:
    Hotel - Deals with the major functions of the hotel (i.e guests booking in, finding rooms etc.)
    Room - Used to decide whether a room has a sea view and stores the guest that is in the room.
    Occupier - For information about the group occupying a room with methods for amount of nights stayed and nights eaten dinner.


    I have completed the Room and Occupier classes with no trouble at all.
    My downfall is creating the hotel class.
    The class has 3 fields so far they are
    Code:
        private Room[] Rooms;
        private int Profit;
        private int FreeRooms;
    The constructor is as follows:
    Code:
     public Hotel(int totalRooms)
        {
            Profit = 0;
            FreeRooms = totalRooms;
            setRoom();
        }
    And here is the problem, the setRoom is a private method used to declare each entry in the array as a new room object. Even numbers having a sea view. The part labelled as SOMETHING is where i wish to step into the array and set each room object as sea view or no seaview. The problem is i just don't know how to put this into code. Here is my attempt althogh after much brain ache i am starting to think using a while or for loop would work?

    Code:
        private void setRoom()
        {
            Rooms = new Room[FreeRooms];
            if (Rooms.SOMETHING % 2 = 0){
                Room.hasSeaView = true;
            }
            else Room.hasSeaView = false;
        }
    Also note that the hasSeaView method has already been set in the Room class.
    I have reffered to all of my work books and scoured the interweb but just can't find a decent example to see what to put here.

    Thanks in advance.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by GesterX
    Also note that the hasSeaView method has already been set in the Room class.
    I have reffered to all of my work books and scoured the interweb but just can't find a decent example to see what to put here.

    Thanks in advance.
    Note that a Room doesn't know where it is stored in your hotel array; you have
    to help it a bit w.r.t. having a sea view or not. Suppose a Room class has the
    following two methods (a getter and a setter for a boolean variable)

    Code:
    =java]
    boolean seaView;
    ...
    public void setSeaView(boolean seaView) { this.seaView= seaView; }
    public boolean isSeaView() { return seaView; }
    We could argue whether or not 'hasSeaView' would be a better name for the
    second method but lets conform to Sun's naming convention for now.

    In the hotel you want to set the even rooms to have a sea view; the odd numbered
    rooms are located at the other side of the hotel. When you 'build' your hotel you
    have to do something like this in your Hotel class:

    1) walk over the 'rooms' array
    2) set the even numbered rooms to 'true' using the method above

    Alternatively you could remove the setter method in the Room class and pass
    a boolean 'seaView' to its constructor. When you build the rooms in the array
    pass the appropriate boolean value to the constructor when you 'new' a Room.

    kind regards,

    Jos

    Comment

    • GesterX
      New Member
      • Nov 2007
      • 19

      #3
      Thanks for the help so far but the part I'm really stuck on is telling the Rooms in the array where abouts in the array they are so they can decide whether they have a seaview or not. But since each entry in the array is declared as a room object i can't directly number them.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by GesterX
        Thanks for the help so far but the part I'm really stuck on is telling the Rooms in the array where abouts in the array they are so they can decide whether they have a seaview or not. But since each entry in the array is declared as a room object i can't directly number them.
        Ok, second try; lets explain the alternative way (see my previous reply).
        Here's the relevant part of a Room class:

        [code=java]
        public class Room {
        private boolean seaView; // set at construction time

        public Room(boolean seaView) {
        this.seaView= seaView;
        ...
        }
        public boolean isSeaView() { return seaView; }
        }
        [/code]

        A Room constructor needs to know whether or not the Room has a sea view.
        You can ask a room whether or not it has a sea view; you can't change it anymore.

        In your Hotel class, when you build it, do this:

        [code=java]
        public class Hotel {
        private Room[] rooms; // the rooms in the hotel
        public Hotel() {
        // first construct the rooms array and construct the rooms as follows:
        for (int i= 0; i < rooms.length; i++)
        rooms[i]= new Room(i%2 == 0); // sea view or not
        }
        ...
        }[/code]

        kind regards,

        Jos

        Comment

        • GesterX
          New Member
          • Nov 2007
          • 19

          #5
          Cheers for clearing that up. I have it now and it works.
          Thanks for your help Jos!

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by GesterX
            Cheers for clearing that up. I have it now and it works.
            Thanks for your help Jos!
            You're welcome of course.

            kind regards,

            Jos

            Comment

            • GesterX
              New Member
              • Nov 2007
              • 19

              #7
              Sorry to constantly ask for help but I've hit another wall.

              I'm trying to create a method that will find the first vacant room in the hotel (i.e the first entry in the array that has the value null)

              Here is my code
              [code=java]
              public int findVacantRoom( )
              {
              int index = 0;
              while(index < Rooms.length) {
              if (Room.getOccupi er() = null) {
              return index; }
              if (Room.getOccupi er() = !null) {
              index ++; }
              }
              return -1;
              }
              [/code]

              The return -1 is for when the hotel is full.

              And here is the (simple) code for the getOccupier method in the Room class
              [code=java]
              public Occupier getOccupier()
              {
              return guest;
              }
              [/code]

              When i try to compile this i get the error message:
              non-static method getOccupier() cannot be referenced to a static context.

              I have no idea what I'm doing wrong as I have done this before in past projects and had no difficulty what so ever.

              Thanks again.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by GesterX
                Sorry to constantly ask for help but I've hit another wall.

                I'm trying to create a method that will find the first vacant room in the hotel (i.e the first entry in the array that has the value null)

                Here is my code
                [code=java]
                public int findVacantRoom( )
                {
                int index = 0;
                while(index < Rooms.length) {
                if (Room.getOccupi er() = null) {
                return index; }
                if (Room.getOccupi er() = !null) {
                index ++; }
                }
                return -1;
                }
                [/code]

                The return -1 is for when the hotel is full.

                And here is the (simple) code for the getOccupier method in the Room class
                [code=java]
                public Occupier getOccupier()
                {
                return guest;
                }
                [/code]

                When i try to compile this i get the error message:
                non-static method getOccupier() cannot be referenced to a static context.

                I have no idea what I'm doing wrong as I have done this before in past projects and had no difficulty what so ever.

                Thanks again.
                You have to be refering to a particular room don't you?
                You have to go through the array and get the Room objects first, then you can call the getOccupier method on a room.

                Comment

                • GesterX
                  New Member
                  • Nov 2007
                  • 19

                  #9
                  Oh i see - so the problem is that I'm not referencing index to a room object in the array then?

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by GesterX
                    Oh i see - so the problem is that I'm not referencing index to a room object in the array then?
                    No you are not.

                    Comment

                    • GesterX
                      New Member
                      • Nov 2007
                      • 19

                      #11
                      Originally posted by r035198x
                      No you are not.
                      Alright thanks for pointing that out - I've got 7 or 8 more methods lined up for this bad boy and I'm bound to get stuck again!
                      All good practice for next weeks assessment though!

                      Thank you guys

                      Comment

                      • GesterX
                        New Member
                        • Nov 2007
                        • 19

                        #12
                        Sorry to pester you guys again but I'm still stuck on this findVacantRoom method. I just can't figure out how to reference the index variable to the room number.

                        Here is my attempted code
                        [code=java]
                        public int findVacantRoom( )
                        {
                        for (int index = 0; index < Rooms.length; index++)
                        if (Room.getOccupi er() = null){
                        return index;
                        }
                        }
                        [/code]

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by GesterX
                          Sorry to pester you guys again but I'm still stuck on this findVacantRoom method. I just can't figure out how to reference the index variable to the room number.

                          Here is my attempted code
                          [code=java]
                          public int findVacantRoom( )
                          {
                          for (int index = 0; index < Rooms.length; index++)
                          if (Room.getOccupi er() = null){
                          return index;
                          }
                          }
                          [/code]
                          If Rooms is an array of room objects, then
                          Rooms[0] refers to the first room object in the array (if it's not null)
                          and Rooms[1] refers to the second room object .... and Room[i] refers to the (i+1)th room in the array.

                          Comment

                          • GesterX
                            New Member
                            • Nov 2007
                            • 19

                            #14
                            okay - thankyou very much!

                            Comment

                            • GesterX
                              New Member
                              • Nov 2007
                              • 19

                              #15
                              Back again!

                              I've come to another problem when trying to compute the bill.

                              First of all here is my code:
                              [code=java]
                              public int getBill(int roomNumber)
                              {
                              int total = 0;
                              int nightsStayed = Occupier.getLen gthOfStay();
                              int numberPpl = Occupier.getNum InGroup();
                              if (Rooms[roomNumber].hasSeaView()= true){
                              total = nightsStayed * SEAVIEW_SUPPLEM ENT;
                              }

                              if(numberPpl = 1){
                              total += nightsStayed * SINGLE_OCCUPIER _SUPPLEMENT;
                              }
                              total += nightsStayed * numberPpl;
                              return total;
                              }
                              [/code]

                              The problem i have is not with the algorithm but an error i am getting on line 4.
                              It's the static/ non-static error again! I know why i am getting this - it's because I'm am not reffering to a specific occupier.

                              I just don't know how to refer to a specific occupier within the array. If you need any more background info or more parts of code to help me with this just say. After this method every method i have lined up is just rounding up the project.

                              Thanks for all of your help guys it's greatly appreciated. I already understand alot more about array objects after being on this site for 24 hours than i have from weeks of lectures.

                              Comment

                              Working...