Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a hard time determining the starting point of my design

Tags:

java

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;
    }
}
like image 811
sjaak Avatar asked Dec 28 '22 06:12

sjaak


2 Answers

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"));
like image 198
Lie Ryan Avatar answered Dec 30 '22 18:12

Lie Ryan


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.

like image 20
tau-neutrino Avatar answered Dec 30 '22 19:12

tau-neutrino