2014-11-27 1 views
0

방 #, 숙박 인 수, 체류 기간 및 1 박 1 인당 가격에 대한 매개 변수가있는 호텔에서의 최종 청구서를 제공하는 프로그램을 작성 중입니다.주 메소드가 클래스 문자열을 잘못 표시합니까?

그러나 BBRoom 클래스에서는 수학이 정확하지만 클래스 매개 변수의 정보를 처리하지는 않습니다.

드라이버 클래스 :

public class BedAndBreakfastDriver { 

public static void main(String[] args) { 

    //float price, int rmNum, int numOccupants, int duration 
    //or 
    //int rmNum, int numOccupants, int duration, float price 

    BBRoom Smith; 

    Smith = new BBRoom(29.95, 16, 1, 5); 

    System.out.println(Smith); 

} 

}

BBRoom 클래스 : 어떤 이유

public class BBRoom { 

final int MAXCAP = 4; 
final int MINCAP = 1; 
final int MINSTAY = 1; 

int room; 
int persons; 
int nights; 
double cost; 
double surcharge; 
double cleanUp; 
double cottageCost; 

NumberFormat fmt1 = NumberFormat.getCurrencyInstance(); 

BBRoom(double price, int rmNum, int numOccupants, int duration){} 


BBRoom(int rmNum, int numOccupants, int duration, double price){} 

     private int getRoomNumber(int rmNum){ 
      room = rmNum;  
      return room; 
     } 

     private int getPersons(int numOccupants){ 
      if (numOccupants < MINCAP){ 
       persons = MINCAP; 
      }if(numOccupants > MAXCAP){ 
       persons = MAXCAP; 
      }else{ 
       persons = numOccupants; 
      } 
      return persons; 
      } 

     private int getNights(int duration){ 

      if(duration < MINSTAY){ 
       nights = MINSTAY; 

      }else{ 

       nights = duration; 

      } 

      return nights; 
     } 

     private double setCost(double price){ 
      return price; 

     } 
     private double getCost(double price){ 



     if (persons < 2){ 
      cost = price*2*nights; 
      }else{ 
      cost = (persons*price*nights); 
     } 

      return cost; 
      } 

     private double BBCottage(double cost){ 

       surcharge = (12.95*nights); 

       cleanUp = 47.99; 

       cottageCost = cost+surcharge+cleanUp; 

       return cottageCost; 

      } 

     public String toString(){ 

       String bill = ("Room Number"+room+" "+"Guests:"+persons+" "+"Nights:"+nights+" "+"Basic Package:"+ fmt1.format(cost)+" "+"Cottage Upgrade:"+fmt1.format(cottageCost)); 
       return bill; 
      } 

     } 

내가

"룸 등 모든 변수를 표시하는 출력을 점점 계속 : 0 손님 : 0 박 : 0 기본 패키지 : 0.00 코티지 업 그레 이드 : 0.00 "

도움이 될 것입니다. 감사!

P. BBCottage 메서드를 하위 클래스로 변환하는 것을 선호하지만이를 수행하는 방법을 잘 모르겠습니다. 내가 그것에 대한 지침을 얻을 수 있다면 좋을 것입니다!

+0

을합니까? 제정신 인 사람은 아무 것도하지 않고 무엇인가를하는 함수 호출을 어떻게 기대합니까? – hexafraction

답변

2

귀하의 생성자는 빈 몸 모든 관련 필드가 0의 기본 값으로 초기화된다

BBRoom(double price, int rmNum, int numOccupants, int duration){} // <<<<<<< nothing going on in {} 

있습니다. 당신이 원하는 것을하기 위해 생성자를 구현하십시오.

0

실제로 BBRoom 내부에 값을 설정하지 않았습니다. 또한 getVariable (int something) 메서드는 별도의 get() 및 set (int something) 메서드로 분할해야합니다. 그렇지 않으면 변수를 동시에 가져오고 설정하기 때문입니다.

toString 메서드에서 get()에서 반환되는 값을 호출하지만 설정되지 않은 값은 호출합니다. 이 문제를 해결하려면 생성자 내부에서 설정해야합니다.

0

생성자가 초기 값을 설정해야합니다.

BBRoom : 다른 클래스로 코티지 분리에 관해서는

public class BBRoom { 

final int MAXCAP = 4; 
final int MINCAP = 1; 
final int MINSTAY = 1; 

int room; 
int persons; 
int nights; 
double cost; 
double surcharge; 
double cleanUp; 
double cottageCost; 

NumberFormat fmt1 = NumberFormat.getCurrencyInstance(); 

BBRoom(double price, int rmNum, int numOccupants, int duration){ 
    someInstanceVariable = price; 
    room = rmNum; 
    persons = numOccupants; 
    ..and so on.. 
} 


BBRoom(int rmNum, int numOccupants, int duration, double price){ 
    ..do the same here.. 
} 

     private int getRoomNumber(int rmNum){ 
      room = rmNum;  
      return room; 
     } 

     private int getPersons(int numOccupants){ 
      if (numOccupants < MINCAP){ 
       persons = MINCAP; 
      }if(numOccupants > MAXCAP){ 
       persons = MAXCAP; 
      }else{ 
       persons = numOccupants; 
      } 
      return persons; 
      } 

     private int getNights(int duration){ 

      if(duration < MINSTAY){ 
       nights = MINSTAY; 

      }else{ 

       nights = duration; 

      } 

      return nights; 
     } 

     private double setCost(double price){ 
      return price; 

     } 
     private double getCost(double price){ 



     if (persons < 2){ 
      cost = price*2*nights; 
      }else{ 
      cost = (persons*price*nights); 
     } 

      return cost; 
      } 

     private double BBCottage(double cost){ 

       surcharge = (12.95*nights); 

       cleanUp = 47.99; 

       cottageCost = cost+surcharge+cleanUp; 

       return cottageCost; 

      } 

     public String toString(){ 

       String bill = ("Room Number"+room+" "+"Guests:"+persons+" "+"Nights:"+nights+" "+"Basic Package:"+ fmt1.format(cost)+" "+"Cottage Upgrade:"+fmt1.format(cottageCost)); 
       return bill; 
      } 

     } 

을 :

BBCottage

을 :

편집
public class BBCottage { 
    public BBCottage(double cost){ 

      surcharge = (12.95*nights); 

      cleanUp = 47.99; 

    } 

    public double getCottage() { 
    cottageCost = cost+surcharge+cleanUp; 

    return cottageCost; 
    } 
    } 

: 나는 또한 실현 주, 당신이하고있는 것을 System.out.println(Smith);. 스미스가 아닌이 방법을 인쇄했습니다. 그래서 우선, 내가 BBRoom가 getter와 setter를 가지고 변경 것 :

void setRmNum(int rm) { 
    room = rm; 
} 

int getRmNum() { 
    return room; 
} 

왜 생성자는 절대적으로 아무것도없는 그런 다음 주에, System.out.println(Smith.getRmNum());

+0

그래서 생성자를 변경했지만 프로그램은 getCost 메서드를 통해 비용을 계산하지 않는 것 같고 손님 수가 2 명에서 4 명 사이인지 확인하지 않습니다. – user3579780

+0

아 나는 그 문제를 안다. 당신은'System.out.println (Smith); '하고있다. 실제로 메소드를 실행해야합니다. 그래서,'System.out.println (Smith.getRoomNumber (2));'. 또한, 나는이 방법들에서 매개 변수들을 제거 할 것이다. 분할하여 setRoomNumber (int rm) 및 getRoomNumber()를 갖습니다. –

관련 문제