-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary using class.py
More file actions
82 lines (77 loc) · 2.69 KB
/
Copy pathlibrary using class.py
File metadata and controls
82 lines (77 loc) · 2.69 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
class Book:
def __init__(self,title,author):
self.title=title
self.author=author
self.is_borrowed=False
def show_info(self):
status="Borrowed" if self.is_borrowed else "Available"
print(f"Title: {self.title}, Author: {self.author}, Status: {status}")
class Library:
def __init__(self,name):
self.name=name
self.books=[]
def add_book(self,title,author):
book=Book(title,author)
self.books.append(book)
print(f"Added: {title} by {author}")
def show_all_books(self):
print(f"\n---{self.name} Catalog---")
print("No books available.")
return
for book in self.books:
book.show_info()
def find_book(self,title):
for book in self.books:
if book.title.lower()==title.lower():
return book
return None
def borrow_book(self,title):
book=self.find_book(title)
if not book:
print(f"Book '{title}' not found in the library.")
elif book.is_borrowed:
print(f"Book '{title}' is already borrowed.")
else:
book.is_borrowed=True
print(f"You have borrowed '{title}'.")
def return_book(self,title):
book=self.find_book(title)
if not book:
print(f"Book '{title}' not found in the library.")
elif not book.is_borrowed:
print(f"Book '{title}' was not borrowed.")
else:
book.is_borrowed=False
print(f"You have returned '{title}'.")
#MAIN PROGRAM
def main():
my_library=Library("AK Library")
while True:
print("\n---AK Library Menu---")
print("1. Show all Books")
print("2. Add a new Book")
print("3. Borrow a Book")
print("4. Return a Book")
print("5. Exit")
choice=input("Enter your choice (1-5): ")
if choice=="1":
my_library.show_all_books()
elif choice=="2":
title=input("Enter book title: ")
author=input("Enter book author: ")
my_library.add_book(title,author)
elif choice=="3":
my_library.show_all_books()
elif choice=="3":
title=input("Enter book title to borrow: ")
my_library.borrow_book(title)
elif choice=="4":
title=input("Enter book title to return: ")
my_library.return_book(title)
elif choice=="5":
print("Exiting the program.")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()