Emma Sophia Jones wrote:Okay so what you’re probably wanting is a HashMap – this is a container which stores a value (which can be any object) against a key (which again can be any object but is pretty often a String). When you call the method .get(<key>) on your HashMap it will return the value associated with that key.
So let’s say we have a HashMap where a person’s age is stored against their name, I can pull out Sally's age by saying
If you’re still a bit uncertain on HashMaps, I’d have a bit of a google and see if you can find a tutorial before you continue reading my post
When we’re creating the HashMap, we need to specify the type of key it will have and the type of value it will store – so something like this:
Once we have that, we can start putting in new guests with a .put() command.
You can then get people out of the HashMap with get():
And also remove them on checkout with remove():
To get this to work you’ll need to rewrite quite a lot of your hotel class as your approach so far probably isn't going to work. It might be worth starting from scratch instead of trying to slot it into what is there already. Does that help?
Emma Sophia Jones wrote:And would you like to keep track of them by name or by, say, room number?
Emma Sophia Jones wrote:What's the goal of the assignment Pierre?
Are you just trying to keep track of the number of guests in the hotel? Or do you need to be able to retrieve information about them while they're staying there?
Les Morgan wrote:Since you are using synchronized in your methods, I suggest you change your declaration of numberOfOccupatedRooms to a protected static int. This will make it a class variable and tie any and all threads to one instance of numberOfOccupatedRooms. As it stands now in your code, the use of a protected int is an instance variable. Instance variables will tie to any particular instance, so if you have 2 instances running, then your number of occupied rooms will be tracked separately in each instance and allow maximum checkins in each instance, 250 in the first instance and 250 more in each additional instance. Without the use of a static qualifier, Class Variable, you will be able to double, triple, or more book your rooms according to how many instances you have running.
Campbell Ritchie wrote:I worry whenever I see anything marked static. Making the variable static would suggest every hotel has the same number of rooms. Are you sure about that? Should the exercise use one instance reached by several threads? Is this possible in view of the tight specifications above?
Paul Clapham wrote:I understand the "capacity" of a hotel -- it's the maximum number of people who can stay in the hotel at one time. That's what "capacity" means. But "size"? I don't understand what the "size" of a hotel is. Perhaps you could explain? Yes, I realize that having to work in English makes that sort of thing harder... perhaps you thought of a specific word in French (which it looks to me is your native language) but your English translation as "size" is not at all specific.
And don't be afraid of long variable names: if you want a variable to store the number of available rooms then numberOfAvailableRooms is a good choice for that variable's name.
Campbell Ritchie wrote:I think the time has come to turn yout computer off and write down what you are trying to do on paper. Only when you have that worked out will you be able to write any code.
Norm Radder wrote:
maybe the problem is that the thread go to 200 instead of 100
Sorry, without the author of the code writing some comments saying what the program is supposed to do,
There are 100s of things the program does not do. Is one of those 100s of things what the program should do? Impossible to tell.