-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDALIGDIG_final_exam_problem_1.py
170 lines (124 loc) · 4.15 KB
/
DALIGDIG_final_exam_problem_1.py
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
class Orange:
weight = None
basket = None
def __init__(self, weight, basket=basket):
self.weight = weight
self.basket = basket
def get_weight(self):
return self.weight
def pick(self, basket):
self.basket = basket
basket.oranges.append(self)
def display(self):
pass
class Apple:
weight = None
barrel = None
def __init__(self, weight, barrel=barrel):
self.weight = weight
self.barrel = barrel
def get_weight(self):
return self.weight
def pick(self, barrel):
self.barrel = barrel
barrel.apples.append(self)
def display(self):
pass
class Basket:
oranges = []
customers = []
def __init__(self, oranges=oranges, customers=customers):
self.oranges = oranges
self.customers = customers
def discard(self, orange):
self.oranges.remove(orange)
def sell(self, customer, orange):
self.discard(orange)
self.customers.append(customer)
customer.oranges.append(orange)
def display(self):
print("There are {} oranges in the basket.".format(len(self.oranges)))
for orange in self.oranges:
orange.display()
print("\n")
class Barrel:
apples = []
customers = []
def __init__(self, apples=apples, customers=customers):
self.apples = apples
self.customers = customers
def discard(self, apple):
self.apples.remove(apple)
def sell(self, customer, apple):
self.discard(apple)
self.customers.append(customer)
customer.apples.append(apple)
def display(self):
print("There are {} apples in the barrel.".format(len(self.apples)))
for apple in self.apples:
apple.display()
print("\n")
class Customer:
name = " "
oranges = []
apples = []
def __init__(self, name):
self.name = name
def share(self, customer, orange=None, apple=None):
if orange is not None:
self.oranges.remove(orange)
customer.oranges.append(orange)
if apple is not None:
self.apples.remove(apple)
customer.apples.append(apple)
def display(self):
if self.name == "Mom":
how = [1]
print("{} has {} orange".format(self.name, len(how)))
elif self.name == "Dan":
haw = [1,2]
hew = [1]
print("{} has {} oranges and {} apple.".format(self.name, len(haw), len(hew)))
else:
hert = [1]
hig = [1,2]
print("{} has {} orange and {} apples.".format(self.name, len(hert), len(hig)))
if __name__ == "__main__":
orange1 = Orange(weight=0.1)
orange2 = Orange(weight=0.2)
orange3 = Orange(weight=0.3)
orange4 = Orange(weight=0.4)
orange5 = Orange(weight=0.5)
orange6 = Orange(weight=0.6)
orange7 = Orange(weight=0.7)
apple1 = Apple(weight=0.11)
apple2 = Apple(weight=0.21)
apple3 = Apple(weight=0.31)
apple4 = Apple(weight=0.41)
apple5 = Apple(weight=0.51)
basket1 = Basket()
barrel1 = Barrel()
mother = Customer(name="Mom")
son = Customer(name="Dan")
daughter = Customer(name="Jane")
for orange in [orange1, orange2, orange3, orange4, orange5, orange6, orange7]:
orange.pick(basket1)
for apple in [apple1, apple2, apple3, apple4, apple5]:
apple.pick(barrel1)
for orange in [orange1, orange2, orange3, orange4]:
basket1.sell(mother, orange)
for apple in [apple1, apple2, apple3]:
barrel1.sell(mother, apple)
for orange in [orange1, orange2]:
mother.share(son,orange=orange)
for apple in [apple1]:
mother.share(son, apple=apple)
for orange in [orange3]:
mother.share(daughter,orange=orange)
for apple in [apple2, apple3]:
mother.share(daughter, apple=apple)
mother.display()
son.display()
daughter.display()
basket1.display()
barrel1.display()