tags:

views:

281

answers:

6

For my CS course I have to program a hotel checkin/checkout system. The program must be able to check people in and out. The programm assigns the first available room to a guest upon checkin. If no room is free, it will say so as well. The Hotel has four rooms.

In the assignment it says there need to be 4 classes: Assignment5, Hotel, Room, & Guest.

  1. Assignment5 class is for the interaction with the user.

  2. Hotel class has four rooms and all methods for operating the rooms.

  3. Room class has 1 guest. If the rooms is empty a guest can check in. If the guest is leaving the room needs to be emptied.

  4. Guest class: the guest has a firstname and a last name.

In the menu there need to be 4 options: 1 show status of rooms available and occupation. 2 check in option 3 check out option 4 end program option.

OK, so I know I should make my assignment for myself. However, I don't know what it is, but starting with assignments I have great problems cutting the thing up in smaller pieces. Also this assignment is to learn working with different classes and I don't really understand which sequence of steps i should take in this case.

Can someone help me getting started by giving some tips? Have been staring at my screen for hours now and just thought I could use some little insights to get me started. Any help is greatly apprecieted!

OK thnks for the help so far.

**First of all, MANY thanks for all your help, you guys are great! Have been at it for 7 hours straight now and still stuck. My problem now is that it doesn't compile. It says:

Java:28: checkIn(Gast) in Hotel cannot be applied to (java.lang.String)
                hotel.checkIn("Guido");
                                     ^
1 error

And maybe, can someone look if this way i put it now is a little bit on the right path? I do thank JavaGeek for his program, but i want to learn it by doing myself.

up until now I have the following:

   import java.util.Scanner;

public class bopgave5 {

    public static void main(String[] args) {

        boolean opnieuw = false;

        do {
            int invoer = menu();

            if (invoer == 2){

                Hotel hotel = new Hotel();
                hotel.checkIn("Guido");

                opnieuw = true;
            }

            else if (invoer == 4)
                opnieuw = false;

            else
            opnieuw = true;
        }
        while (opnieuw == true);

    }

    public static int menu (){
        Scanner sc = new Scanner(System.in);


        System.out.println("MENU:   [1] Statusoverzicht");
        System.out.println("    [2] Check-in");
        System.out.println("    [3] Check-out");
        System.out.println("    [4] Einde");
        System.out.print("Uw invoer: ");
        int invoer = sc.nextInt();  

        return invoer;

    }


}

class Hotel {

    Kamer kamer1, kamer2, kamer3, kamer4;

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




    void checkIn(Gast nieuweGast) {


        if (kamer1.gast == null) {      
        kamer1.gast = nieuweGast;
        System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 1");
        return;
        }

        else if (kamer2.gast == null) {     
        kamer2.gast = nieuweGast;
        System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 2");
        return;
        }

        else if (kamer3.gast == null) {     
        kamer3.gast = nieuweGast;
        System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 3");
        return;
        }

        else if (kamer4.gast == null) {     
        kamer4.gast = nieuweGast;
        System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 5");
        return;
        }

        else {
        System.out.println("hotel is vol");
        return;
        }


    }                        

}

class Kamer {

    Gast gast;

    Kamer() {
        gast = null;
    }


}

class Gast {
    String naam;

    Gast(String nieuweNaam) {
        naam = nieuweNaam;
    }
}
+2  A: 

Short of solving this thing for you, here is what I would suggest you do.

Think of each operation that needs to be performed, and then diagram the logic that needs to take place to accomplish that operation. Then, with a clear list of logical operations that need to take place to accomplish that task, think carefully about what components you can break that operation up into and where each of those sub-tasks should go. This will give you a good idea of what the class diagram should look like. Once you've diagrammed the logic, classes/methods, and implemented one part, move on to the next part. At this point, you'll probably find that something wasn't structured in a way that allows it to be reused for the next task, so refactor it (modify the structure of the code) to make it more reusable.

For example, tackle the check-in operation. What do you need to do first? Well, first you need determine if there are any rooms available. How do you do that? You need to ask all of the rooms (by your design, if I remember correctly) if they are available. So, we can now identify that we need a method on the rooms to tell us that they are available. Maybe tackle that first. Then the checking for a free room part. That probably goes on the hotel class. You'll need to iterate over the rooms and ask them if they are free. After this, you need to decide what to do if there is a free room(s), or if there aren't any. I guess you display an error message if there aren't? If there are free rooms, you need form the check-in operation. Write that. Rinse and repeat. Before long, you'll have a functioning program.

