2013-09-03 2 views
1

호텔 게스트 서비스 생성과 관련된 클래스에 대한 지정입니다. 아래에 8d "floor", 20 "rooms"의 2 차원 어레이를 만들고 Room 개체로 채 웁니다. 현재 중첩 된 for 루프를 사용하여 각 Room 개체를 검토하고 방 번호를 할당하려고합니다. 예를 들어, 1 층에는 101-120 실이 포함됩니다.2d 객체가 포함 된 배열

아래 클래스는 내가 사용하고있는 테스트 클래스입니다.

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

다음은 변수, 세터, 게터뿐만 아니라 toString() 재정의 메서드를 포함 클래스 Room입니다.

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 객체 고유의 방 번호를 할당합니까? 그런 다음 올바른 순서로 숫자를 넣어 당신이 alredady varables를 사용하는 방법을 원하는 경우

+2

귀하의 질문은 무엇입니까? – Thilo

+0

배열의 각 Room 객체에 고유 한 실 번호를 어떻게 할당합니까? – Stickandjab

+0

방 번호 만 신경 쓰면 방 번호에 세터가 필요합니다. 특별히 1 층에 101-120 개의 방이 들어 있기를 원할 경우 ((1 + floor) * 100) + 1 + 방을 계산하고 값을 – ameer

답변

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; 
+0

감사합니다. Thilo 도움이됩니다. – Stickandjab

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 
    } 

) 격차를 채 웁니다.

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

당신은 예를 들어, 생성자를 통해 전달하거나 클래스 룸에서 세터 (교실 ahahah) 예를 들어

으로 설정할 수 있습니다

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

그리고 당신의 방 클래스, 당신은 방 번호가 매개 변수로만 포함 된 생성자를 추가 할 수 있습니다.

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

교실 ~ 교실, 갑자기 실현 8U – Stickandjab