Java

OOP GUIDE / WORK

 

BOTTLE CLASS SOLUTIONS

 

 

TASK – PART 1 – BOTTLE CLASS

 

Here is my solution.

 

 

public class Bottle

{

     private String content;  //ex: water, "" means empty

     private int maxVolume;   //in mL (so int works here)

     private int filledVolume;  //also in mL

     private String colour;

    

     public Bottle(int mv, String col)

     {

           maxVolume = mv;

           colour = col;

           content = "";

           filledVolume = 0;

     }

    

     public int getFilledVolume()

     {

           return filledVolume;

     }

    

     public int getMaxVolume()

     {

           return maxVolume;

     }

    

     public String getColour()

     {

           return colour;

     }

    

     public String getContent()

     {

           if (content.equals(""))

           {

                return "nothing";

           }

           else

           {

                return content;

           }

     }

    

     public double getRemainingVolume()

     {

           return maxVolume - filledVolume;

     }

    

     public double getPercentageFull()

     {

           return (double)filledVolume / maxVolume;   //0.00 to 1.00

     }

    

     public double getPercentageEmpty()

     {

           return (double)getRemainingVolume() / maxVolume;  //0.00 to 1.00

     }

    

     public boolean isEmpty()

     {

           return filledVolume == 0;  //interesting solution

     }

    

     public boolean isFull()

     {

           return filledVolume == maxVolume;  //interesting solution

     }

    

     public boolean contains(String liquid)

     {

           if (content.contains(liquid))

           {

                return true;

           }

           else

           {

                return false;

           }

     }

    

     public void pourOut()

     {

           content = "";

           filledVolume = 0;

     }

    

     public void pourOut(int volumeToRemove)

     {

           if (volumeToRemove > filledVolume)

           {

                System.out.println("Error.  The bottle doesn't have that much in it.  Bottle emptied.");

                pourOut();

           }

           else

           {

                filledVolume = filledVolume - volumeToRemove;

                if (filledVolume == 0)

                {

                      content = "";

                }

           }

          

     }

    

     public String toString()

     {

           return colour + " bottle (" + maxVolume + "mL) contains " + getContent() + " (" + (getPercentageFull()*100) + "% full).";

     }

    

     public void addLiquid(String newLiquid, double addedVolume)

     {

           if (isFull())

           {

                System.out.println("Overflow - bottle is already full.");

           }

           else if (isEmpty())

           {

                content = newLiquid;

                filledVolume += addedVolume;               

                if (filledVolume > maxVolume)

                {

                      filledVolume = maxVolume;

                    System.out.println("Overflow.  Bottle is now full.");

                }

           }

           else

           {

                if (!content.contains(newLiquid))

                {

                      content = content + " & " + newLiquid;

                }

                filledVolume += addedVolume;               

                if (filledVolume > maxVolume)

                {

                      filledVolume = maxVolume;

                    System.out.println("Overflow.  Bottle is now full.");

                }

           }

     }

}

 

 

 

 

 

TASK – PART 2 – BOTTLE CLASS TESTER

 

No solution needed

 

 

TASK – PART 3 – BOTTLE CLASS TESTER 2

 

No solution needed.