File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Search A Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Search A Vector" Watch "Search A Vector" New topic
Author

Search A Vector

Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Ross McManus
Greenhorn

Joined: Jul 28, 2010
Posts: 28
Thanks.

So that would go in the mouse clicked event?


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Jun 10, 2008
Posts: 156

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.


Priety.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Search A Vector