The important thing to remember here is that there are MANY different ways to accomplish this in an object-oriented manner. When you are just learning OO design and the basics of programming, it is important to not get too hung up on the perfect design and to dive in. Then when you are in the middle of implementing it, I'm sure you'll say 'hey, if I do it this way it will come up much better'. I like to think of this as an iterative learning process.

tau-neutrino
thanks for the input, i updated my work, but it doesnt compile. Can you give me feedback why it doesn't compile and whether or not this way of doing it makes sense? Thanks!
sjaak
+5  A: 

So you have 4 classes.

In the assignment it says there need to be 4 classes: Assignment5, Hotel, Room, & Guest.

With the division of responsibility as such:

Assignment5 class is for the interaction with the user.

Hotel class has four rooms and all methods for operating the rooms. (extra emphasis: "rooms" is plural)

Room class has 1 guest. If the rooms is empty a guest can check in. If the guest is leaving the room needs to be emptied. (or in other word, operating a single room)

Guest class: the guest has a firstname and a last name.

First, you'd probably want to identify the "state" that each object would have. IOW, you need to determine the attributes/instance fields that each object have. Let's start with an example: Guest class: the guest has a firstname and a last name.. Do the same for all the other classes.

Next, you want to identify the methods that will be needed. Let's start with another example: Room class has 1 guest. If the rooms is empty a guest can check in. If the guest is leaving the room needs to be emptied.. On top of that, you'll need some constructors for each class.

Next, for each method, you want to find out the arguments that the method needs, and their return values, if they need to return a value. For example, for a check in method, you'll need the Room and a Guest; and you'll need to check whether the room is empty before you can check in.

UPDATE:

My problem now is: how can i make it work that there are 4 rooms and that after checking in 1 person, checking in a second person will put it in a different room?

Your teacher has a good advice, break it into pieces.

Basically, you have the problem: "Checking in the second person should put him in a different room".

So, how do we break this down? First, we need to find an empty room, so we need a method for that (say findEmptyRoom()), and after we find a room that's available, we need to check in the guest into that room, so we need another method (say checkIn()). So, we find an empty room, then we checked the guest into that room, then we're done.

