NEED HELP ASAP: logical if statement?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • daxco21
    New Member
    • Mar 2008
    • 3

    NEED HELP ASAP: logical if statement?

    I have been working on this simple project for a week, and come to the point where I feel like I need outside help. For the life of me I can't figure out what is wrong with this "sellIceCream(. ..)" method. I think theres something wrong with my if statement, because even if i have enough "ingredient s", it still prints my "not enough" statement instead of selling.

    The error that the online tester for this project gave me was:

    "Test results indicate that your code still contains bugs. Your code appears to cover only 56% of the behavior required for this assignment.

    Double check that you have carefully followed all initial conditions requested in the assignment in setting up your solution, and that you have also met all requirements for a complete solution in the final state of your program.

    The following hint(s) may help you locate some ways in which your solution may be improved:

    *

    sell ice cream
    *

    multi sell I ice cream
    *

    sell ice cream no ice cream

    (only 3 of 4 hints shown)"

    I think I set all the initial conditions correctly. In anycase, I don't know what's wrong. This is my whole program:


    Code:
    public class IceCreamStation
    {
        /** instance variables.*/
        /**amount of cash in station.*/
        private double numCash;
        /**number of cones.*/
        public int numCones;
        /**amount of cash used to buy cones.*/
        public double cashBuyCones;
        /**amount of cash used to buy ice cream.*/
        public double cashBuyIceCream;
        /**number of cones sold.*/
        public int numSold;
        /**amount of ice cream in pints.*/
        public double numIceCream;
        /**price per pint of ice cream.*/
        public static final double PRICEPERPINT = 3.25;
        /**price per pint box of cones.*/
        public static final double PRICEPERBOX = 2.50;
        /**amount of cones in box.*/
        public static final int CONESPERBOX = 10;
        /**amount of pints to make ice cream cone.*/
        public static final double QUARTERPINT = .25;
    
        /**
         * Constructor for objects of class IceCreamStation.
         * @version 2008.02.14
         */
        public IceCreamStation()
        {
            // initialise instance variables
            numCones = 0;
            numIceCream = 0;
            cashBuyCones = 0; 
            cashBuyIceCream = 0;
            numSold = 0;
            numCash = 100.00;
        }
        
        /**
         * Constructor for input start cash.
         * 
         * @param startCash   starting cash
         */
        public IceCreamStation(double startCash)
        {
            // initialise instance variables
            numCones = 0;
            numIceCream = 0;
            cashBuyCones = 0; 
            cashBuyIceCream = 0;
            numSold = 0;
            numCash = startCash;
            
            
        }
    
        /**
         * buys cones by the box.
         * 
         * @param buyNumBox  number of boxes
         * */
        public void buyCones(int buyNumBox) 
        {
            cashBuyCones = buyNumBox * PRICEPERBOX;
            
            if (numCash >= cashBuyCones)
            {
                numCones = numCones + buyNumBox * CONESPERBOX;
                numCash = numCash -  cashBuyCones;
            }
            else
            {
                System.out.println("Not enough cash to buy cones");
            }
        }
        
        /**
         * buys ice cream by the pint.
         * 
         * @param numPints  number of pints
         * */
        public void buyIceCream(double numPints)
        {
            cashBuyIceCream = numPints * PRICEPERPINT;
            if (numCash >= cashBuyIceCream)
            {
                numCash = numCash - cashBuyIceCream;
                numIceCream = numIceCream + numPints;
            }
            else
            {
                System.out.println("Not enough cash to buy ice cream");
            }
        }
        
        /**
         * sells ice cream cones.
         * 
         * @param numToSell  number of ice cream cones 
         * @param unitPrice   cost per cone
         * */
        public void sellIceCreamCones(int numToSell, double unitPrice)
        {
            if (numCones < numToSell || (numIceCream * QUARTERPINT) < numToSell)
            {
                System.out.println("not enough cones or ice cream");
            }
            else
            {
                numSold = numSold + numToSell;
                numCones = numCones - numToSell;
                numIceCream = numIceCream - (numToSell * QUARTERPINT);            
                numCash = numCash + (numToSell * unitPrice);         
            }
        } 
        /**
         * returns number of cones in station.
         * */
        public int getConesLeft()
        {
            return numCones;
        }
        
         /**
         * returns how many pints of ice cream left.
         * */
        public double getIceCreamLeft()
        {
            return numIceCream;
        }
        
         /**
         * returns number of cones sold.
         * */
        public int getNumIceCreamConesSold()
        {
            return numSold;
        }
        
         /**
         * returns amount of cash in station.
         * */
        public double getCashOnHand()
        {
            return numCash;
        }
    }
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Code:
    public void sellIceCreamCones(int numToSell, double unitPrice)
        {
            if (numCones < numToSell || (numIceCream * QUARTERPINT) <numToSell)
            {
                System.out.println("not enough cones or ice cream");
            }
            else
            {
                numSold = numSold + numToSell;
                numCones = numCones - numToSell;
                numIceCream = numIceCream - (numToSell * QUARTERPINT);            
                numCash = numCash + (numToSell * unitPrice);         
            }
        }
    You are comparing Double type value to an integer type value...

    Code:
    if (numCones < numToSell || (numIceCream * QUARTERPINT) <numToSell)
    Update us,
    Sukatoa

    Comment

    • daxco21
      New Member
      • Mar 2008
      • 3

      #3
      If I changed my int to a double, I would be losing percision wouldn't I? Could casting the int as a double work? What would this look like, and where should it go if this is the solution?

      Comment

      • daxco21
        New Member
        • Mar 2008
        • 3

        #4
        i think it would also mess up the equations because the int and double types won't multiply out.

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Using doubles for currency is asking for trouble, because of floating point rounding errors. A better solution is to use int and work with values in cents. If you want to display values like "3.25", you can handle that explicitly.

          Comment

          Working...