Skip to content

Instantly share code, notes, and snippets.

Created March 22, 2018 15:00
Show Gist options
  • Save sadmicrowave/73541a76f8133be09c8318c0770c5343 to your computer and use it in GitHub Desktop.
Save sadmicrowave/73541a76f8133be09c8318c0770c5343 to your computer and use it in GitHub Desktop.
A fun Virtual Barista for brewing coffee
import abc, time, sys, itertools
class Container (object):
__meta__ = abc.ABCMeta
level = None
original_level = None
def increase (self, amt=1):
self.level = self.level + amt
def decrease (self, amt=1):
self.level = self.level - amt
def is_empty(self):
return self.level == 0
class Pot (Container):
def __init__(self, level):
self.level = level
self.original_level = level
class Watertank (Container):
def __init__(self, level):
self.level = level
self.original_level = level
class Coffee:
def __init__(self):
self.watertank = Watertank(20)
self.pot = Pot(0)
self.filter = False
self.on = False
self.spoons = 0
self.expresso = 0
def brew(self): # And as requirment
if self.is_ready():
print("Your coffee is brewing...", end="")
spinner = itertools.cycle(['-', '/', '|', '\\'])
while self.on :
self.watertank.decrease( 1 )
self.pot.increase( 1 )
if self.pot.level == self.watertank.original_level and self.watertank.is_empty() :
print("\nThe coffee has been brewed. Enjoy!")
else :
print("Sorry, the brewer is not ready to brew.")
def ready_check(self):
if self.watertank.level == 0 :
print( "The Watertank is not filled." )
if not self.filter :
print( "A filter has not been loaded." )
if self.pot.level > 0 :
print( "The Pot is not empty." )
if not self.on:
print( "The brewer is not turned on." )
def is_ready(self):
return self.watertank.level > 0 \
and self.filter \
and self.spoons > 0\
and self.pot.level == 0 \
and self.on
def add_spoons(self, amt=1):
self.spoons = self.spoons + amt
print("%s spoon(s) of sugar has been added." % amt)
def add_milk(self, milk):
self.milk = milk
print("%s has been added." % self.milk)
def add_expresso(self, expresso):
if expresso:
self.expresso = expresso
print("%s shot(s) of Expresso have been added." % self.expresso)
def install_filter(self):
if not self.filter :
self.filter = True
print("A filter has been installed.")
else :
print("A filter is already installed.")
def turn_on (self):
if not self.on :
self.on = True
print("The brewer is now on!")
print("The brewer is already on!")
def turn_off (self):
if self.on :
self.on = False
print("The brewer is now off!")
else :
print("The brewer is already off!")
class Person (object):
__name = None
# all levels are on a scale of 0-10
expresso = 0
sugar = 0
milk = 0
def name (self):
return self.__name
def name (self, name):
self.__name = name
def __str__ (self):
return "%s" % self.__name
class Rule (object):
def __init__ (self, name, type, min=0, max=0, choose=[]): = name
self.type = type
self.min = min
self.max = max
self.choose = choose
def validate(self, v):
if eval(self.type) == int:
return self.type_compare(v) and self.max_compare(v) and self.min_compare(v)
elif eval(self.type) == str and self.choose:
return self.type_compare(v) and self.choose_compare(v)
def type_compare(self, v):
return type(eval( '%s("%s")' % (self.type, v) )) == eval(self.type)
def min_compare(self, v):
return int(v) >= self.min
def max_compare(self, v):
return int(v) <= self.max
def choose_compare(self, v):
return v.upper() in [v.upper() for v in self.choose]
def print_choose(self):
msg = ""
if self.choose :
for v in self.choose:
msg = msg + "\t- %s\n" % v
return msg
def casted(self, v):
return eval( '%s("%s")' % (self.type, v) )
class Barista (object):
def __init__ (self):
person = Person() = self.__input("\n\nHello! I'm your personalized barista, Albert. What's your name? ")
def personalize (self, person):
expresso = Rule('Expresso', 'int', min=0, max=10)
person.caffeine = self.__input("\nOn a scale from 0-10, how many shots of Expresso do you prefer? ", rule=expresso)
sugar = Rule('Sugar', 'int', min=0, max=10)
person.sugar = self.__input("On a scale from 0-10, how many scoops of Sugar do you prefer? ", rule=sugar)
milk = Rule('Milk', 'str', choose=['Fat Free Milk', 'Whole Milk', 'Almond Milk', 'Soy Milk'])
person.milk = self.__input("From these options, which type of Milk do you prefer?\n%s" % milk.print_choose(), rule=milk)
print("\nAlright! Thanks for that info. I'll start making your coffee right away!\n")
brewer = Coffee()
print("Perfect! Your coffee is ready %s, enjoy!" % person)
def __input (self, msg, rule=None):
v = input( msg )
if rule and not rule.validate(v):
raise Exception("Uh oh. You didn't answer the question properly.")
if rule :
return rule.casted(v)
else :
return v
except Exception as e:
print( e )
self.__input(msg, rule=rule)
def __greet (self, person):
print( "Well, hello %s! Let's get started constructing your perfect cup of coffee!" % person )
if __name__ == "__main__":
albert = Barista()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment