Checking Field boolean value in Java Composition Class

Tags: , ,

I’m having some trouble checking the boolean value for a composition class that holds a few boolean fields. If true a print line statement will print but I’m not sure if the boolean value is not getting detected that it been initialized with the object creation in the driver or if there is something wrong with the logic of my code.

I’m still learning Java and sorry if I have quite a bit of inefficient code choices, but for the most part, the program is working, I just can’t get the Burger rollType to print out after the corresponding boolean rolltype is varified. I’m not even sure if the boolean for the roll-type is even being checked or set correctly.

Currently, I am using getBreadRoll().iswhateverrolltype() to get the roll type from the composite class BreadRoll and then pass it into a boolean variable of the corresponding rolltype then be checked in a conditional to set the String variable rollType to the roll type and then used in the String for print out.

Here is the code.

This is the additions Method that prints out the selected parts of the burger and price per item.

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="";

        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++){
            additions += " "+rollType+" at $"+getFbreadRollPrice()+ ", n";
            isWhiteCH = false;
        }else if(isWheatCH){
            additions += rollType+" at $"+getFbreadRollPrice()+ ", n";
            isWheatCH = false;
        }else if(isBrownRyeCH){
            additions += rollType+" at $"+getFbreadRollPrice()+ ", n";
            isBrownRyeCH = false;
        else if(isMeatCH){
            additions +="Meat patty at $"+getFmeatPrice()+ ", n";
            isMeatCH = false;
        else if(isLettuceCH){
            additions +="a piece of lettuce at $"+getFlettucePrice()+", n";
            isLettuceCH = false;
        else if(isTomatoCH){
            additions +="a tomato at $"+getFtomatoPrice()+", n";
            isTomatoCH = false;
        else if(isCarrotCH){
            additions +="some carrots at $"+getFcarrotPrice()+", n";
            isCarrotCH = false;
    }//end for loop

    return additions + "And the final total for the "+getClass().getSimpleName()+" n" +
            "is $"+getPrice()+".";
}//end additions Method

Here is the BreadRoll Composite Class that holds the fields that I’m trying to check. Its not a child of any of the classes but an object of The BreadRoll class and is used as a field in the other Classes.

public class BreadRoll {

    private boolean whiteRoll;
    private boolean wheatRoll;
    private boolean brownRyeRoll;

    public BreadRoll(boolean whiteRoll, boolean wheatRoll, boolean brownRyeRoll) {

            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

Here are the fields that I’m entering in for the driver and the output that results.

public static void main(String[] args) {
        Hamburger JaysHam = new Hamburger(new BreadRoll(true, false, false),
                true, true, false, false);


Here is the output.

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.

After the String ‘The burger you made consists of a’ the Roll type should print along with the price.


I just ended up changing the class for BreadRoll to an enum and then checking the enum states for the additions method. That was much simpler and solved the problem Credit goes to @Chrylis and @tgdavies.

Thanks guys

Source: stackoverflow