473,574 Members | 3,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Having some trouble with arrays for my hotel class

19 New Member
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
Expand|Select|Wrap|Line Numbers
  1.     private Room[] Rooms;
  2.     private int Profit;
  3.     private int FreeRooms;
  4.  
The constructor is as follows:
Expand|Select|Wrap|Line Numbers
  1.  public Hotel(int totalRooms)
  2.     {
  3.         Profit = 0;
  4.         FreeRooms = totalRooms;
  5.         setRoom();
  6.     }
  7.  
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?

Expand|Select|Wrap|Line Numbers
  1.     private void setRoom()
  2.     {
  3.         Rooms = new Room[FreeRooms];
  4.         if (Rooms.SOMETHING % 2 = 0){
  5.             Room.hasSeaView = true;
  6.         }
  7.         else Room.hasSeaView = false;
  8.     }
  9.  
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.
Nov 19 '07 #1
24 4190
JosAH
11,448 Recognized Expert MVP
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)

Expand|Select|Wrap|Line Numbers
  1. =java]
  2. boolean seaView;
  3. ...
  4. public void setSeaView(boolean seaView) { this.seaView= seaView; }
  5. public boolean isSeaView() { return seaView; }
  6.  
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
Nov 19 '07 #2
GesterX
19 New Member
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.
Nov 19 '07 #3
JosAH
11,448 Recognized Expert MVP
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:

Expand|Select|Wrap|Line Numbers
  1. public class Room {
  2.    private boolean seaView; // set at construction time
  3.  
  4.    public Room(boolean seaView) {
  5.       this.seaView= seaView;
  6.       ...
  7.    }
  8.    public boolean isSeaView() { return seaView; }
  9. }
  10.  
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:

Expand|Select|Wrap|Line Numbers
  1. public class Hotel {
  2.    private Room[] rooms; // the rooms in the hotel
  3.    public Hotel() {
  4.       // first construct the rooms array and construct the rooms as follows:
  5.       for (int i= 0; i < rooms.length; i++)
  6.          rooms[i]= new Room(i%2 == 0); // sea view or not
  7.    }
  8.    ...
  9. }
kind regards,

Jos
Nov 19 '07 #4
GesterX
19 New Member
Cheers for clearing that up. I have it now and it works.
Thanks for your help Jos!
Nov 19 '07 #5
JosAH
11,448 Recognized Expert MVP
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
Nov 19 '07 #6
GesterX
19 New Member
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
Expand|Select|Wrap|Line Numbers
  1.     public int findVacantRoom()
  2.     {
  3.     int index = 0;
  4.     while(index < Rooms.length) {
  5.         if (Room.getOccupier() = null) {
  6.             return index; }
  7.         if (Room.getOccupier() = !null) { 
  8.             index ++; }
  9.         }
  10.         return -1;
  11.     }
  12.  
The return -1 is for when the hotel is full.

And here is the (simple) code for the getOccupier method in the Room class
Expand|Select|Wrap|Line Numbers
  1.     public Occupier getOccupier()
  2.     {
  3.         return guest;
  4.     }
  5.  
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.
Nov 19 '07 #7
r035198x
13,262 MVP
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
Expand|Select|Wrap|Line Numbers
  1.     public int findVacantRoom()
  2.     {
  3.     int index = 0;
  4.     while(index < Rooms.length) {
  5.         if (Room.getOccupier() = null) {
  6.             return index; }
  7.         if (Room.getOccupier() = !null) { 
  8.             index ++; }
  9.         }
  10.         return -1;
  11.     }
  12.  
The return -1 is for when the hotel is full.

And here is the (simple) code for the getOccupier method in the Room class
Expand|Select|Wrap|Line Numbers
  1.     public Occupier getOccupier()
  2.     {
  3.         return guest;
  4.     }
  5.  
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.
Nov 19 '07 #8
GesterX
19 New Member
Oh i see - so the problem is that I'm not referencing index to a room object in the array then?
Nov 19 '07 #9
r035198x
13,262 MVP
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.
Nov 19 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3300
by: Chris | last post by:
I'm wondering what is the "best" way to structure a set of info about an item, like a book or a hotel. Eg, let's say you had this info to present for each book in a collection: Title Author Publisher Edition info etc
27
2599
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this to the function: example = new Array("A",2); example = new Array("Q",4); function loopIt(example);
4
4020
by: DaHool | last post by:
Hi there !!! I browsed around the Internet in search for a solution of a little difficult problem i have in VB.NET.... However, i cannot find a suitable anwser anywhere, so i thought i'll give it a try here... Okay, here's the deal: I am trying to read from unmanaged memory with a class type struct, this
9
7322
suzee_q00
by: suzee_q00 | last post by:
I will admit that lots of times the obvious eludes me and this is most likely one of those times but if anyone has any ideas on what I can do to make this code work, I would greatly appreciate it. Here's the code: while ( ( fgets ( buf, BUFLEN, fin ) ) != NULL ) /* pulls a line from "fin" up to the length of BUFLEN and stores it in "buf" */ {...
0
1347
by: melledge | last post by:
IDEAlliance and the XML 2006 Planning Committee have just added an informative and fast-paced Vendor PechaKucha Night to the conference agenda. The session takes place Tuesday evening, December 5 from 7:00 - 9:00 pm. XML 2006 takes place December 4-7 at the Sheraton Boston Hotel. In this lightning round session, vendors are allowed 20...
1
2437
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are of different lengths (e.g. usually between 25 and 45 rows. The columns in each file have the same length). The text files have been numbered...
1
1359
by: Mike Kent | last post by:
The APL 2007 conference, sponsored by ACM SIGAPL, has as its principal theme "Arrays and Objects" and, appropriately, is co-located with OOPSLA 2007, in Montreal this October. APL 2007 starts with a tutorial day on Sunday, October 21, followed by a two-day program on Monday and Tuesday, October 22 and 23. APLers are welcome to attend...
5
1430
by: polas | last post by:
Afternoon everyone. I am having some trouble and thought a few of you might be able to help me... I have a simple function to copy the contents of one array into another - the arguments to the function are the first array (to copy into), the second array (copy from) and the number of items to copy, the prototype is void cpyarray (float * a,...
2
2280
by: virtualweb | last post by:
Hello: Im working on a Hotel Reservtion script and ran into a little problem: I have 3 types of rooms, 1 is Single, 2 is Double, 3 is Suite for this reason, $totaltypes = '3'; (in the snippet below). I have a form with checkboxes in order to reserve rooms, all rooms that are checked are saved in the array @All_Rooms as detailed below: ...
0
7841
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8100
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7856
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8137
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6509
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5335
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3773
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1099
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.