tags:

views:

147

answers:

4
+6  A: 

To start off, a Hotel has more than one Room. Depending on what you've learnt as far, you should be using an array to hold all Room instances

Room[] rooms;

Hotel() {
    rooms = new Room[4];
}

or an ArrayList

List<Room> rooms;

Hotel() {
    rooms = new ArrayList<Room>();
}

See also:


Update as per your comment: just check every room if it has a guest until you find a room without a guest (like as in real world!). Pseudo:

if there is no guest in room1, then use room1;
else if there is no guest in room2, then use room2;
else if there is no guest in room3, then use room3;
else if there is no guest in room4, then use room4;
else say "sorry, no rooms left!";

This is by the way easier to do in a simple loop when you use an array.

for each room, check if there is no guest in room, then use room;
if there is no room, then say "sorry, no rooms left!";

Oh, don't forget to make the guest null when s/he leaves the room. This will make the room eligible for reuse.

See also:

BalusC
thanks! arrays i will use later on. I'd first like to be able to do it without.
sjaak
Personally I would suggest a Pool (like http://commons.apache.org/pool/apidocs/org/apache/commons/pool/ObjectPool.html) here. In the constructor you add 4 rooms to the pool, and checkIn borrows, checkOut returns the room to the pool. Not the most efficient solution maybe (?) but it would resemble the problem domain a bit more than an array.
extraneon
@ext: Truly, we can abstract all ugliness more further away, but I think this is a bit too complicated for a beginner :)
BalusC
@BalusC Maybe you're right. It's just that the concept of a pool seems like a natural match for this problem to me. The commons pool API is unfortunately not (no generics makes it look like a hack) :)
extraneon
BalusC thanks again for your reply, helped me a great deal! Got the system running right now am to excited! Will post my solution next week after it is graded (they have somekind of fraud detection system and i wouldn't want them to think i stole it from stackoverflow :))
sjaak
You're welcome. Don't forget to mark the most helpful answer on your questions to accepted. See also http://stackoverflow.com/faq :)
BalusC
+1  A: 
class Hotel {

Room room1, room2, room3, room4;

    Hotel() {
        room1 = new Room();
        room2 = new Room();
        room3 = new Room();
        room4 = new Room();
    }

}

If you haven't learned an Array yet.

But an Array (or ArrayList) is a better way to do this.

Ryan Guest
A: 

the next person that checks in will check in room 2

This can't be done right now. Your hotel only has one room! It should have more, 3 for example:

class Hotel {

  Kamer kamer1;
  Kamer kamer2;
  Kamer kamer3;

  Hotel (){
    kamer1 = new Kamer();
    kamer2 = new Kamer();
    kamer3 = new Kamer();
  }
}

Or better use arraylist

class Hotel {
  ArrayList<Kamer> kamers;

  Hotel (){
    kamers = new ArrayList<Kamer>();//make room-list
    kamers.add( new Kamer() );//add one room
    kamers.add( new Kamer() );//add one room
    kamers.add( new Kamer() );//add one room
    //more?
  }
}

If you need a hotel with 3 rooms, just call: new Hotel();.

static void checkIn() {
   Kamer k1 = new Kamer(g1.naam);

Why are you making a new room when someone wants to check in? You can't just build a new room if you need one, right? If you remove the static you can use the fields kamer1, kamer2 (or kamers if you used the ArrayList).

 void checkIn()
 {
   if (kamer1.guest==null)
      //kamer1 is available!
   //
   //
Ishtar
thanks for your reply, i used your info and put it in. However if i run the program now it doesn't work. After putting on in a room, the second one should get the no room message. however it doesn't work. The thing is actually i don't really know exactly what i am doing with making the rooms. What exactly am i saying with room1 = new room(). And what does it do? Sorry for the stupid questions I am just totally overwelmed by learning about classes/objects(?)..
sjaak
btw, arrays i will use later on. I'd first like to be able to do it without.
sjaak
A: 

Hi sjaak,

a comment on the static (also noticed by Ishtar).

If you make checkIn static on the Hotel class (and not instance) you are saying that checkIn is the same over all hotels, and not just your hotel. That suggests that if you have 5 hotels, each with its own rooms, all the rooms are alike. So you could checkIn in hotel 1 and get a room from hotel 2. That's really not what you want to do.

public class Hotel   {
  // stuff

  // Check a guest into this hotel
  public Room instanceCheckIn(Guest guest) {
    //stuff
  }

  // Check a guest into one of our hotels. It doesn't matter to which 
  // particular hotel this room belongs :)
  public static Room staticCheckIn(Guest guest) {
    //stuff
  }
}

Usage:

Hotel instanceHotel = new Hotel();
// check a guest into a room of this hotel (instanceHotel)
instanceHotel.instanceCheckIn(someGuest);

// Check a guest into one of the rooms of our hotels
Hotel.staticCheckIn(someGuest);

// Error: cannot call a non-static method on a class
Hotel.instanceCheckIn(someGuest); 

// Technically OK, but incredibly bad taste. You suggest something
// is happening with this hotel (called instanceHotel) but in stead
// your actually working on all hotels together (shared data).
instanceHotel.staticCheckIn(someGuest);

In general it's a very good habit to not use static unless you really need it. It suggests something is broken in your domain model. That does not mean you should not use it, but realize that it has a particular odor. (public static void main(String[] args) is of course a special case).

extraneon