-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrarymanagementsystem.py
More file actions
95 lines (73 loc) · 3.15 KB
/
Copy pathlibrarymanagementsystem.py
File metadata and controls
95 lines (73 loc) · 3.15 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
#Objective: Build a simple Library Management System using OOP concepts like classes, objects, inheritance, and encapsulation.
class Book:
def __init__(self, title, author, isbn, available):
self.title = title
self.author = author
self.isbn = isbn
self.available = available
def __str__(self):
return f"Title: {self.title}, Author: {self.author}, ISBN: {self.isbn}, Available: {self.available}" #returns a string with book info
class Member:
def __init__(self, name, member_id, books_borrowed):
self.name = name
self.member_id = member_id
self.books_borrowed = [] #list of books borrowed
def __str__(self):
return f"Name: {self.name}, Member ID: {self.member_id}, Books Borrowed: {len(self.books_borrowed)}"
def borrow_book(self, book): #Marks the book as borrowed
self.books_borrowed.append(book)
book.available = False
print(f"{self.name} has borrowed {book.title}")
print(f"{book.title} is now marked as borrowed")
def return_book(self, book): #Marks the book as available
self.books_borrowed.remove(book)
book.available = True
print(f"{self.name} has returned {book.title}")
print(f"{book.title} is now marked as available")
class Library:
def __init__(self, books, members):
self.books = books
self.members = members
def add_book(self, book): #add a book to the library
self.books.append(book)
print(f"Added {book.title} to the library")
def register_member(self, member): #register a member to the library
self.members.append(member)
print(f"Registered {member.name} to the library")
def find_book_by_title(self, title): #find a book by title
for book in self.books:
if book.title == title:
return book
return None
def display_available_books(self): #Prints all books that are available.
for book in self.books:
if book.available:
print(book.__str__())
def display_all_members(self): #Prints all members in the library
for member in self.members:
print(member.__str__())
#add books
book1 = Book("The Great Gatsby", "F. Scott Fitzgerald", "9780743273565", True)
book2 = Book("To Kill a Mockingbird", "Harper Lee", "9780446310789", True)
book3 = Book("Pride and Prejudice", "Jane Austen", "9780141439518", True)
book4 = Book("1984", "George Orwell", "9780451524935", True)
#add members
member1 = Member("John Doe", "1234567890", [])
member2 = Member("Jane Smith", "0987654321", [])
member3 = Member("Jim Beam", "1357911131", [])
#one member borrow a book, then return it.
member1.borrow_book(book1)
member1.return_book(book1)
#create library
library = Library([book1, book2, book3], [member1, member2])
#add books to the library
library.add_book(book4)
#register a member to the library
library.register_member(member3)
#find a book by title
found_book = library.find_book_by_title("The Great Gatsby")
print(f"Found book: {found_book}")
#find all available books
library.display_available_books()
#find all members
library.display_all_members()