【发布时间】:2020-10-31 18:51:06
【问题描述】:
我在检查包含一些布尔字段的组合类的布尔值时遇到了一些问题。如果为 true,将打印一条打印行语句,但我不确定是否未检测到布尔值是否已通过驱动程序中的对象创建进行初始化,或者我的代码逻辑是否有问题。
我仍在学习 Java,如果我有很多低效的代码选择,我很抱歉,但在大多数情况下,程序正在运行,我只是无法在相应的布尔 rolltype 之后打印出 Burger rollType是可变的。我什至不确定滚动类型的布尔值是否被检查或设置正确。
目前,我正在使用 getBreadRoll().iswhateverrolltype() 从复合类 BreadRoll 获取卷类型,然后将其传递到相应卷类型的布尔变量中,然后在条件中检查以将字符串变量 rollType 设置为卷类型,然后在字符串中用于打印输出。
这是代码。
这是添加方法,打印出汉堡的选定部分和每件商品的价格。
public String additions() {
String additions = "The "+getClass().getSimpleName()+" you made consists of a \n";
boolean isWhiteCH = getBreadRoll().isWhiteRoll();
boolean isWheatCH = getBreadRoll().isWheatRoll();
boolean isBrownRyeCH = getBreadRoll().isBrownRyeRoll();
String rollType="";
if(isWhiteCH){
rollType = "White Roll";
}
else if(isWheatCH){
rollType = "Wheat Roll";
}
else if(isBrownRyeCH){
rollType = "Brown Rye Roll";
}
boolean isBreadRollCH=getBreadRoll().isBrownRyeRoll() || getBreadRoll().isWheatRoll() || getBreadRoll().isWhiteRoll();
boolean isMeatCH=isMeat();
boolean isLettuceCH=isLettuce();
boolean isTomatoCH=isTomato();
boolean isCarrotCH=isCarrot();
for(int i=0; i < getCount(); i++){
if(isWhiteCH){
additions += " "+rollType+" at $"+getFbreadRollPrice()+ ", \n";
isWhiteCH = false;
continue;
}else if(isWheatCH){
additions += rollType+" at $"+getFbreadRollPrice()+ ", \n";
isWheatCH = false;
continue;
}else if(isBrownRyeCH){
additions += rollType+" at $"+getFbreadRollPrice()+ ", \n";
isBrownRyeCH = false;
continue;
}
else if(isMeatCH){
additions +="Meat patty at $"+getFmeatPrice()+ ", \n";
isMeatCH = false;
continue;
}
else if(isLettuceCH){
additions +="a piece of lettuce at $"+getFlettucePrice()+", \n";
isLettuceCH = false;
continue;
}
else if(isTomatoCH){
additions +="a tomato at $"+getFtomatoPrice()+", \n";
isTomatoCH = false;
continue;
}
else if(isCarrotCH){
additions +="some carrots at $"+getFcarrotPrice()+", \n";
isCarrotCH = false;
continue;
}
}//end for loop
return additions + "And the final total for the "+getClass().getSimpleName()+" \n" +
"is $"+getPrice()+".";
}//end additions Method
这是包含我要检查的字段的 BreadRoll 复合类。它不是任何类的子类,而是 BreadRoll 类的对象,并在其他类中用作字段。
public class BreadRoll {
private boolean whiteRoll;
private boolean wheatRoll;
private boolean brownRyeRoll;
public BreadRoll(boolean whiteRoll, boolean wheatRoll, boolean brownRyeRoll) {
if(wheatRoll){
this.whiteRoll = whiteRoll;
this.wheatRoll = false;
this.brownRyeRoll = false;
}else if(wheatRoll){
this.whiteRoll = false;
this.wheatRoll = wheatRoll;
this.brownRyeRoll = false;
}else if(brownRyeRoll){
this.whiteRoll = false;
this.wheatRoll = false;
this.brownRyeRoll = brownRyeRoll;
}
}//end constructor
以下是我为驱动程序输入的字段以及产生的输出。
public static void main(String[] args) {
Hamburger JaysHam = new Hamburger(new BreadRoll(true, false, false),
true, true, false, false);
System.out.println(JaysHam.getPrice());
System.out.println(JaysHam.additions());
这是输出。
5.29
The Burger you made consists of a
Meat patty at $1.50,
a piece of lettuce at $0.50,
And the final total for the Hamburger
is $5.29.
在字符串“您制作的汉堡由 a 组成”之后,Roll 类型应与价格一起打印。
【问题讨论】:
-
additions方法被切断了,但我猜你忘了附加rollType。请提供minimal reproducible example。 -
对不同的“种类”有不同的方法是一种可怕的代码气味。它不仅效率低下,而且是一种无法清晰传达的数据模型。您应该有子类 (
WhiteRoll extends BreadRoll) 或枚举 (BreadType.WHITE),它是BreadRoll上的一个属性。 -
你的 BreadRoll 类从不检查 whiteRoll,并检查wheatRoll 两次
-
我认为使用枚举来完全替换 BreadRoll 类是个好主意。
-
@shmosel 我会发布完整的
additions方法。