-
Notifications
You must be signed in to change notification settings - Fork 0
/
mylib.py
208 lines (184 loc) · 7.29 KB
/
mylib.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
class User():
user_list = []
count = 1
def __init__(self, name, user_id) -> None:
self.__name = name
self.__user_id = user_id
def get_name(self):
return self.__name
def get_user_id(self):
return self.__user_id
@classmethod
def get_user_list(cls):
return cls.user_list
@classmethod
def register_user(cls):
print("\nEnter the information of the user being added:")
name = input("Name: ")
user_id = cls.count
cls.count += 1
user = User(name=name, user_id=user_id)
cls.user_list.append(user)
return
@classmethod
def show_users(cls):
if User.get_user_list() == []:
print("\nNo users to view")
else:
for index, user in enumerate(User.get_user_list(), start=1):
print(f"\n{index} - Name: {user.get_name()}")
print(f" ID: {user.get_user_id()}")
return
@classmethod
def remove_user(cls):
if User.get_user_list() == []:
print("\nNo users to remove")
else:
User.show_users()
r = input("\nEnter the name or ID of the user to remove: ")
flag = False
if r.isdigit():
for user in User.get_user_list():
if user.get_user_id() == int(r):
cls.user_list.remove(user)
print(f"\nUser {user.get_name()} has been removed")
flag = True
break
else:
for user in User.get_user_list():
if user.get_name().upper() == r.upper():
cls.user_list.remove(user)
print(f"\nUser {user.get_name()} has been removed")
flag = True
break
if not flag:
print("\nUser not found")
class Book():
loaned_books_list = []
returned_books_list = []
def __init__(self, name, author, year, available) -> None:
self.__name = name
self.__author = author
self.__year = year
self.available = available
def get_name(self):
return self.__name
def get_author(self):
return self.__author
def get_year(self):
return self.__year
def get_available(self):
return self.available
@classmethod
def get_loan_list(cls):
return cls.loaned_books_list
@classmethod
def get_return_list(cls):
return cls.returned_books_list
@classmethod
def add_book(cls):
print("\nEnter the information of the book being added:")
name = input("Name: ")
author = input("Author: ")
year = input("Year: ")
while not year.isdigit() or int(year) > 2024 or int(year) < 0:
print("\nInvalid option. Try again")
year = input("Year: ")
book = Book(name=name, year=int(year), author=author, available=True)
cls.returned_books_list.append(book)
return
@classmethod
def show_books(cls):
if Book.get_return_list() + Book.get_loan_list() == []:
print("\nNo books to view")
else:
for index, book in enumerate(Book.get_return_list() + Book.get_loan_list(), start=1):
availability = "Available" if book.get_available() == True else "Unavailable"
print(f"\n{index} - Name: {book.get_name()}")
print(f" Author: {book.get_author()}")
print(f" Year: {book.get_year()}")
print(f" Availability: {availability}")
return
@classmethod
def returned_books(cls):
if Book.get_return_list() == []:
print("\nNo returned books")
else:
for index, book in enumerate(Book.get_return_list(), start=1):
availability = "Available" if book.get_available() == True else "Unavailable"
print(f"\n{index} - Name: {book.get_name()}")
print(f" Author: {book.get_author()}")
print(f" Year: {book.get_year()}")
print(f" Availability: {availability}")
return
@classmethod
def loaned_books(cls):
if Book.get_loan_list() == []:
print("\nNo loaned books")
for index, book in enumerate(Book.get_loan_list(), start=1):
availability = "Available" if book.get_available() == True else "Unavailable"
print(f"\n{index} - Name: {book.get_name()}")
print(f" Author: {book.get_author()}")
print(f" Year: {book.get_year()}")
print(f" Availability: {availability}\n")
return
@classmethod
def remove_book(cls):
if Book.get_return_list() + Book.get_loan_list() == []:
print("\nNo books to remove")
else:
Book.show_books()
r = input("\nEnter the name of the book to be removed: ")
flag = False
for book in Book.get_return_list() + Book.get_loan_list():
if book.get_name().upper() == r.upper():
flag = True
if book.get_available() == False:
cls.loaned_books_list.remove(book)
else:
cls.returned_books_list.remove(book)
print(f"\nThe book {book.get_name()} has been removed")
break
if not flag:
print(f"\nBook not found")
return
@classmethod
def loan_book(cls):
if Book.get_return_list() == []:
print("\nNo available books to loan")
else:
name = input("\nEnter the name of the book to be loaned: ")
flag = False
for book in Book.get_return_list():
if book.get_name().upper() == name.upper():
flag = True
if book.get_available() == True:
book.available = False
cls.returned_books_list.remove(book)
cls.loaned_books_list.append(book)
print(f"\nThe book {book.get_name()} has been loaned")
else:
print(f"\nThe book {book.get_name()} is already marked as loaned")
if not flag:
print(f"\nBook not found")
return
@classmethod
def return_book(cls):
if Book.get_loan_list() == []:
print("\nNo available books to return")
else:
name = input("\nEnter the name of the book to be returned: ")
flag = False
for book in Book.get_loan_list():
if book.get_name().upper() == name.upper():
flag = True
if book.get_available() == False:
book.available = True
cls.loaned_books_list.remove(book)
cls.returned_books_list.append(book)
print(f"\nThe book {book.get_name()} has been returned")
else:
print(f"\nThe book {book.get_name()} is already marked as returned")
if not flag:
print(f"\nBook not found")
return