【发布时间】:2014-11-27 00:55:49
【问题描述】:
我正在编写一个程序,它提供酒店住宿的最终账单,其中包含房间号、客人数量、住宿时间和每人每晚的价格等参数。
我的 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);
}
}
BB Room 类:
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”
任何帮助将不胜感激。谢谢!
附:我也更喜欢将我的 BBCottage 方法变成一个子类,但我不太确定如何实现这一点。如果我也能在这方面得到一些指导,那就太好了!
【问题讨论】:
-
为什么你的构造函数什么都不做?一个理智的人怎么会期望一个什么都不做的函数调用来做某事?
标签: java string class methods subclass