Search code examples
javaif-statementbluej

If statement not running correctly


What I'm trying to do is get my program to run a method to make lemonade (makeLemonade) if and only if I have enough ingredients to make the lemonade, but I keep getting an error from the tests provided for me that tells my lemonade is being made even when I don't have enough ingredients.

Here's the code I have right now for the if statement that's giving me some trouble. I've tried using && and || along with different mixes of >= and > to no avail so far.

 public int makeLemonade() {
    if (lemons >= 6 && gallonsOfWater >= 1 && cupsOfSugar >= 1 && emptyGlasses >= 8) {
        lemons = lemons - 6;
        gallonsOfWater = gallonsOfWater - 1;
        cupsOfSugar = cupsOfSugar - 1; 
        emptyGlasses = emptyGlasses - 8;
        glassesOfLemonade = glassesOfLemonade + 8;
        return glassesOfLemonade;
    } else {
        return 0;
    }
}

This is the error my test is giving me right now

"Test of method makeLemonade failed for activty 3.

The following code was executed:

LemonadeStand ls = new LemonadeStand(5, 2, 2, 16, 1.1);
ls.makeLemonade();

Fields were modified even though there was not enough lemons available to make lemonade."

Here is the entire code so far

    /**
 * LemonadeStand.java
 * 
 */

//Put any imports below this line.

/**
 * Short, one-line description of LemonadeStand class here.
 * 
 * Optionally, include a paragraph that provides a more 
 * detailed description.
 *
 * @author Nicholas Thomas 
 * @version 2/19/2018
 */
public class LemonadeStand
{
    //Put instance variables below this line.  
    private int lemons;
    private int gallonsOfWater;
    private int cupsOfSugar;
    private int emptyGlasses;
    private double price;
    private double income;
    private int glassesOfLemonade;
    /** No arg constructor.
     * LemonadeStand Constructor
     *
     */
    public LemonadeStand()
    {
        lemons = 0; 
        gallonsOfWater = 0;
        cupsOfSugar = 0;
        glassesOfLemonade = 0;
        emptyGlasses = 0;
        price = 0;
        income = 0;
    }

    /** Contructor.
     * LemonadeStand Constructor
     *
     * @param newLemons A parameter
     * @param newGallonsOfWater A parameter
     * @param newCupsOfSugar A parameter
     * @param newEmptyGlasses A parameter
     * @param newPrice A parameter
     */
    public LemonadeStand(int newLemons, int newGallonsOfWater, 
    int newCupsOfSugar, int newEmptyGlasses, double newPrice)
    {
        setLemons(newLemons); 
        setGallonsOfWater(newGallonsOfWater);
        setCupsOfSugar(newCupsOfSugar);
        setEmptyGlasses(newEmptyGlasses);
        setPrice(newPrice); 
        glassesOfLemonade = 0;
        income = 0;
    }

    /** Main method of the program.
     * Method main
     *
     * @param args A parameter
     */
    public static void main(String[] args)
    {
        LemonadeStand lemonadeStand = new LemonadeStand(15, 3, 4, 20, 1.5);
        lemonadeStand.makeLemonade();
        System.out.println(lemonadeStand.getLemons());
        System.out.println(lemonadeStand.getGallonsOfWater());
        System.out.println(lemonadeStand.getCupsOfSugar());
        System.out.println(lemonadeStand.getGlassesOfLemonade());
    }

    /** Mutator to change the amount of lemons.
     * Method setLemons
     *
     * @param newLemons A parameter
     * @return newLemons
     */
    public int setLemons(int newLemons)
    {
        if (lemons < 0)
        {
            lemons = newLemons;
            return newLemons;
        }
        else
        { 
            return 0;
        }

    }

    /** Mutator to change gallons of water.
     * Method setGallonsOfWater
     *
     * @param newGallonsOfWater A parameter
     * @return gallonsOfWater
     */
    public int setGallonsOfWater(int newGallonsOfWater)
    {
        if (gallonsOfWater < 0)
        {
            gallonsOfWater = newGallonsOfWater;
            return gallonsOfWater;
        }
        else
        {
            return 0;
        }
    }

    /** Mutator to set cups of sugar.
     * Method setCupsOfSugar
     *
     * @param newCupsOfSugar A parameter
     * @return cupsOfSugar
     */
    public int setCupsOfSugar(int newCupsOfSugar)
    {
        if (cupsOfSugar < 0)
        {
            cupsOfSugar = newCupsOfSugar;
            return cupsOfSugar;
        }
        else
        { 
            return 0;
        }
    }

