-4

Hi I'm trying to create "a class called HotelReport which, when given a Hotel object will produce a short, textual report describing the name of the hotel, the number of rooms and, for each room, lists the number and size of the beds.", but I'm unsure how to add the rooms and number of beds and get the final report to output, any help?

Hotel Class

import java.util.*;

public class Hotel {

// Hotel Name
private String hotelname;

public void setName(String hotelname) {
    this.hotelname = hotelname;

}

public String getName() {
    return this.hotelname;
}

// Hotel Rooms
private List<String> hotelRooms = new ArrayList<String>();

public void sethotelRooms(List<String> hotelRooms) {
    this.hotelRooms = hotelRooms;

}

public List<String> gethotelRooms() {
    return hotelRooms;
  }
}

Room Class

import java.util.*;

public class Room {

private int roomNumber;

public Room(int roomNumber) {
    this.roomNumber = roomNumber;
}

private static List<Bed> beds = new ArrayList<Bed>();

public Room(List<Bed> beds) {
    setBeds(beds);
}

public void setBeds(List<Bed> beds) {
    this.beds = beds;
}

public List<Bed> getBeds() {
    return beds;
}

public String getFormat() {
    return String.format("Beds:\t%s\n", getBeds());
}
}

Bed Class

 import java.util.ArrayList;
 import java.util.*;

  public class Bed {
  // size of bed
  private int singleBed = 1;
  private int doubleBed = 2;

  // implement single bed
  public int getsingleBed() {
    return singleBed;
  }

  public void setsingleBed() {
    this.singleBed = singleBed;
   }

   // implement double bed
    public int getdoubleBed() {
    return doubleBed;
}

public void setdoubleBed() {
    this.doubleBed = doubleBed;
 }
 }

HotelReport Class

 public class HotelReport {

 public static void main(String[] args) {

    Hotel hotelRooms = new Hotel();
    hotelRooms.setName("Intercontinental");
    hotelRooms.addRoom(1,2,3)


    }
    }
edpo
  • 9
  • 2
  • `hotelRooms.addRoom(1,2,3)` Do you know what this function does? – Ruchir Baronia Dec 18 '15 at 04:37
  • 1
    Because I dont see any method `addRoom` with parameter `int, int, int` in class `Hotel`... – Ruchir Baronia Dec 18 '15 at 04:38
  • What error are you getting? – Tot Zam Dec 18 '15 at 04:38
  • What you want to do is override the toString method: http://stackoverflow.com/questions/10734106/how-to-override-tostring-properly-in-java It's similar to the `getFormat` method you have in your Room class. You could make a similar method for your HotelReport class. – MC10 Dec 18 '15 at 04:40

3 Answers3

0

Try this:

Hotel class:

class Hotel {
    private String name;
    private List<Room> rooms = new ArrayList<>();

    public void addRoom(Room room) {
        rooms.add(room);
    }

    public String getName() {
        return name;
    }

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

    public List<Room> getRooms() {
        return rooms;
    }

    public void setRooms(List<Room> rooms) {
        this.rooms = rooms;
    }

    @Override
    public String toString() {
        return "Hotel{" +
                "name='" + name + '\'' +
                ", rooms=" + rooms +
                '}';
    }
}

Room class:

class Room {
    private int roomNum;
    private List<Bed> beds = new ArrayList<>();

    public Room(int roomNum) {
        this.roomNum = roomNum;
    }

    public void addBed(Bed bed) {
        beds.add(bed);
    }

    public int getRoomNum() {
        return roomNum;
    }

    public void setRoomNum(int roomNum) {
        this.roomNum = roomNum;
    }

    public List<Bed> getBeds() {
        return beds;
    }

    public void setBeds(List<Bed> beds) {
        this.beds = beds;
    }

    @Override
    public String toString() {
        return "Room{" +
                "roomNum=" + roomNum +
                ", beds=" + beds +
                '}';
    }
}

Bed class

class Bed {

    private BedType bedType = BedType.SINGLE;

    public Bed() {
    }

    public Bed(BedType bedType) {
        this.bedType = bedType;
    }

    public BedType getBedType() {
        return bedType;
    }

