Created
May 12, 2023 08:32
-
-
Save seunoyeniyi/0d2df544c8799a0a524b1c4d7ab4ac8e to your computer and use it in GitHub Desktop.
Library Management with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Things Fall Apart,Chinua Achebe,209,Novel | |
Half of a Yellow Sun,Chimamanda Ngozi Adichie,433,Novel | |
Americanah,Chimamanda Ngozi Adichie,587,Novel | |
Purple Hibiscus,Chimamanda Ngozi Adichie,307,Novel | |
The Secret Lives of Baba Segi's Wives,Lola Shoneyin,245,Novel |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#OYENIYI SEUN TAIWO | |
#CSC/2021/235 | |
class Book: | |
def __init__(self, title, author, num_pages, book_type): | |
self.title = title | |
self.author = author | |
self.num_pages = num_pages | |
self.book_type = book_type | |
def print_info(self): | |
print("Title:", self.title) | |
print("Author:", self.author) | |
print("Number of Pages:", self.num_pages) | |
print("Type of Book:", self.book_type) | |
print() | |
class Library: | |
def __init__(self): | |
self.books = [] | |
def add_book(self, book): | |
self.books.append(book) | |
def print_books(self): | |
for book in self.books: | |
book.print_info() | |
def borrow_book(self, title): | |
for book in self.books: | |
if book.title.lower() == title.lower(): | |
self.books.remove(book) | |
return book | |
return None | |
# Main program | |
library = Library() | |
# Add books from the file books.txt | |
with open("books.txt", "r") as file: | |
for line in file: | |
title, author, num_pages, book_type = line.strip().split(",") | |
book = Book(title, author, int(num_pages), book_type) | |
library.add_book(book) | |
# Print all books in the library | |
print("________________ Books in the Library: ________________") | |
library.print_books() | |
print("________________________________________________________________") | |
# Borrow a book | |
while True: | |
title = input("Enter the title of the book you want to borrow: ") | |
book = library.borrow_book(title) | |
if book is not None: | |
print("________________ You borrowed the following book: ________________") | |
book.print_info() | |
break | |
else: | |
print("Book not found in the library. Please enter a valid book title.") | |
# Print remaining books in the library | |
print("________________ Books in the Library after borrowing: ________________") | |
library.print_books() | |
print("__________________ Library closed __________________") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment