Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Search A Vector

 
Ross McManus
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a Hotel Booking System.

On my Assignment sheet on the Button Events part it says.

" If the source of a button event is 'BOOK SINGLE ROOM' or 'BOOK DOUBLE ROOM' the Vector of Rooms should be searched, over the range for bedrooms, to ensure that there is a vacant room of the requested type: otherwise a suitable message is displayed stating that there are no rooms available."

" If there is a vacant room (ie the getCustomer() method returns null) then the user is requested to input the name of the customer, and the room is booked using the bookRoom(String) method. The JLabel associated with the room is to show the text 'OCCUPIED'"

My question is how do I search a vector to see if there is a vacant room?

The code I have for the Hotel GUI class where I have created the Vector and added the Bedroom & MeetingRooms to it is


And the code that I have for the Room & Bedroom class's is


[edit]Please avoid very long lines; they make the code hard to read. I have split the longest line, and you can see how it ought to be done. CR[/edit]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you can use a "for" loop to visit each room in the Vector "hotel", and call getCustomer() on it to see if it returns null.
 
Ross McManus
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

So that would go in the mouse clicked event?


 
Campbell Ritchie
Sheriff
Pie
Posts: 48985
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have panels called bedroom101, bedroom102, etc (or similar). Why can't you write a loop which will initialise them, and associate them with a Bedroom object? You can even set up a Map<Bedroom, Panel> which will allow you to find the panel from the associated Bedroom.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ross McManus wrote:Thanks.

So that would go in the mouse clicked event?


Yes. Personally, I'd put the code that actually looped over the rooms into a separate method, and call it from the event handler -- but you could put it all in the event handler if you like.
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ross,

You have probably got the answer from the replies posted.

I have an alternative way of doing this.

You could maintain 2 additional separate vectors one for Bedrooms and one for the Meeting rooms and populate them with vacant Bedroom numbers and Meeting room numbers respectively.

So if the vector size is 0 you dont have anything vacant. Else, you have vacant Bedrooms or Meeting rooms.

This could solve you problem.
These vectors should be in sync with your original vector.
Hope this helps.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic