-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibary mangement.py
155 lines (124 loc) · 4.54 KB
/
libary mangement.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
import sys
MAX_BOOKS = 100
class Book:
def __init__(self):
self.bookId = 0
self.title = ""
self.author = ""
self.isAvailable = True
library = [Book() for _ in range(MAX_BOOKS)]
bookCount = 0
def addBook():
global bookCount
if bookCount == MAX_BOOKS:
print("Library is full. Cannot add more books.")
return
library[bookCount].title = input("Enter book title: ")
library[bookCount].author = input("Enter author name: ")
library[bookCount].bookId = bookCount + 1
library[bookCount].isAvailable = True
print(f"Book added successfully. Book ID: {library[bookCount].bookId}")
bookCount += 1
def displayBooks():
if library[0].bookId == 0:
print("Library is empty. No books to display.")
return
print("Book ID\tTitle\tAuthor\tStatus")
for book in library:
if book.bookId != 0:
status = "Available" if book.isAvailable else "Checked Out"
print(f"{book.bookId}\t{book.title}\t{book.author}\t{status}")
def searchBook():
searchTitle = input("Enter the title of the book to search: ")
found = False
for book in library:
if book.bookId != 0 and book.title == searchTitle:
found = True
print("Book is found!")
status = "Available" if book.isAvailable else "Checked Out"
print(f"Book ID: {book.bookId}\nTitle: {book.title}\nAuthor: {book.author}\nStatus: {status}")
break
if not found:
print("Book not found in the library.")
def borrowBook():
bookId = int(input("Enter the Book ID to borrow: "))
if bookId < 1 or bookId > MAX_BOOKS or library[bookId - 1].bookId == 0:
print("Invalid Book ID. Please enter a valid Book ID.")
return
if library[bookId - 1].isAvailable:
print("Book successfully borrowed.")
library[bookId - 1].isAvailable = False
else:
print("Sorry, the book is already checked out.")
def returnBook():
bookId = int(input("Enter the Book ID to return: "))
if bookId < 1 or bookId > MAX_BOOKS or library[bookId - 1].bookId == 0:
print("Invalid Book ID. Please enter a valid Book ID.")
return
if not library[bookId - 1].isAvailable:
print("Book successfully returned.")
library[bookId - 1].isAvailable = True
else:
print("This book is already in the library.")
def deleteBook():
bookId = int(input("Enter the Book ID to delete: "))
if bookId < 1 or bookId > MAX_BOOKS or library[bookId - 1].bookId == 0:
print("Invalid Book ID. Please enter a valid Book ID.")
return
# Shift the remaining books to fill the gap
for i in range(bookId - 1, MAX_BOOKS - 1):
library[i] = library[i + 1]
print("Book successfully deleted.")
def displayAvailableBooks():
if library[0].bookId == 0:
print("Library is empty. No books to display.")
return
print("Available Books:")
print("Book ID\tTitle\tAuthor")
for book in library:
if book.bookId != 0 and book.isAvailable:
print(f"{book.bookId}\t{book.title}\t{book.author}")
def updateBook():
bookId = int(input("Enter the Book ID to update: "))
if bookId < 1 or bookId > MAX_BOOKS or library[bookId - 1].bookId == 0:
print("Invalid Book ID. Please enter a valid Book ID.")
return
library[bookId - 1].title = input("Enter updated book title: ")
library[bookId - 1].author = input("Enter updated author name: ")
print("Book details updated successfully.")
def main():
while True:
print("\nLibrary Management System")
print("1. Add Book")
print("2. Display Books")
print("3. Search Book")
print("4. Borrow Book")
print("5. Return Book")
print("6. Delete Book")
print("7. Display Available Books")
print("8. Update Book")
print("0. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
addBook()
elif choice == 2:
displayBooks()
elif choice == 3:
searchBook()
elif choice == 4:
borrowBook()
elif choice == 5:
returnBook()
elif choice == 6:
deleteBook()
elif choice == 7:
displayAvailableBooks()
elif choice == 8:
updateBook()
elif choice == 0:
print("\nExiting the program. Goodbye!")
sys.exit(0)
else:
print("\nInvalid choice. Please enter a valid option.")
if __name__ == "__main__":
main()