-
Notifications
You must be signed in to change notification settings - Fork 5
/
library_models.py
73 lines (59 loc) · 1.88 KB
/
library_models.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
class Resource:
def __init__(self, resource_type):
self.resource_type = resource_type
self.current_borrower = None
def lend(self, patron):
self.current_borrower = patron
def return_resource(self):
self.current_borrower = None
def get_status(self):
if self.current_borrower:
return "on loan"
else:
return "available"
class Book(Resource):
def __init__(self, title="", author=""):
self.title = title
self.author = author
super().__init__(resource_type="book")
def get_info(self):
return f"{self.title}, by {self.author}"
class Magazine(Resource):
def __init__(self, title="", issue=""):
self.title = title
self.issue = issue
super().__init__(resource_type="magazine")
def get_info(self):
return f"{self.title}, issue {self.issue}"
class Patron:
def __init__(self, name):
self.name = name
class Library:
def __init__(self, name="", resources=None):
self.name = name
if resources is None:
self.resources = []
else:
self.resources = resources
def get_books(self):
books = [
r for r in self.resources
if r.resource_type == "book"
]
return books
def show_books(self):
print(f"All books in {self.name}:")
for book in self.get_books():
info = book.get_info()
print(f"- {info} ({book.get_status()})")
def get_magazines(self):
magazines = [
r for r in self.resources
if r.resource_type == "magazine"
]
return magazines
def show_magazines(self):
print(f"All magazines in {self.name}:")
for magazine in self.get_magazines():
info = magazine.get_info()
print(f"- {info} ({magazine.get_status()})")