Originally Posted by
Moonlightkid
I got a question regarding what type of collection class should i used based on the scenario given but i am not sure whether i am correct? Can any1 correct me if i am wrong.
Not really, but you will probably get copious observations. Correctness depends on detailed and thorugh consideration of problem domain, but you are off to a good start
Originally Posted by
Moonlightkid
1. When a hotel is fully booked, it holds a waiting list of customers in case of cancellations. Once a cancellation is received, the newly available room is allocated to the first person on the waiting list, who is then deleted from the list. On a particular day the first three customers added to the list are Ali Baba, Bibi Batek and
Charlie Chan.
You keep using List in your application description, therefore you should give Collection type List deep contemplation. HashSet removes the key from code scope, therefore I suggest consideration of HashMap. Also, private static final Number totalRoomCount = ( room count, probably an int or long or Interger or Long ) Problem with linked list is if any link ever breaks, list from there on is lost. If Bibi Batek takes a hike to the Pulkovo Observatory, we have to set and remove or whatever.
Originally Posted by
Moonlightkid
2. A video shop wants to store the titles of videos available for hire. It must be possible to list all the titles available but no particular order is required. Although the store may have more than one copy of a particular film, the title should only appear once. Titles available include: Iron Man, Crystal Skull and Dr Doplenty.
HashMap, keyed on class Video's String member variable Title, and which class has a countOfItemsOnHand member variable. You then do HashMap.get(video.title) and ++ or -- the item count if not zero......if zero can only ++
Originally Posted by
Moonlightkid
3. An ICT203 tutor wants to store her students' myUniSIM names as strings in such a way that she can easily look up an individual student, as in the table below. There are no two students in her group with the same name.
class Student{
String name.... with get name and set name or with set name in the constructor and then:
if( HashMap.contains(Student.name)){ do something ();/// }
else{ add name or declare error }
Try not to fall into using exceptions to handle student name not in HashMap, possibly handle add already present throws exception but that is not an actual exceptional condition. What is exceptional is trying to read / view or write / add / modifiy without authorization....
That is what an exceptional condition actually is.