One-line summary

In this exercise you create cocktails from ingredients and calculate size, cost, and alcohol percentage.

Exercise description

This exercise uses four classes: Beverage, NonAlcoholic, Alcoholic, and Cocktail.

Beverage is already complete and describes a beverage, consisting of a string name, a float cost, and a float size. The cost is given in dollars, and size indicates the size of one unit of the beverage, in liters.

NonAlcoholic is already complete, and it is a Beverage.

Alcoholic you have to create. It is a Beverage, whereby one extra parameter is given on creation, namely the alcohol percentage, which is a float. Store this in the attribute self.alcohol. Make sure that the alcohol percentage is also shown by the __repr__() method.

Cocktail you have to complete. A cocktail consists of a string name, and a dictionary ingredients. The keys used for the dictionary ingredients are Beverages. The corresponding value is the number of units of the beverage in the cocktail. You have to fill in the following methods for this class:

UML diagram UML Diagram

Example

The following code creates cola and lemon juice. They are both non-alcoholic. The size of a unit of cola is 0.1L, and the size of a unit of lemon juice is 0.01L. For one unit of cola you pay 80 cents, and for a unit of lemon juice you pay 55 cents.

cola = NonAlcoholic( "Cola", 0.80, 0.1 )
lemonjuice = NonAlcoholic( "Lemon juice", 0.55, 0.01 )

The following code creates rum, which is alcoholic. A unit of rum is 0.05L, which costs $2.65. The alcohol percentage is 40%.

rum = Alcoholic( "Rum", 2.65, 0.05, 40 )

The following code creates a cocktail Cuba Libre, which mixes 1.5 units of cola, 2 units of rum, and 1 unit of lemon juice.

cubalibre = Cocktail( "Cuba Libre" )
cubalibre.add_ingredient( cola, 1.5 )
cubalibre.add_ingredient( rum, 2 )
cubalibre.add_ingredient( lemonjuice, 1 )

Printing the cocktail using the function cocktailline() which is given in the code, produces as output:

Cuba Libre, $7.05, 0.26L, 15.38%

To explain:

Copy this code and edit it:

# Name:
# Student number:

class Beverage:
    def __init__( self, name="", cost=0.0, size=0.1 ):
        self.name = name
        self.cost = cost
        self.size = size
    def __repr__( self ):
        return f"{self.name}, ${self.cost:.2f}, {self.size}L"

class NonAlcoholic( Beverage ):
    pass
        
# Create a class Alcoholic, which is a Beverage which on creation gets one more parameter,
# namely an alcohol percentage. Store it in the attribute self.alcohol.
# Make sure that this extra parameter is also shown when the __repr__() method is used.

class Cocktail:
    def __init__( self, name="" ):
        self.name = name
        self.ingredients = {} a
        # Dictionary of ingredients, whereby the key is a Beverage and the value is the amount.
    def add_ingredient( self, beverage, count ):
        # Fill in the add_ingredient() method, which adds the given beverage count times
        # to the cocktail.
        pass
    def cost( self ):
        # Return the total cost of the cocktail, which is the sum of the costs of all the
        # ingredients.
        return 0.0
    def size( self ):
        # Return the total size of the cocktail, which is the sum of the sizes of all the
        # ingredients.
        return 0.0
    def alcohol( self ):
        # Return the alcohol percentage of the cocktail, which is the percentage of the
        # complete cocktail which is alcohol.
        return 0.0
    def __repr__( self ):
        return self.name + ", " + str( self.ingredients )

def cocktailline( cocktail ):
    return f"{cocktail.name}, ${cocktail.cost():.2f}, {cocktail.size():.2f}L, {cocktail.alcohol():.2f}%"