Repository navigation
Expand file tree
/
Copy pathCoffeeMaker.py
More file actions
50 lines (38 loc) · 1.41 KB
/
Copy pathCoffeeMaker.py
File metadata and controls
50 lines (38 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Design and Implement to Extend a Coffee object with dynamic features (e.g., milk,
sugar, whipped cream) using Decorators. """
class coffee:
def cost(self):
return 5.0
def description(self):
return "plain coffee"
class coffeedecorator(coffee):
def __init__(self,coffee):
self.coffee = coffee
def cost(self):
return self.coffee.cost()
def description(self):
return self.coffee.description()
class milk(coffeedecorator):
def cost(self):
return self.coffee.cost()+1.5
def description(self):
return self.coffee.description()+",Milk"
class sugar(coffeedecorator):
def cost(self):
return self.coffee.cost()+2
def description(self):
return self.coffee.description()+",Sugar"
class whipped_cream(coffeedecorator):
def cost(self):
return self.coffee.cost()+5
def description(self):
return self.coffee.description()+",Whipped cream"
if __name__ =="__main__":
my_coffee=coffee()
print(f"Description:{my_coffee.description()},Cost:{my_coffee.cost()}")
my_coffee=milk(my_coffee)
print(f"Description:{my_coffee.description()},Cost:{my_coffee.cost()}")
my_coffee=sugar(my_coffee)
print(f"Description:{my_coffee.description()},Cost:{my_coffee.cost()}")
my_coffee=whipped_cream(my_coffee)
print(f"Description:{my_coffee.description()},Cost:{my_coffee.cost()}")