Hotel Booking System (Bluej Java)

Use of array when add an client object to an array within a range

Page 1 of 1

7 Replies - 4167 Views - Last Post: 11 January 2011 - 02:43 PM Rate Topic: -----

#1 side21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 11-January 11

Hotel Booking System (Bluej Java)

Posted 11 January 2011 - 01:19 PM

Hi,

Sorry if this has already been stated elsewhere. Wasn't able to find it!

Right, im designing a small reservation system via java on BLUEJ. Iv so far done the following coding.

 public void addBooking(Client client, int dayNumber, int numberOfDays)
    {

            for(int index = dayNumber; index <  bookableDays.length -1 ; index++){

                bookableDays[index-1] = client;

                if(index > numberOfDays -1){
                    bookableDays[index] = null;
                }
    }



The dayNumber is assigned as the index element in the array, so the user can reserve any day (array element) which is less than 30 (array declared as 30 in the constructor). A client would like to book a property e.g booking on the 5th (passed on via dayNumber & stored in element 4) and staying for seven days (passed on via numberOfDays paramter so the clients details should increment from element 4 up to 11).

However, when i enter the numberOfDays (in the parameter of the method) the increment only stops up to the element which is the same value of the numberOfDays -1, hence, if i enter 10, the value stores up to element 9 (which I do not want it to do).

Thanks,
Side21

This post has been edited by side21: 11 January 2011 - 01:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Hotel Booking System (Bluej Java)

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Hotel Booking System (Bluej Java)

Posted 11 January 2011 - 01:33 PM

I am not sure I understand the problem, but I think it has something to do with the if condition:
if(index > numberOfDays -1){  
  bookableDays[index] = null;
}

according to that code, if the index is bigger than numberOfDays - 1 you don't book the client.
that is why the booking is stopped at numOfDays - 1.
Was This Post Helpful? 1
  • +
  • -

#3 side21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 11-January 11

Re: Hotel Booking System (Bluej Java)

Posted 11 January 2011 - 01:42 PM

View Postjapanir, on 11 January 2011 - 12:33 PM, said:

I am not sure I understand the problem, but I think it has something to do with the if condition:
if(index > numberOfDays -1){  
  bookableDays[index] = null;
}

according to that code, if the index is bigger than numberOfDays - 1 you don't book the client.
that is why the booking is stopped at numOfDays - 1.


Thanks for getting back.

If I don't put in this code, then the array will iterate through all the elements from the stated index value all the way to the length of the array (which is 29). I needed to prevent that from happening so i tried that coding. The problem is the assignment of numberOfDays. I want it to add additional days from the index value which is initially inputted but instead it assigns to an element instead of it adding the client between array (4 to 14) --- (index = 4, numberOfDays = 10) and the rest being null.
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Hotel Booking System (Bluej Java)

Posted 11 January 2011 - 01:55 PM

well but the loop iterates only as long as index < bookableDays.length -1:
index < bookableDays.length - 1

so you will never try to access an invalid index in the array.

Just a fix, the condition should be:
index < bookableDays.length

since array's indices are values between 0 and array.length - 1
so, if the array's length is 30, the valid indices would be:
0,1,2...29
if you will stop at bookableDays - 1, you will only reach 28, and miss 29.

so the method seems fine to me like so:
public void addBooking(Client client, int dayNumber, int numberOfDays)
   {
           for(int index = dayNumber; index <  bookableDays.length ; index++){
               bookableDays[index-1] = client;
           }
   }

Edit: added "}" to the code

This post has been edited by japanir: 11 January 2011 - 01:56 PM

Was This Post Helpful? 1
  • +
  • -

#5 side21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 11-January 11

Re: Hotel Booking System (Bluej Java)

Posted 11 January 2011 - 02:08 PM

View Postjapanir, on 11 January 2011 - 12:55 PM, said:

well but the loop iterates only as long as index < bookableDays.length -1:
index < bookableDays.length - 1

so you will never try to access an invalid index in the array.

Just a fix, the condition should be:
index < bookableDays.length

since array's indices are values between 0 and array.length - 1
so, if the array's length is 30, the valid indices would be:
0,1,2...29
if you will stop at bookableDays - 1, you will only reach 28, and miss 29.

so the method seems fine to me like so:
public void addBooking(Client client, int dayNumber, int numberOfDays)
   {
           for(int index = dayNumber; index <  bookableDays.length ; index++){
               bookableDays[index-1] = client;
           }
   }

Edit: added "}" to the code



This works fine if I wanted to increment the client object from the index value to the end of the array. However I'm attempting to iterate through the arrays based on the booking requirement, so it should not add the client object from the specified index value to the end of the array. It should only place the client object in the array which is given the dayNumber (index) until the number of days the client wants to stay in a room (numberOfDays). So its like adding additional days starting from the specified index value. The numberOfDay variable is not adding to the index. If input numberOfDays = 10 and I wanted book it on the 5th day ( index= 4) I want my details to be stored in arrays 4 up to 14 and NOT up to the 9th array (which is currently being stored for numberOfDays).
Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Hotel Booking System (Bluej Java)

Posted 11 January 2011 - 02:18 PM

Then it needs a small change:
public void addBooking(Client client, int dayNumber, int numberOfDays)
   {
           int lastDay = dayNumber + numberOfDays;//the last day
           //you can check here if lastDay is bigger than 30 and act accordingly.
           
           //or you can valid it in the loop
           for(int index = dayNumber; (index < lastDay)  && (index <  bookableDays.length) ; index++){
               bookableDays[index-1] = client;
           }
   }

Was This Post Helpful? 1
  • +
  • -

#7 side21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 11-January 11

Re: Hotel Booking System (Bluej Java)

Posted 11 January 2011 - 02:29 PM

View Postjapanir, on 11 January 2011 - 01:18 PM, said:

Then it needs a small change:
public void addBooking(Client client, int dayNumber, int numberOfDays)
   {
           int lastDay = dayNumber + numberOfDays;//the last day
           //you can check here if lastDay is bigger than 30 and act accordingly.
           
           //or you can valid it in the loop
           for(int index = dayNumber; (index < lastDay)  && (index <  bookableDays.length) ; index++){
               bookableDays[index-1] = client;
           }
   }


Thanks! I did create a local variable before but i did it within the For loop. No wonder it wouldn't make sense!

Thanks a ton! :D
Was This Post Helpful? 0
  • +
  • -

#8 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Hotel Booking System (Bluej Java)

Posted 11 January 2011 - 02:43 PM

No problem, glad I could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1