    public void setBedType(BedType bedType) {
        this.bedType = bedType;
    }

    public enum BedType {
        SINGLE, DOUBLE
    }

    @Override
    public String toString() {
        return "Bed{" +
                "bedType=" + bedType +
                '}';
    }
}

Finally usage:

public static void main(String[] args) throws Exception {

        // beds for room#1
        Bed bed1 = new Bed(Bed.BedType.SINGLE);
        Bed bed2 = new Bed(Bed.BedType.DOUBLE);
        // beds for room#2
        Bed bed3 = new Bed(Bed.BedType.SINGLE);
        Bed bed4 = new Bed(Bed.BedType.SINGLE);

        // Room #1
        Room room1 = new Room(1);
        room1.addBed(bed1);
        room1.addBed(bed2);

        // Room #1
        Room room2 = new Room(2);
        room2.addBed(bed3);
        room2.addBed(bed4);

        // Hotel
        Hotel hotel = new Hotel();
        hotel.setName("Intercontinental");
        hotel.addRoom(room1);
        hotel.addRoom(room2);

    }

Get data:

// get the data
        String hotelName = hotel.getName();
        System.out.println("hotelName = " + hotelName);
        List<Room> rooms = hotel.getRooms();
        for (Room room : rooms) {
            System.out.println("room = " + room);
            List<Bed> beds = room.getBeds();
            for (Bed bed : beds) {
                System.out.println("bed = " + bed);
            }
        }

Out put:

hotelName = Intercontinental
room = Room{roomNum=1, beds=[Bed{bedType=SINGLE}, Bed{bedType=DOUBLE}]}
bed = Bed{bedType=SINGLE}
bed = Bed{bedType=DOUBLE}
================================
room = Room{roomNum=2, beds=[Bed{bedType=SINGLE}, Bed{bedType=SINGLE}]}
bed = Bed{bedType=SINGLE}
bed = Bed{bedType=SINGLE}
================================
Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
0

Firstly, you'd better add an addRoom() function in Hotel class like this:

public void addBed(Room room){
    this.rooms.add(room);
}

Then, override the toString function for each class above.

@override
public String toString(){
    String report = "Hotel name:" + this.hotelName;
    return report;
}

Use an ArrayList to hold all the instance of Hotel in your main function, and you can use a loop to retrieve them.

Simon Bai
  • 120
  • 1
  • 12
0

Your code is kind of weird in my opinion. The Hotel class has a field called hotelRooms which is of type ArrayList<String>. Shouldn't it be ArrayList<Room>? That just makes more sense.

And in your Bed class, I am really confused about what you are doing. I think a better implementation would be

public class Bed {
    private int bedSize;
    //getter and setter for bedSize omitted. I'm lazy
    public static final int SIZE_DOUBLE = 2;
    public static final int SIZE_SINGLE = 1;
}

You can now set the bed size to double bed like this

yourBed.setBedSize (Bed.SIZE_DOUBLE);

Now that your problems are fixed, let's see how we can turn these objects into String.

To turn an object into a string, you can write a method that returns a String , something like

public String description () {
    //blah blah blah
}

However, you better use the toString method in Object for this purpose. Read Effective Java for more info of why you should do this.

@Override
public String toString () {
    //blah blah blah
}

And you write a toString method for every class that is related to the hotel. Let's see how the toString methods in each class would look like: (I only show the body cos I'm lazy)

Bed:

if (bedSize == SIZE_DOUBLE)
    return "a double bed";
else
    return "a single bed";

Room:

return "Room " + Integer.toString (roomNumber);

Hotel:

StringBuilder builder = new StringBuilder ();
builder.append ("A hotel called").append(hotelName).append(".");
builder.append ("It has ").append (hotelRooms.size()).append (" rooms.");
for (Room room : hotelRooms) {
    builder.append (room.toString()).append (" has ");
    for (Bed bed : room.beds) {
        builder.append (bed.toString()).append (" ");
    }
    builder.append (".");
}

return builder.toString();

Now you can display the hotel's description:

Hotel hotel = new Hotel ();
//do stuff with the hotel, such as setting some of its properties
System.out.println (hotel.toString());
Sweeper
  • 213,210
  • 22
  • 193
  • 313