主方法错误地显示类字符串?

时间:2014-11-27 00:55:49

标签: java string class methods subclass

我正在编写一个程序,提供住宿的最终账单,其中包括Room#,客人数量,住宿时间和每人每晚的价格。

我的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.S。我也更喜欢把我的BBCottage方法变成一个子类,但我不太清楚如何实现它。如果我能得到一些指导,那就太棒了!

3 个答案:

答案 0 :(得分:2)

你的构造函数有一个空体

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

所有相关字段都初始化为默认值0。实现构造函数以执行您想要的操作。

答案 1 :(得分:0)

您实际上从未在BBRoom中设置值。此外,方法getVariable(int something)应该分成单独的get()和set(int something)方法,因为否则你会同时获取并设置一个变量。

在toString方法中,调用将从get()返回的值,但永远不会设置这些值。要修复它,您需要在构造函数中设置它们。

答案 2 :(得分: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;
           }

        }

至于将Cottage分成另一类:

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更改为有吸气剂和制定者:

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

int getRmNum() {
    return room;
}

然后,在您的主要内容中,执行System.out.println(Smith.getRmNum());