如何在另一个对象中声明对象数组长度

时间:2017-12-11 20:39:23

标签: java

我正在尝试创建一个包含3个对象的程序;酒店,客房和床。物品床将收纳有关床的信息。对象室将保存有关房间的信息,包括它包含的床数。对象酒店将包含有关其包含的房间数的信息。

我的酒店类代码看起来像这样

public class Hotel {

    private String name;
    private boolean HasVacency = false;
    public int numberOfRooms;
    Room[] rooms = new Room[numberOfRooms + 1];

    public Hotel() {


    }

    public void setRoom(int numberOfRooms) {
        this.numberOfRooms = numberOfRooms;
    }

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

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





}

我创建酒店并声明值的测试类的代码如下所示

public static void main(String[] args) {
    HotelTest t = new HotelTest();
    t.getHotelInfo();



}

public void getHotelInfo() {
    Hotel test = new Hotel();
    int numberOfRooms;
    int numberOfBeds;
    String size;
    Scanner input = new Scanner(System.in);

    System.out.println("what is the name of the hotel");
    String name = input.next();
    test.setName(name);

    System.out.println("how many rooms does the hotel have");
    numberOfRooms = input.nextInt();
    test.setRoom(numberOfRooms);
    System.out.println(test.rooms.length);


    for( int i = 0; i< test.numberOfRooms + 1; i++) {

        System.out.println("how many beds does room " + (i + 1) + " have");
        numberOfBeds = input.nextInt();
        System.out.println(i);
        test.rooms[i].setNumberOfBeds(numberOfBeds);

    }

}

然而,当我尝试设置测试中房间数的值时,我一直得到一个空指针。对不起凌乱的代码

2 个答案:

答案 0 :(得分:1)

这里要看几件事。您的代码打破了封装(您的酒店类的公共成员被其他类访问);你可以利用构造函数来使事情变得更整洁;看看从其组件向上创建酒店而不是自上而下的方法。我已经包含了一些基于你的示例代码。

public class Hotel {

    private final List<Room> rooms;

    private final String name;

    // constructor to initialize hotel with name and number of rooms
    public Hotel(String name, List<String> rooms) {
        this.name = name;
        this.rooms = rooms;
    }

    public Room[] getRooms() {
        return this.rooms;
    }

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

}

public class Room {

    private final int beds;

    // constructor to initialize a room with the number of beds it needs
    public Room(int beds) {
        this.beds = beds;
    }

    public int getBeds() {
        return this.beds;
    }

}

public void getHotelInfo() {
    Scanner input = new Scanner(System.in);

    System.out.println("what is the name of the hotel");
    String name = input.next();

    System.out.println("how many rooms does the hotel have");
    int numberOfRooms = input.nextInt();

    List<Room> rooms = new ArrayList<>();        

    for( int i = 0; i< numberOfRooms; i++) {
        System.out.println("how many beds does room " + (i + 1) + " have");
        int numberOfBeds = input.nextInt();

        // create a room with the number of beds specified and add it to the list of rooms
        Room room = new Room(numberOfBeds);
        rooms.add(room);
    }

    Hotel hotel = new Hotel(name, rooms);

    // from here on, if you want the hotel's name or its rooms, you can use the appropriate getter methods in the Hotel and Room classes

}

答案 1 :(得分:0)

class Hotel {

    private String name;
    private boolean HasVacency = false;


    public Hotel(int numberOfRooms) {

        Room[] rooms = new Room[numberOfRooms + 1];

    }



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

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

我修改了你的酒店课程。在您更新numberOfRooms之前,您的旧代码阵列正在初始化。创建这样的酒店对象Hotel test = new Hotel(numberOfRooms);

相关问题