terça-feira, 20 de agosto de 2013

Silly season: os atributos de um café Português

O café é uma ciência complexa. 

E não, isto não se trata de mais uma piada sobre Java. O café em Portugal é apreciado pela subtileza das suas configurações e pela pouca subtileza de alguns empregados de mesa. 

O café em Portugal é perfeitamente dominado por um serviço de mesa experiente mas potencialmente confuso para um programador desprovido de cafeína.

Aqui fica uma implementação rudimentar em Python.
 
#!/usr/bin/python
# coding=utf-8

class Length:
        short, normal, long = range (3)

class Sweetner:
        sugar, sweetner = range (2)

class Mixer:
        spoon, cinnamon_stick = range (2)

class Type:
        normal, decaf = range (2)

class Coffee:
        "representation of a portuguese coffee"

        # the members are "semi private" - not meant to be accessed directly    
        def __init__(self, ctype = Type.normal , length = Length.normal , milk_spot = False , cup_preheating = False , sweetening = Sweetner.sugar ,  mixer_type = Mixer.spoon ):
                self.__type = ctype
                self.__length = length
                self.__milk_spot = milk_spot
                self.__cup_preheating = cup_preheating
                self.__sweetening = sweetening
                self.__mixer_type = mixer_type
                # homework: validate input and raise ValueError if it's wrong :-)

        # this function translates the internal representation of Coffee atrributes into an order that can be sent to the waiter
        def __str__(self):
                result = "Um %s %s %s %s %s %s" % ( self.get_type_pt(), self.get_length_pt(), self.get_milk_spot_pt(), self.get_cup_preheating_pt(), self.get_sweetening_pt(), self.get_mixer_type() )
                # clear redundant whitespace on return
                return ' '.join(result.split())+'.'

        def get_type_pt(self):
                if self.__type == Type.normal:
                        return "café"
                return "descafeinado"

        def get_length_pt(self):
                if self.__length == Length.short:
                        return "curto"
                if self.__length == Length.normal:
                        return ""
                if self.__length == Length.long:
                        return "cheio"
                return "erro"

        def get_milk_spot_pt(self):
                if self.__milk_spot == True:
                        return "pingado"
                return ""

        def get_cup_preheating_pt(self):
                if self.__cup_preheating == True:
                        return "em chávena escaldada"
                return ""

        def get_sweetening_pt(self):
                if self.__sweetening == Sweetner.sweetner:
                        return "com adoçante"
                return ""

        def get_mixer_type(self):
                if self.__mixer_type == Mixer.cinnamon_stick:
                        return "e pauzinho de canela"
                return ""

Enough said. Posto isto, há que fazer os pedidos. Experimentem estes.




# café default - for machos
cafe = Coffee()
print cafe

# ultrademanding customer
cafe = Coffee(Type.normal, Length.long, True, True, Sweetner.sweetner, Mixer.cinnamon_stick)
print cafe

# no comments
cafe = Coffee(Type.decaf, Length.normal, False, False, Sweetner.sweetner, Mixer.spoon)
print cafe

 

Sem comentários: