java - 检查 Java 组合类中的字段布尔值
问题描述
我在检查包含几个布尔字段的组合类的布尔值时遇到了一些麻烦。如果为真,将打印一条打印行语句,但我不确定是否未检测到布尔值是否已通过驱动程序中的对象创建进行初始化,或者我的代码逻辑是否有问题。
我仍在学习 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 类型应与价格一起打印。
解决方案
我刚刚将 BreadRoll 的类更改为枚举,然后检查添加方法的枚举状态。这要简单得多,并且解决了问题归功于@Chrylis 和@tgdavies。
多谢你们
推荐阅读
- python - 如何将字符串中的模式向前移动一个位置(删除第一个,替换最后一个)?
- html - 多个表单生成的jquery不提交
- r - knitr::is_word_output() 检查当前输出类型是否为单词——就像 knitr::is_latex_output() 和 knitr::is_html_output()
- java - Maven:全新安装时找不到包
- api - Google Place API 提供非英语结果
- css - 如何使用 transparentImage 和 svg 与 firefox 显示过滤/webkitFilter 矩阵颜色?
- ruby-on-rails - 使用设计 gem 的 Rails 用户到用户消息
- python - Pandas Dataframe groupby 一列和所有其他列的总和
- java - Kafka Consumer订阅topic但不消费消息,不出现在消费者组列表中
- elasticsearch - Elasticsearch count api 不返回正确的计数值