如何输出对象及其变量

时间:2015-12-18 04:31:37

标签: java eclipse class oop variables

嗨,我正在尝试创建一个名为HotelReport的课程,当给出一个酒店对象时,会产生一个简短的文本报告,描述酒店的名称,房间的数量,并且每个房间都会列出数字和床的大小。“,但我不确定如何添加房间和床位数,并获得最终报告输出,任何帮助?

酒店等级

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;
  }
}

房间类

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());
}
}

床位

 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;
 }
 }

酒店报告类

 public class HotelReport {

 public static void main(String[] args) {

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


    }
    }

3 个答案:

答案 0 :(得分:0)

试试这个:

酒店课程:

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 +
                '}';
    }
}

房间类:

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 +
                '}';
    }
}

床上课

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 +
                '}';
    }
}

最后用法:

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 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}
================================

答案 1 :(得分:0)

首先,您最好在酒店类中添加addRoom()功能,如下所示:

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

然后,覆盖上面每个类的toString函数。

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

使用ArrayList在主函数中保存Hotel的所有实例,并且可以使用循环来检索它们。

答案 2 :(得分:0)

我认为你的代码有点奇怪。 Hotel类有一个名为hotelRooms的字段,其类型为ArrayList<String>。不应该是ArrayList<Room>吗?这更有意义。

在你的Bed课程中,我对你正在做的事情感到很困惑。我认为更好的实施将是

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;
}

您现在可以将床的大小设置为双人床

yourBed.setBedSize (Bed.SIZE_DOUBLE);

现在问题已解决,让我们看看如何将这些对象转换为String

要将对象转换为字符串,您可以编写一个返回String的方法,例如

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

但是,为了这个目的,最好在toString中使用Object方法。阅读 Effective Java 以获取有关您应该执行此操作的更多信息。

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

并为每个与酒店相关的课程编写toString方法。让我们看看每个类中toString方法的样子如何:(我只显示身体因为我懒惰)

床:

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

间:

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

地图:

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();

现在您可以显示酒店的描述:

Hotel hotel = new Hotel ();
//do stuff with the hotel, such as setting some of its properties
System.out.println (hotel.toString());