1
1
# Vending machine – Take 2: Using OOP
2
2
3
- This tutorial revists a previous example and uses object-oriented programming
3
+ This tutorial revisits a previous example and uses object-oriented programming
4
4
paradigms to represent the concepts related to a vending machine. We will see
5
5
how classes and objects will help us better represent the "real world" and help
6
6
us track state:
@@ -11,21 +11,18 @@ Let's begin by designing our vending machine by modelling different objects.
11
11
First, we have:
12
12
- Coins
13
13
- Products
14
- - DollarAmount
14
+ - Vending Machine
15
15
16
16
We can consider these as abstract classes / concepts. In practise, we have
17
17
specific coins like quarters, loonies & toonies. For products, we have chips,
18
18
candy and drinks – these could be broken down further into specific products
19
- like "355ml Coca-Cola can". These are considered concrete classes which all share
20
- common properties to the abstracts coins and products.
21
-
22
- Money in this case is a subclass of the python Decimal class and is used to
23
- represent a dollar amount. It is already provided as part of the boilerplate.
19
+ like "355ml Coca-Cola can". These are considered concrete classes which all
20
+ share common properties to the abstracts coins and products.
24
21
25
22
We must also model our vending machine and define actions on it:
26
23
- ` + insert_coin(coin: Coin) `
27
24
- ` + buy_product(product: str) -> Product `
28
- - ` + get_balance() -> Money `
25
+ - ` + get_balance() -> int `
29
26
- ` + get_change() -> List[Coin] `
30
27
31
28
@@ -35,38 +32,42 @@ We must also model our vending machine and define actions on it:
35
32
36
33
| ** Coin** |
37
34
| - |
38
- | ` value: DollarAmount ` |
35
+ | ` value: int ` |
39
36
| ` label: str ` |
40
37
| ` str() -> str ` |
41
38
42
39
43
- | ** FiveCent(Coin)** | |
40
+ The following classes inherit from the parent ` Coin ` class. Inheritance is
41
+ achieved by providing parentheses around the class definition and providing the
42
+ parent class(es) as arguments.
43
+
44
+ | ** Nickel(Coin)** | |
44
45
| - | - |
45
- | value | ` DollarAmount('0.05') ` |
46
+ | value | ` 5 ` |
46
47
| ` str() ` | ` '5¢' ` |
47
48
48
49
49
- | ** TenCent (Coin)** | |
50
+ | ** Dime (Coin)** | |
50
51
| - | - |
51
- | value | ` DollarAmount('0.10') ` |
52
+ | value | ` 10 ` |
52
53
| ` str() ` | ` '10¢' ` |
53
54
54
55
55
56
| ** Quarter(Coin)** | |
56
57
| - | - |
57
- | value | ` DollarAmount('0.25') ` |
58
+ | value | ` 25 ` |
58
59
| ` str() ` | ` '25¢' ` |
59
60
60
61
61
62
| ** Loonie(Coin)** | |
62
63
| - | - |
63
- | value | ` DollarAmount('1.00') ` |
64
+ | value | ` 100 ` |
64
65
| ` str() ` | ` '$1' ` |
65
66
66
67
67
68
| ** Toonie(Coin)** | |
68
69
| - | - |
69
- | value | ` DollarAmount('2.00') ` |
70
+ | value | ` 200 ` |
70
71
| ` str() ` | ` '$2' ` |
71
72
72
73
@@ -75,34 +76,41 @@ We must also model our vending machine and define actions on it:
75
76
| ** Product** |
76
77
| --- |
77
78
| ` name: str ` |
78
- | ` price: DollarAmount ` |
79
+ | ` price: int ` |
80
+ | ` str() -> str ` |
79
81
80
82
81
83
| ** Chips(Product)** | |
82
84
| - | - |
83
85
| ` name ` | ` 'Chips' ` |
84
- | ` price ` | ` DollarAmount('2.25') ` |
86
+ | ` price ` | ` 225 ` |
87
+ | ` str() -> str ` | ` Chips: $2.25 ` |
85
88
86
89
87
90
| ** Drink(Product)** | |
88
91
| - | - |
89
92
| ` name ` | ` 'Drink' ` |
90
- | ` price ` | ` DollarAmount('2.75') ` |
93
+ | ` price ` | ` 275 ` |
94
+ | ` str() -> str ` | ` Drink: $2.75 ` |
91
95
92
96
93
97
| ** Candy(Product)** | |
94
98
| - | - |
95
99
| ` name ` | ` 'Candy' ` |
100
+ | ` price ` | ` 315 ` |
101
+ | ` str() ` | ` Candy: $3.15 ` |
96
102
97
103
98
104
99
105
#### Vending Machine
100
- | ** VendingMachine** |
101
- | - |
102
- | - ` + insert_coin(coin: Coin) ` |
103
- | - ` + buy_product(product: str) -> Product ` |
104
- | - ` + get_balance() -> Money ` |
105
- | - ` + get_change() -> List[Coin] ` |
106
+ | ** VendingMachine** |
107
+ | -
108
+ | ` coins: List[Coin] ` |
109
+ | ` purchases: List[Product] ` |
110
+ | ` insert_coin(coin: Coin) ` |
111
+ | ` buy_product(product: str) -> Product ` |
112
+ | ` get_balance() -> int ` |
113
+ | ` get_change() -> List[Coin] ` |
106
114
107
115
108
116
@@ -125,7 +133,7 @@ We must also model our vending machine and define actions on it:
125
133
the purchase should be added to a list of purchases on the object.
126
134
127
135
128
- ### .get_balance() -> Money
136
+ ### .get_balance() -> int
129
137
130
138
- The get_balance function should return the sum of inserted coins minus the
131
139
sum of the price of purchased products.
0 commit comments