检查 Java 组合 Class 中的字段布尔值
Checking Field boolean value in Java Composition Class
我在检查包含几个布尔字段的组合 class 的布尔值时遇到了一些问题。如果为真,将打印一条打印行语句,但我不确定是否未检测到布尔值是否已使用驱动程序中的对象创建对其进行了初始化,或者我的代码逻辑是否有问题。
我还在学习 Java 如果我有很多低效的代码选择,我很抱歉,但在大多数情况下,该程序正在运行,我只是无法打印 Burger rollType在相应的布尔 rolltype 被验证后输出。我什至不确定是否正确检查或设置了滚动类型的布尔值。
目前,我正在使用 getBreadRoll().iswhateverrolltype() 从复合 class BreadRoll 中获取滚动类型,然后将其传递到相应滚动类型的布尔变量中,然后在有条件的情况下进行检查将字符串变量rollType设置为卷类型,然后在字符串中用于打印。
这是代码。
这是 additions 方法,打印出汉堡的选定部分和每件商品的价格。
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 Composite Class。它不是任何 classes 的子元素,而是 The BreadRoll class 的对象,并在其他 Classes 中用作字段。
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 .50,
a piece of lettuce at [=15=].50,
And the final total for the Hamburger
is .29.
在字符串 'The burger you made consists of a' 之后,卷筒类型应与价格一起打印。
我刚刚将 BreadRoll 的 class 更改为枚举,然后检查添加方法的枚举状态。这要简单得多,解决了问题归功于@Chrylis 和@tgdavies。
谢谢大家
我在检查包含几个布尔字段的组合 class 的布尔值时遇到了一些问题。如果为真,将打印一条打印行语句,但我不确定是否未检测到布尔值是否已使用驱动程序中的对象创建对其进行了初始化,或者我的代码逻辑是否有问题。
我还在学习 Java 如果我有很多低效的代码选择,我很抱歉,但在大多数情况下,该程序正在运行,我只是无法打印 Burger rollType在相应的布尔 rolltype 被验证后输出。我什至不确定是否正确检查或设置了滚动类型的布尔值。
目前,我正在使用 getBreadRoll().iswhateverrolltype() 从复合 class BreadRoll 中获取滚动类型,然后将其传递到相应滚动类型的布尔变量中,然后在有条件的情况下进行检查将字符串变量rollType设置为卷类型,然后在字符串中用于打印。
这是代码。
这是 additions 方法,打印出汉堡的选定部分和每件商品的价格。
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 Composite Class。它不是任何 classes 的子元素,而是 The BreadRoll class 的对象,并在其他 Classes 中用作字段。
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 .50,
a piece of lettuce at [=15=].50,
And the final total for the Hamburger
is .29.
在字符串 'The burger you made consists of a' 之后,卷筒类型应与价格一起打印。
我刚刚将 BreadRoll 的 class 更改为枚举,然后检查添加方法的枚举状态。这要简单得多,解决了问题归功于@Chrylis 和@tgdavies。
谢谢大家