-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.py
73 lines (58 loc) · 2.01 KB
/
library.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 Book:
def __init__(self, title, author):
self.title = title
self.author = author
self.is_checked_out = False
def check_out(self):
self.is_checked_out = True
print(f"{self.title} by {self.author} has been checked out.")
def check_in(self):
self.is_checked_out = False
print(f"{self.title} by {self.author} has been checked in.")
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
print(f"{book.title} by {book.author} has been added to the library.")
def check_out_book(self, title):
for book in self.books:
if book.title == title and not book.is_checked_out:
book.check_out()
return
print(f"Sorry, {title} is not available.")
def check_in_book(self, title):
for book in self.books:
if book.title == title and book.is_checked_out:
book.check_in()
return
print(f"Sorry, {title} is not in the library.")
class Client:
def __init__(self, name):
self.name = name
self.checked_out_books = []
def check_out_book(self, library, title):
for book in library.books:
if book.title == title and not book.is_checked_out:
book.check_out()
self.checked_out_books.append(book)
return
print(f"Sorry, {title} is not available.")
def check_in_book(self, library, title):
for book in self.checked_out_books:
if book.title == title:
book.check_in()
self.checked_out_books.remove(book)
return
print(f"Sorry, {title} is not checked out.")
library = Library()
book1 = Book("To Kill a Mockingbird", "Harper Lee")
library.add_book(book1)
book2 = Book("Pride and Prejudice", "Jane Austen")
library.add_book(book2)
client1 = Client("John Doe")
client1.check_out_book(library, "To Kill a Mockingbird")
client1.check_out_book(library, "Pride and Prejudice")
client2 = Client("Jane Doe")
client2.check_out_book(library, "To Kill a Mockingbird")
client1.check_in_book(library, "To Kill a Mockingbird")