-
Notifications
You must be signed in to change notification settings - Fork 0
/
Technical_test.py
193 lines (137 loc) · 4.45 KB
/
Technical_test.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'''
I apologize for code with less comments because I had limited time to put all the functionalities.
But I have tried to keep the code as readable as possible by following naming convention to depict the usage.
'''
i = 0 # for UID creation
ownerdict = {}
petdict = {}
def get_id():
global i
i += 1
return i
class General:
def __init__(self, name, birthday):
self.id = get_id()
self.name = name
self.birthday = birthday
def UpdateName(self, name):
self.name = name
def UpdateBDay(self, bday):
self.birthday = bday
class Owner(General):
def __init__(self, first, last, birthday):
global ownerdict
General.__init__(self, first, birthday)
self.lastname = last
self.doglist = []
self.catlist = []
ownerdict[self.id] = self
def __str__(self):
return self.name + " " + self.lastname
def ReadOwner(self):
owner = ownerdict[self.id]
print(owner, owner.birthday, owner.doglist, owner.catlist)
def DeleteOwner(self):
global ownerdict, petdict
owner = ownerdict[self.id]
del ownerdict[self.id]
for id in owner.doglist+owner.catlist:
del petdict[id]
print("Deleted", owner)
def AddPet(self, pet_type, petid):
'''
Add new pet in petlist of pwner
:param pet_type: type of pet, Dog or Cat
:param petid: Unique ID of pet
:return: None
'''
if pet_type == 'Dog':
self.doglist.append(petid)
else:
self.catlist.append(petid)
def AllDogs(self):
return [ petdict[petid] for petid in self.doglist]
def AllCats(self):
return [petdict[petid] for petid in self.catlist]
def AllPetsCount(self):
return len(self.AllCats()+ self.AllDogs())
def UpdateLastName(self, lname):
'''
Update last name of Pwner
:param lname: New last name
:return: None
'''
self.lastname = lname
def RemovePet(self,pet_type, petid):
'''
Remove a pet from petlist
:param pet_type: Dog or Cat
:param petid: ID of pet
:return:
'''
if pet_type == 'Dog':
self.doglist.remove(petid)
else:
self.catlist.remove(petid)
class Pet(General):
def __init__(self, pet_type, first, birthday, ownerid):
if pet_type not in ['Dog', 'Cat']:
print("Invalid pet, only Dog or Cat is allowed")
else:
global petdict
self.pet_type = pet_type
General.__init__(self,first, birthday)
self.ownerid = ownerid
owner = ownerdict[ownerid]
owner.AddPet(pet_type, self.id)
petdict[self.id] = self
def UpdateOwner(self,new_ownerid):
'''
Change the owner of pet
:param new_ownerid: ID of new owner
:return: None
'''
owner = ownerdict[new_ownerid]
owner.AddPet(self.pet_type, self.id)
old_owner = ownerdict[self.ownerid]
old_owner.RemovePet(self.pet_type, self.id)
self.ownerid = new_ownerid
def GetOwner(self):
return self.ownerid
def __str__(self):
return self.name
def ReadPet(self):
'''
Print details of self
:return: None
'''
pet = petdict[self.id]
print(pet, pet.pet_type, pet.birthday)
def DeletePet(self):
'''
Delete self from petdict and owner's petlist
:return: None
'''
global petdict
pet = petdict[self.id]
del petdict[self.id]
owner = ownerdict[pet.ownerid]
if pet.pet_type == 'Dog': owner.doglist.remove(pet.id)
else: owner.catlist.remove(pet.id)
print("Deleted", pet)
def ListAllOwners():
'''
Generate list of all Owners with their details
'''
print("List of all owners")
for key in ownerdict:
if ownerdict[key].ReadOwner():
print(ownerdict[key].ReadOwner())
Owner("A", "Ben", "30-11-2017")
Owner("B", "Cathy", "30-1-2007")
Owner("D", "Tom", "10-11-2001")
Pet("Dog","gen","21-11-2002",3)
Pet("Dog","genna","21-11-2002",3)
Pet("Dog","hen","12-11-2002",2)
Pet("Cat","heena","12-1-2012",2)
ListAllOwners()