2d包含对象的数组

时间:2013-09-03 07:38:39

标签: java object multidimensional-array

这是一项涉及创建酒店客户服务的课程作业。我创建了下面的2d数组,其中包含8个“楼层”,20个“房间”,并用Room个对象填充它们。我目前正在尝试使用嵌套的for循环来遍历每个Room对象并为其分配一个房间号。例如,1楼将包含101-120房间。

下面的课程是我正在使用的测试课程。

public class Test {

    public Test() {
    }

    public static void main(String[] args) {
        /**
          * Creates a two dimensional array with 8 rows and 20 columns
          */
        Room [][] hotelBuild = new Room[8][20];
        /**
          * populates the 2d array with Room objects
          */
        for (int floor=0; floor<hotelBuild.length; floor++) {
            for (int room=0; room<hotelBuild[floor].length; room++) {
                hotelBuild[floor][room] = new Room();
                /**
                 * used to print out contents of 2d array
                 */
                //System.out.print(hotelBuild[floor][room]=new Room());
            }
        }
    }
}

下面是包含变量,setter,getter以及Room覆盖方法的类toString()

public class Room {

//Instance variables

    /**
     *the rooms number
     */
    private int roomNumber;

    /** is the room occupied or not
     *
     */
    private boolean isOccupied;

    /** name of guest
     *
     */
    private String guest;

    /** cost per day
     *
     */
    private double costPerDay;

    /** number of days guest is staying
     *
     */
    int days;

    //Constructors
    public Room(){
    }
    /** Construct a room with values above
     *
     */
    public Room(int room, boolean nonVacant, String guestName, double cost, int day) {
        roomNumber = room;
        isOccupied = nonVacant;
        guest = guestName;
        costPerDay = cost;
        days = day;
    }

    // getters

    /** gets roomNumber
     *
     */
    public int getRoomNumber(){
        return roomNumber;
    }

    /** gets isOccupied
     *
     */
    public boolean getIsOccupied(){
        return isOccupied;
    }

    /** gets guest
     *
     */
    public String getGuest(){
        return guest;
    }

    /** gets costPerDay
     *
     */
    public double getCostPerDay(){
        return costPerDay;
    }

    /** gets days
     *
     */
    public int getDays(){
        return days;
    }

    // setters

    /** sets isOccupied
     *
     */
    public void setIsOccupied(boolean full){
        this.isOccupied = full;
    }

    /** sets days
     *
     */
    public void setDays(int numDays){
        this.days = numDays;
    }

    /** sets guest name
     *
     */
    public void setGuest(String name){
        this.guest = name;
    }

    /** formats output depending if room is occupied or not
     *
     */

    public String toString(){
        if(isOccupied == true){
            return "Room number: " + roomNumber + "\n"+ "Guest name: "
                + guest + "\n"+ "Cost : " + costPerDay
                + "\n"+ "Days: " + days + "\n";
        }
        else{
            return "Room number " + roomNumber
                + " costs " + costPerDay + "\n";
        }
    }
}

如何为阵列中的每个Room对象分配一个唯一的房间号?

4 个答案:

答案 0 :(得分:0)

你可以做到

for (int floor=0; floor<hotelBuild.length; floor++){
    for (int room=0; room<hotelBuild[floor].length; room++){
        Room r = new Room();
        r.setRoomNumber(
           cleverCalculationWithFloorAndRoomNr(floor, room));
        hotelBuild[floor][room]= r;

答案 1 :(得分:0)

如果您想要一种方法将数字按正确的顺序排列,那么请使用您所拥有的可变数据。使用计数知道它是什么楼层(它将在第一个循环结束时增加)。然后房间号是Floor * 100 +在第一个循环开始时重置为1的另一个变量。只是提示帮助你。填补空白;)

int floorNumber = 1;
int roomNumber = 1;
for (int floor=0; floor<hotelBuild.length; floor++){
        for (int room=0; room<hotelBuild[floor].length; room++){
            hotelBuild[floor][room]=new Room();

                            /**
                             * used to print out contents of 2d array
                             */
            //System.out.print(hotelBuild[floor][room]=new Room());
            //Add room number here.
            //Increase room number

        }
  //floorNumber increase
  //roomNumber reset
    }

一旦你实现了这一点,你可以尝试使用你在循环(房间和地板)中的变量进行一些小的改动。

答案 2 :(得分:0)

for (int floor=0; floor<hotelBuild.length; floor++){
    for (int room=0; room<hotelBuild[floor].length; room++){
        hotelBuild[floor][room]=new Room();
        // room number format: xxyy, x = floor, y = room
        // this assumes maximum 99 rooms per floor, it should be enough
        hotelBuild[floor][room].setNumber((floor + 1) * 100 + room + 1);
    }
}

答案 3 :(得分:0)

你可以通过例如构造函数传递它,或者在类Room(教室ahahah)中使用setter设置

例如:

for (int floor=0; floor<hotelBuild.length; floor++){
        for (int room=0; room<hotelBuild[floor].length; room++){
            hotelBuild[floor][room]=new Room((floor+1)*100+room+1);
        }
    }

在Room类中,您可以添加仅包含房间号作为参数

的构造函数
class Room{
  public Room(int roomNumber){
    this.roomNumber = roomNumber;
  }
...
}