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;
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
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]
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?
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.
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.
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?
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;
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( );
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 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.
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
}
Comment