    /** Mutator to modify the number of empty glasses.
     * Method setEmptyGlasses
     *
     * @param newEmptyGlasses A parameter
     * @return emptyGlasses
     */
    public int setEmptyGlasses(int newEmptyGlasses)
    {
        if (emptyGlasses < 0)
        {
            emptyGlasses = newEmptyGlasses;
            return emptyGlasses;
        }
        else
        {
            return 0;
        }
    }

    /** Mutator to modify the glasses of lemonade.
     * Method setGlassesOfLemonade
     *
     * @param newGlassesOfLemonade A parameter
     * @return glassesOfLemonade
     */
    public int setGlassesOfLemonade(int newGlassesOfLemonade)
    {
        if (glassesOfLemonade < 0)
        {
            glassesOfLemonade = newGlassesOfLemonade;
            return glassesOfLemonade;
        }
        else
        {
            return 0;
        }
    }

    /** Mutator to change the price.
     * Method setPrice
     *
     * @param newPrice A parameter
     * @return price
     */
    public double setPrice(double newPrice)
    {
        if (price < 0)
        {
            price = newPrice;
            return price;
        }
        else
        {
            return 0;
        }
    }

    /** Mutator to set the income.
     * Method setIncome
     *
     * @param newIncome A parameter
     * @return income
     */
    public double setIncome(double newIncome)
    {
        if (income < 0)
        {
            income = newIncome;
            return income;
        }
        else
        {
            return 0;
        }
    }

    /** Accessor to make lemonade.
     * Method makeLemonade
     *
     * @return The return value
     **/

    public int makeLemonade()
    {
        if (lemons >= 6 && gallonsOfWater >= 1 && cupsOfSugar >= 1 
        && emptyGlasses >= 8)
        {
            lemons -= 6;
            gallonsOfWater -= 1;
            cupsOfSugar -=  1; 
            emptyGlasses -= 8;
            glassesOfLemonade += 8;
            return glassesOfLemonade;
        }
        else 
        {
            return 0;
        }
    }

    /** Accessor to lemonade selling.
     * Method sellLemonade
     *
     * @return The return value
     */
    public int sellLemonade()
    {
        if (glassesOfLemonade <= 1)
        {
            makeLemonade();
            return 0;}
        else 
        {
            glassesOfLemonade = glassesOfLemonade - 1;
            income = income + price;
            return glassesOfLemonade; 
        }

    }

    /** Accessor to get number of lemons.
     * Method getLemons
     *
     * @return The return value
     */
    public int getLemons()
    {
        return lemons;
    }

    /** Accessor to return gallons of water.
     * Method getGallonsOfWater
     *
     * @return The return value
     */
    public int getGallonsOfWater()
    {
        return gallonsOfWater;
    }

    /** Accessor to return cups of sugar.
     * Method getCupsOfSugar
     *
     * @return The return value
     */
    public int getCupsOfSugar()
    {
        return cupsOfSugar;
    }

    /** Accessor to return the value of empty glasses.
     * Method getEmptyGlasses
     *
     * @return The return value
     */
    public int getEmptyGlasses()
    {
        return emptyGlasses;
    }

    /** Accessor to return glasses of lemonade.
     * Method getGlassesOfLemonade
     *
     * @return The return value
     */
    public int getGlassesOfLemonade()
    {
        return glassesOfLemonade;
    }

    /** Accessor to return the price.
     * Method getPrice
     *
     * @return The return value
     */
    public double getPrice()
    {
        return price;
    }

    /** Accesor to return the income rate.
     * Method getIncome
     *
     * @return The return value
     */
    public double getIncome()
    {
        return income;
    }

    /** Accessor for lemonade selling.
     * Method sellMoreLemonade
     *
     * @param requestedGlasses A parameter
     * @return The return value
     */
    public int sellMoreLemonade(int requestedGlasses)
    {
        return 0;
    }
}

Solution

  • The main issue with the code is the setter-usage. For example:

    if (gallonsOfWater < 0) {
      gallonsOfWater = newGallonsOfWater;
      return gallonsOfWater;
    } else {
        return 0;
    }
    

    The gallonsOfWater field of LemonadeStand is initialized e.g. in the 2nd constructor by the call setGallonsOfWater(newGallonsOfWater);.

    Let us assume that you pass the value 3 to this constructor, so it will be setGallonsOfWater(3);. In the setter this leads to 3 < 0 and hence

    • the return value of 0 for the setter.
    • not setting the value for the field gallonsOfWater.

    (I assume that debugging would have helped here also)