-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.py
More file actions
98 lines (66 loc) · 2.03 KB
/
Copy pathlibrary.py
File metadata and controls
98 lines (66 loc) · 2.03 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
from abc import ABC
import pickle, re
class InvalidISBNException(Exception):
pass
class BorrowLimitExceededException(Exception):
pass
class BookAlreadyExistException(Exception):
pass
class Book:
def __init__(self, title, author, ISBN):
self.ISBN = ISBN
self.title = title
self.author = author
self.is_borrowed = False
def __str__(self):
return f"Book(title={self.title}, author={self.author}, isbn={self.ISBN})"
class LibraryUser(ABC):
def __init__(self, name, email, telephone, borrow_limit):
self.name = name
self.email = email
self.telephone = telephone
self.borrow_limit = borrow_limit
self.borrowed_books = []
class Student(LibraryUser):
def __init__(self, name, email, telephone):
super().__init__(name, email, telephone, borrow_limit=3)
class Staff(LibraryUser):
def __init__(self, name, email, telephone):
super().__init__(name, email, telephone, borrow_limit=5)
class Library:
def __init__(self):
self.books = []
self.users = []
def addBook(self, newBook):
try:
for book in self.books:
if book.ISBN == newBook.ISBN:
raise BookAlreadyExistException(
f"Book with ISBN {newBook.ISBN} already exists in the library.")
self.books.append(newBook)
except BookAlreadyExistException as e:
print(e)
else:
print(f"Book '{newBook.title}' added successfully!")
def addUser(self, user):
self.users.append(user)
print(f"User '{user.name}' added successfully!")
def borrowBook(self, user, book):
if len(user.borrowed_books) < user.borrow_limit and not book.is_borrowed:
book.is_borrowed = True
user.borrowed_books.append(book)
print(f"Book '{book.title}' borrowed successfully!")
elif len(user.borrowed_books) == user.borrow_limit:
raise BorrowLimitExceededException(f"{user.name} has exceeded their borrowing limit.")
else:
print("Book already borrowed")
def returnBook(self, user, book):
user.borrowed_books.remove(book)
book.is_borrowed = False
print(f"Book '{book.title}' returned successfully!")
def searchForBook(self, querytype, query):
if len(self.books) == 0:
print("No items found.")
return
found_any = False
print(f"File '{filename}' not found.")