Next step, we break findEmptyRoom(). Our hotel has 4 rooms (let's say we store room1, room2, room3, and room4 as member variables, alternatively, you can use an array if you already learn about it in class). To find which one of the four rooms are empty, we need to ask a room if it is empty; so we need another method for that (say isEmpty()). To find an empty room, ask room 1, if room1 is empty return room1, otherwise ask room 2, if room2 is empty return room2, otherwise ask room 3, if room3 is empty, return room3, otherwise ask room 4, if room4 is empty, return room4. Otherwise, all rooms are occupied. [note: you will need to figure out what to do if all rooms are occupied]. An array will make this process much easier, you just need to loop through the array, and ask if it's empty and return the room if it's empty, otherwise continue checking next room.

Next, we break checkIn(). So, what do checkIn() need to know to fulfill the checking in process? It needs to know about two information: the room and the guest. Since checkIn() is an instance method of a room class, room is implied, so we only need one parameter, i.e. guest. So void checkIn(Guest guest) should set the guest member variable of the room.

Next, we break isEmpty(). How do we know if a room is empty? If a room has a guest inside it, then it's occupied, otherwise it's empty. So, isEmpty() checks if the guest member variable refers to a valid guest, returns true if it is valid guest, otherwise returns false. In Java, null is often used to mark that something is not a valid object, so you can check whether the guest member variable is null (obviously, you'll need to set the guest member variable to null when a guest checked out).

Do the same thing for the other processes. Break the problem down into smaller pieces until you're confident that you can work with a piece.

UPDATE2:

The error message you've got:

Java:28: checkIn(Gast) in Hotel cannot be applied to (java.lang.String)
                hotel.checkIn("Guido");

is because you're passing a String ("Guido") to a function that takes a Gast argument (void checkIn(Gast nieuweGast) {...}). You should instead, pass a Gast object:

// declare a variable called g, with type Gast
Gast g;

// create a new Gast object, passing "Guido" as parameter 
// to Gast's constructor, and assign the new object to g
g = new Gast("Guido");

// call hotel.checkIn(Gast) function with g as the argument, 
// i.e. the Gast object we just created in the previous line
hotel.checkIn(g);

or more simply:

hotel.checkIn(new Gast("Guido"));
Lie Ryan
thanks for the input, i updated my work, but it doesnt compile. Can you give me feedback why it doesn't compile and whether or not this way of doing it makes sense? Thanks!
sjaak
@sjaak: see my updated answer
Lie Ryan
@sjaak: see my update2 answer
Lie Ryan
+2  A: 

Start with the objects, the domain

Try giving the classes useful members. What is a 'hotel' for this problem? When they have useful members, what are useful operation on them, the methods?

Fields: A room should have a room number. A room should know what guest is staying in that room. A guest should have a name. A hotel should have a collection of rooms, a name, an address? Maybe the address is useless here.

Methods: A room should have a method to see if it's empty and what guest is staying there if it is not empty. A hotel should have a method that tells you how many rooms are available, or give you the actual rooms. All the guests that stay in the hotel? Etc, etc, etc.

You'll notice you will have to keep refining, changing, fixing your model to be able to meet the requirements. For example 'checking in' might need a new method in room you did not yet have.

Ishtar
thanks for the input, i updated my work, but it doesnt compile. Can you give me feedback why it doesn't compile and whether or not this way of doing it makes sense? Thanks!
sjaak
@sjaak - Don't use `Hotel.checkIn();` make a Hotel object: `Hotel mijnhotel = new Hotel();` and call `checkIn()` on that object: `mijnHotel.checkIn();`
Ishtar
I'm sorry but I don't understand what you mean by this?
sjaak
@sjaak - Check this: http://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static-c
Ishtar
thanks, i came accross that one already, but didn't really understand it. However, i don't know what i did but it works now. However i have a new problem now: how can i make it work that there are 4 rooms and that after checking in 1 person, checking in a second person will put it in a different room? Loads of thanks already!
sjaak
+1  A: 
//Guest.java
package hotelcheckinsystem;

class Guest {
  private String name;

  public String getName() {
   return name;
  }

  public Guest(String name) {
    super();
    this.name = name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

//Room.java
package hotelcheckinsystem;

class Room {
  private Guest guest;

  public Guest getGuest() {
    return guest;
  }

  public void setGuest(Guest guest) {
    this.guest = guest;
  }

  public void removeGuest() {
    guest.setName("");
  }

  public boolean isEmpty() {
    if (this.getGuest().getName().equalsIgnoreCase("")) {
      return true;
    }
    return false;
  }
}

//Hotel.java
package hotelcheckinsystem;

public class Hotel {
  private Room[] rooms = new Room[4];

  public Hotel() {
    for (int i = 0; i < 4; ++i) {
      rooms[i] = new Room();
      Guest guest = new Guest("");
      rooms[i].setGuest(guest);
    }
  }

  public void assignRoomToGuest(String name) {

    int i;
    for (i = 0; i < 4; ++i) {
      if (rooms[i].isEmpty()) {
        Guest guest = new Guest(name);
        rooms[i].setGuest(guest);
        System.out.println("Room number " + i + " assigned to " + name);
        return;
      }
    }
    if (i == 4) {
      System.out.println("No empty Rooms to assign to " + name);
    }
  }

  public void emptyRoom(int roomNo) {

    if (roomNo > 3) {
      System.out.println("please enter number between 0 to 3");
      return;
    }
    rooms[roomNo].removeGuest();
    System.out.println("Room number " + roomNo + " is empty now!.");
  }
}

//Main.java

import java.util.Scanner;
import hotelcheckinsystem.*;

public class Main {
  public static void main(String[] args) {
    Hotel hotel = new Hotel();
    while (true) {
      System.out.println("Enter the Option: ");
      System.out.println("1. Check in. \n2. Check out. \n3. Exit");
      Scanner sc = new Scanner(System.in);
      int option = sc.nextInt();
      switch (option) {
      case 1:
        System.out.println("Enter the guests name");
        hotel.assignRoomToGuest(sc.next());
        break;

      case 2:
        System.out.println("Enter the Room number");
        hotel.emptyRoom(sc.nextInt());
        break;

      case 3:
         System.exit(0);
      }
    }
  }
}

Sample Run:-

Enter the Option:

  1. Check in.

  2. Check out.

  3. Exit

1

Enter the guests name

empty

Room number 0 assigned to empty

Enter the Option:

  1. Check in.

  2. Check out.

  3. Exit

1

Enter the guests name

A

Room number 1 assigned to A

Enter the Option:

  1. Check in.

  2. Check out.

  3. Exit

1

Enter the guests name

B

Room number 2 assigned to B

Enter the Option:

  1. Check in.

  2. Check out.

  3. Exit

1

Enter the guests name

C

Room number 3 assigned to C

Enter the Option:

  1. Check in.

  2. Check out.

  3. Exit

1

Enter the guests name

D

No empty Rooms to assign to D

Enter the Option:

  1. Check in.

  2. Check out.

  3. Exit

2

Enter the Room number

1

Room number 1 is empty now!.

Enter the Option:

  1. Check in.

  2. Check out.

  3. Exit

1

Enter the guests name

D

Room number 1 assigned to D

Enter the Option:

  1. Check in.

  2. Check out.

  3. Exit

3

JavaGeek
-1. Not only you're not helping the OP learn by doing it himself, your solution is not even a good OOP. In your solution, Hotel class is a God Object (http://en.wikipedia.org/wiki/God_object) that has too much responsibilities.
Lie Ryan
+1 @Lie: I was trying to help. Looks like I have to learn good designs in OOP. :)
JavaGeek
@missingfaktor: Thanks for edit. :)
JavaGeek
thanks java geek! the problem i now have is that i need an input system, not the sample input. However, when i make this so that one can input a string and that it takes that string and put it into a room. Afterwards if that function is chosen again, a string is put in, it puts the string in the same room, not in the next one. Any ideas?
sjaak
@sjaak: refer to my program, it checks before putting in the String whether the Room is empty or not. If yes, then only it puts the String.You must be making function calls on the same object, hence it is putting String in the same 'function'(as you are calling it).Else, i did not understand what you are trying to say.
JavaGeek
I have refactored the code, Now Hotel class is not a God Object as @Lie Ryan stated.
JavaGeek
Ok, I've lifted the downvote. One thing not quite right in your current solution is `this.getGuest().getName().equalsIgnoreCase("empty")`. What if someone named "Mr. Empty" checks into your hotel? It's probably better to set `guest` to `null` when a room is empty, instead of having a special name.
Lie Ryan
@Lie Ryan: Done with the "Mr. Empty" thing. Nobody can have name "".
JavaGeek
@JavaGeek: I'm trying not to appear too nitpicky here, but the front-desk clerk could possibly forget to fill the name in, or the guest refuses to disclose his name. I'd rather distinguish between "no guest" and "have guest" by assigning `guest` to `null`; or if I'm using "Null Object Pattern", I'd use a subclass.
Lie Ryan
@Lie: I tried inserting nothing, it doesnt accept empty String or white spaces too. So it works fine. There will be no guest with name "" or " ". This means the front-desk user has to insert some name otherwise the process will not proceed.
JavaGeek
@JavaGeek: are you sure? Does this not succeed? new Guest("");
Lie Ryan
@Lie Ryan: Removed public from the class Guest and Room. Now they will only be visible in the same package. So, now only class Hotel can access them. Main class will always be outside package hotelcheckinsystem, hence it cannot succeed in this. new Guest("");. Main class has only access to Hotel class.
JavaGeek
A: 

I can see you already made some code but for your next similar assignment I would code the classes in the following way.

  1. Guest
  2. Room
  3. Hotel
  4. Assignment5 <-- This is the most complicated one and is something you can do at last.

Why?

Guest is really easy to code. When that is done go on to Room. Room is gonna need the class Guest. After you finished Room go code Hotel. Hotel is gonna use Room.

From what I can understand from your (dutch)code you store a guest as a String. This is not the purpose of the assignment. It is supposed to store a Guest object.

I hope this will help you.

bobjink
thanks for the breakdown, that is also the order my teachers example was; guess she didn't do that for no reason :) Also: your folowing comment (From what I can understand from your (dutch)code you store a guest as a String. This is not the purpose of the assignment. It is supposed to store a Guest object.) was very valuable, i intuitively thought the Guest guest was weird. So what exactly does 'Guest geust;' in the Room/Kamer class do?
sjaak
It stores a Guest object. But you have already implemented it I can see. So no worries.
bobjink
A: 

I'm joining this a little late, but the reason for the error is you are attempting to check in a String called Guido instead of a Guest (Gast) with the name field of Guido. You would need some lines like the following:
Gast guido = new Gast("Guido");
hotel.checkIn(guido); // this was previously hotel.checkIn("Guido")

Woot4Moo