-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinFactory.py
More file actions
186 lines (154 loc) · 5.94 KB
/
Copy pathCoinFactory.py
File metadata and controls
186 lines (154 loc) · 5.94 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# These are templates for manufacturing British coins.
# Running the program will present a list of all coins and their properties.
# There are some default properties that all coins will inherit, and then
# there are unique properties for each child class of coins.
import random # Needed for the ability to Flip and land on Heads or Tails randomly!
class Coin(object):
def __init__(self, rare = False, clean = True, heads = True, **kwargs): # **kwargs packs down all the Coin parent data into a dictionary called kwargs, making Coin able to accept keyword args from child classes.
for key, value in kwargs.items(): # loops through all items in the child class dictionary and appends the keyword args to all other items in the parent class. Should generally work for any dictionary in parent/child class relationships.
setattr(self, key, value)
self.is_rare = rare
self.is_clean = clean
self.heads = heads
if self.is_rare:
self.value = self.original_value * 1.25
else:
self.value = self.original_value
if self.is_clean:
self.colour = self.clean_colour
else:
self.colour = self.rusty_colour
def rust(self):
self.colour = self.rusty_colour
def clean(self):
self.colour = self.clean_colour
def __del__(self):
print("Coin spent!")
def flip(self):
heads_options = [True, False]
choice = random.choice(heads_options)
self.heads = choice
def __str__(self): # cleans up output of final, complete string. Defines what comes out when you print that object.
if self.original_value >= 1.00:
return "£{} coin".format(int(self.original_value))
else:
return "{}p coin".format(int(self.original_value * 100))
class One_Pence(Coin): # class child_of(parent)
def __init__(self):
data = { # this is a dictionary with unique data pairs for this child class
"original_value": 0.01,
"clean_colour": "bronze",
"rusty_colour": "brownish",
"num_edges": 1,
"diameter": 20.3,
"thickness": 1.52,
"mass": 3.56
}
super(One_Pence, self).__init__(**data) # unpacks the data from the dictionary, making the data keyword args.
class Two_Pence(Coin):
def __init__(self):
data = {
"original_value": 0.02,
"clean_colour": "bronze",
"rusty_colour": "brownish",
"num_edges": 1,
"diameter": 25.9,
"thickness": 1.85,
"mass": 7.12
}
super().__init__(**data)
class Five_Pence(Coin):
def __init__(self):
data = {
"original_value": 0.05,
"clean_colour": "silver",
"rusty_colour": None, # Polymorfism: overriding standard behaviour from parent class, because silver coins don't rust!
"num_edges": 1,
"diameter": 18.0,
"thickness": 1.77,
"mass": 3.25
}
super().__init__(**data)
def rust(self):
self.colour = self.clean_colour # Polymorfism in practice, multiple definitions of functions. Overrides parent function.
def clean(self):
self.colour = self.clean_colour
class Ten_Pence(Coin):
def __init__(self):
data = {
"original_value": 0.10,
"clean_colour": "silver",
"rusty_colour": None,
"num_edges": 1,
"diameter": 24.5,
"thickness": 1.85,
"mass": 6.50
}
super().__init__(**data)
def rust(self):
self.colour = self.clean_colour
def clean(self):
self.colour = self.clean_colour
class Twenty_Pence(Coin):
def __init__(self):
data = {
"original_value": 0.20,
"clean_colour": "silver",
"rusty_colour": None,
"num_edges": 7,
"diameter": 21.4,
"thickness": 1.7,
"mass": 5.00
}
super().__init__(**data)
def rust(self):
self.colour = self.clean_colour
def clean(self):
self.colour = self.clean_colour
class Fifty_Pence(Coin):
def __init__(self):
data = {
"original_value": 0.50,
"clean_colour": "silver",
"rusty_colour": None,
"num_edges": 7,
"diameter": 27.3,
"thickness": 1.70,
"mass": 8.00
}
super().__init__(**data)
def rust(self):
self.colour = self.clean_colour
def clean(self):
self.colour = self.clean_colour
class Pound(Coin):
def __init__(self):
data = {
"original_value": 1.00,
"clean_colour": "gold",
"rusty_colour": "greenish",
"num_edges": 1,
"diameter": 22.5,
"thickness": 3.15,
"mass": 9.5
}
super().__init__(**data)
class Two_Pound(Coin):
def __init__(self):
data = {
"original_value": 2.00,
"clean_colour": "gold",
"rusty_colour": "greenish",
"num_edges": 1,
"diameter": 28.4,
"thickness": 2.50,
"mass": 12.00
}
super().__init__(**data)
# A list of our coin types (all coin child classes):
Coins = [One_Pence(), Two_Pence(), Five_Pence(), Ten_Pence(), Twenty_Pence(), Fifty_Pence(), Pound(), Two_Pound()]
for Coin in Coins:
arguments = [Coin, Coin.colour, Coin.value, Coin.diameter, Coin.thickness, Coin.num_edges, Coin.mass]
# Unpack above arguments to this string:
string = "{}, colour:{}, value{}, diameter(mm):{}, thickness(mm):{}, number of edges:{}, mass(g):{}".format(*arguments)
print(string)