Results 1 to 11 of 11
  1. #1
    Ujemny is offline Member
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    8
    Rep Power
    0

    Default Hotel Program. Problem with objects

    Hello!
    I have big problem with my task on my university. Unfortunately, i don't know why my program doesnt works.

    Here is my code:

    Java Code:
    class Hotel 
    {
    	
    	int floors;
    	RoomNumbers[][] hotel;
            
    	
    	public Hotel (int floors)  
    	{
    		hotel = new RoomNumbers[floors][20];
    		this.floors = floors;
                    
                    
                    for(int i = 0; i < floors; i++){
                        for(int j = 0; j < 20; j++){
                            hotel[i][j]= new RoomNumbers(i,j);
                        }
                    }
            }        
            public void PrintFreeRooms(){
                
                System.out.println("====== FREE ROOMS ========");
                for(int i = 0; i < floors; i++){
                    for(int j = 0; j < 20; j++){
                    if(!new RoomNumbers(i,j).rent()){
                            System.out.println(hotel[i][j].number);
                           // System.out.println(hotel[i][j].rent());    to debug
                    }
                           
                }
                System.out.println("===================================");
            }
            }
            public void Rent(Person surname, RoomNumbers room){
               if(!room.rent()){
                    room.rent = true;
                   // pokoj.rent_by = surname;
                    
                }
                    
                    
                    
            }
            
    }
    
    class Person{
        
        String surname;
        
        
        Person (String nazwisko)
        {
            this.surname= nazwisko;
            
        }
    }
    
    class RoomNumbers{
    
        int number, floors;
        
        boolean rent = false;
        Person rent_by;
        
        RoomNumbers(int floors, int number){
            
            this.number = (100*floors + number);
            this.floors = floors;
           
        }
        
        public boolean rent()
        {
            return this.rent == true;
        }
    
    }
    
    
    public class Main {
    
        public static void main(String[] args) {
        
        Hotel hotelik = new Hotel(2);
        Person kowalski = new Person("Kowalski");
        RoomNumbers firstone = new RoomNumbers(0,0);
        
        hotelik.Rent(kowalski, firstone);
        
        hotelik.PrintFreeRooms();
        
       
    
        }
        
    }
    Ok, where is the problem? I want to write program which simulate hotel. I can't write good function "Rent". There is "
    Java Code:
    if(!room.rent()){
                    room.rent = true;"
    , but the problem is that it do not change the value of rent.
    Why is that?


    Thank you for any advise!

  2. #2
    Skywola is offline http://www.tachufind.com
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default Re: Hotel Program. Problem with objects

    Your function appears to be working fine.

    It appears to me that the classes are not talking to one another. You have hotel, and rooms,
    and as far as I can tell, there is no linkage between the two.

    I would be considering re-writing the whole thing . . . . .

    And use a class that has most of the features contained in it.

    Java Code:
    class Hotel{
       public final int NUMBER_OF_FLOORS = 2;
       public final int NUMBER_OF_ROOMS = 120;
       int roomNumber;
       int floor;
       Boolean occupied;
       int rooms[][] = new int[NUMBER_OF_ROOMS][NUMBER_OF_FLOORS];
    }
    Then instantiate the class, and do all your operations on it . . . .
    Instead of using an array you could use an ArrayList too.
    Last edited by Skywola; 01-10-2014 at 10:45 AM.

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: Hotel Program. Problem with objects

    Your 'advice' (which is actually only an opinion) is pretty poor; an occupied boolean in a HOTEL ??? A room is occupied, not an entire hotel.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Hotel Program. Problem with objects

    @OP

    I do not like your class RoomNumbers. It's a confusing entity and it's plural.

    I think you should have a Room class with a boolean occupied (definitely NOT as a field in the hotel) the number and the customer who has rented it, which is fine.

    Put all objects of type Room in a collection in the Hotel and your rent method will merely search for the given room and rent it to the given Person if it is available.

  5. #5
    Skywola is offline http://www.tachufind.com
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default Re: Hotel Program. Problem with objects

    @superhaNds I stand corrected on that; you are right, it would not work to put room in the same class as Hotel. I'm still a bit new to Java, but I hate it when I
    post something and no one responds even more than when someone responds and they are not quite correct, because even that can get you thinking when you
    are stuck. The one thing I knew for sure is that I was right about the function working properly, i.e., setting room to true. My suggestion also was a bit of
    an off-the-cuff affair anyway, as it was clearly stated to be a university assignment, and you cannot help someone by doing their work for them.

  6. #6
    Ujemny is offline Member
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    8
    Rep Power
    0

    Default Re: Hotel Program. Problem with objects

    Thank you for replies guys! I really appreciate that :)
    My big proglem is that i haven't broken blocked in object programming, yet. So i try a lot of things, but procedural programming is still in my mind.

    @superhaNds: What do you mean that RoomNumbers are "entity" and "plural"?

    I'm still fighting with this task ;x Any other advise(more precise)? I mean i understand what you are telling, but i donot know how to do that. Of course i want to do it by my own, but i need somebody to show me the way!

    Thank you!


    Edit: Ok i have some idea.

    I have done something like that:

    Java Code:
    void RentARoom(RoomNumbers room){
                for(int i = 0; i < hotel.length; i++){
                    for(int j = 0; j <= 19; j++){
                        if(room.number == hotel[i][j].number && room.floors == hotel[i][j].floors )
                           hotel[i][j].occupied = true;
                            
                    }
                        
                }
    I think that it is not the nicest way to do it, but it works! :)
    Last edited by Ujemny; 01-11-2014 at 03:44 AM.

  7. #7
    Skywola is offline http://www.tachufind.com
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default Re: Hotel Program. Problem with objects

    I have had the very same problem . . . . getting away from functional
    programming takes a clear effort . . . but, if you persist, you will
    find a huge payoff once you get it. I suppose I am probably not
    quite there yet myself, but I really can see now why using classes
    is so much better.

    You can in some ways, think of classes as an offshoot of functional
    programming, with the requirement that you create an instance
    of the class to work with it.
    That is something that functional
    programmers have trouble with, because they are use to doing everything
    directly. But the problem is that when you do something functionally,
    you HAVE to do it ALL directly, and that really slows things down and
    complicates things in the long run.

    As a functional programmer, you naturally think of abstraction as your enemy, because when you
    write code functionally, you have to keep track of every little thing that you are doing and annotate
    it so you remember what the hell you did twenty minutes ago.

    However . . . . with object-oriented programming, it is practically the
    exact opposite, you use the abstract to your advantage. What I mean
    by that, you look at what the program looks like in its functional form,
    and think to yourself,

    what does all this have in common?

    I am lazy, and that is what makes a good programmer in some ways,
    because if you are lazy, you do not want to keep repeating the
    same lines of code over and over again. This is where object-oriented
    programming comes in . . . . if you are good at being lazy, you can
    find the common-ness in the code, write classes for it, and save
    yourself a lot of time and frustration.

    The following is how I would do it, but be forewarned, I have not checked it
    out on the machine, and I don't think I quite nailed all that you are trying to
    do in it, but with this, you should be able to implement what it is you need
    to do.

    Java Code:
       public final int NUMBER_OF_FLOORS = 2;
       public final int NUMBER_OF_ROOMS = 120;
    
    class Room{
      boolean occupied;
      int number;
      int floor;
    
      Room {   // Class constructor
        occupied = false;
        number = 0;
        floor = 0;
      }
    
    }
    
      public static void main(String[] args) {
         int rooms = NUMBER_OF_FLOORS * NUMBER_OF_ROOMS;
         ArrayList<Room> hotelRooms = new ArrayList<>();       
         
         for(int i = 0; i < rooms; i++){
            // instantiation needed with classes! Note: you must make a new one each time in this case.
            Room room = new Room();  
            room.number = i;
            room.floor = 0;
            other settings as in the above line
            hotelRooms.add(room);
         }
          for(room unit: hotelRooms){
          System.out.println(unit.number);
          System.out.println(unit.floor);
          System.out.println(unit.occupied);
          }
      }
    This is just a way of using a class and an ArrayList to keep track of it all;
    remember, you can do anything with a class that you can with functional
    programming and more . . . if you do not want to use an ArrayList for now,
    just use and array . . . you can do the exact same thing functionally with
    the elements in a class as you can with functional as long as you remember
    that you have to instantiate the class and use the correct method to address
    the elements in the class.

    One last note . . . . read about the things that you need to use in your program
    before you start fiddling with them, (i.e., if your going to use an ArrayList, read the
    API briefly, and maybe even keep it handy for reference while you are writing your
    program). If you try things without reading about them first, you will be there a
    long time. Your objective is to get as much done as possible in the shortest amount
    of time, in the laziest manner possible, so you can use what time you do have to
    your best advantage.

    Note: I modified a few mistakes after the first post, so there were errors if you read it
    early on, immediately after I posted it.
    Last edited by Skywola; 01-11-2014 at 05:22 AM.

  8. #8
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Hotel Program. Problem with objects

    Just a quick comment here - would there be anything wrong with using a boolean array?

  9. #9
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Hotel Program. Problem with objects

    Quote Originally Posted by Ujemny View Post
    Thank you for replies guys! I really appreciate that :)
    My big proglem is that i haven't broken blocked in object programming, yet. So i try a lot of things, but procedural programming is still in my mind.

    @superhaNds: What do you mean that RoomNumbers are "entity" and "plural"?

    I'm still fighting with this task ;x Any other advise(more precise)? I mean i understand what you are telling, but i donot know how to do that. Of course i want to do it by my own, but i need somebody to show me the way!

    Thank you!


    Edit: Ok i have some idea.

    I have done something like that:

    Java Code:
    void RentARoom(RoomNumbers room){
                for(int i = 0; i < hotel.length; i++){
                    for(int j = 0; j <= 19; j++){
                        if(room.number == hotel[i][j].number && room.floors == hotel[i][j].floors )
                           hotel[i][j].occupied = true;
                            
                    }
                        
                }
    I think that it is not the nicest way to do it, but it works! :)

    Entity as in Class. You would not want a class name to be plural. I am saying that there are much simpler ways to achieve what you want. The 2D array is unnecessary.
    My advice is to make a Room Class (which will look exactly like your roomnumbers, but the room will just know its floor and the rest you have) and put them in a List preferably.
    You will also need to initialize the rooms in a simple loop.

    Concerning your rentAroom(..) function, shouldn't it accept a Person also? You just change the status, but who is occupying it?

    EDIT: methods by convention start with a lower case letter and your data fields in your classes should be private, create also setters and getters to mutate and access them.
    Last edited by superhaNds; 01-11-2014 at 05:20 PM.

  10. #10
    Skywola is offline http://www.tachufind.com
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default Re: Hotel Program. Problem with objects

    @AlexGraal Probably not, I suppose, but I wonder how you would implement it,
    connecting each Boolean with each room number. When room is an object, it is easy
    to tie it all together, and, avoid confusing one rooms data with that of another,
    given that each room is an object.
    If we talk about encapsulation though, I probably should have made the fields private.
    Also, the constructor is probably just fluff, but it is good to get use to it, for when you
    really do need one.

  11. #11
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Hotel Program. Problem with objects

    Just without thinking about it, you'd have:

    Java Code:
    boolean[][] hotel = new boolean[Roomsperfloor][floors]
    To check if room 3 on floor 2 is taken or not, simply check the value of hotel[2][1] - if true, it is taken. If false, it is empty.

    Seems to me like the simplest way to do it.
    Last edited by AlexGraal; 01-12-2014 at 01:47 AM.

Similar Threads

  1. Hotel management system in Java
    By evoL in forum New To Java
    Replies: 7
    Last Post: 02-27-2014, 12:47 PM
  2. Replies: 2
    Last Post: 09-27-2013, 06:40 PM
  3. Hotel Reservation in JAVA (help)
    By BattalGazi in forum New To Java
    Replies: 4
    Last Post: 07-31-2011, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •