0% found this document useful (0 votes)
21 views3 pages

Lib

Uploaded by

pramod kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

Lib

Uploaded by

pramod kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

"""

Creating a library management system in Python involves


managing books, library members, checkouts, and more.
Here's a simplified example of how to create a basic library management system
using Python:
"""

#Step 1: Import Required Libraries

import csv
import os
import datetime

#Step 2: Define Classes

"""
Create classes for books, members, and transactions (checkouts):
"""

class Book:
def __init__(self, book_id, title, author, copies):
self.book_id = book_id
self.title = title
self.author = author
self.copies = copies

class Member:
def __init__(self, member_id, name, contact):
self.member_id = member_id
self.name = name
self.contact = contact

class Transaction:
def __init__(self, member_id, book_id, checkout_date, due_date):
self.member_id = member_id
self.book_id = book_id
self.checkout_date = checkout_date
self.due_date = due_date

#Step 3: Create Functions

"""
Create functions for actions like
adding books, members, and transactions, checking out and returning books, and
displaying reports:
"""

def add_book(book_id, title, author, copies):


book = Book(book_id, title, author, copies)
books.append(book)

def add_member(member_id, name, contact):


member = Member(member_id, name, contact)
members.append(member)

def checkout_book(member_id, book_id):


if is_available(book_id):
today = datetime.date.today()
due_date = today + datetime.timedelta(days=14)
transaction = Transaction(member_id, book_id, today, due_date)
transactions.append(transaction)
decrement_copies(book_id)
return True
else:
return False

def return_book(book_id):
for transaction in transactions:
if transaction.book_id == book_id:
transactions.remove(transaction)
increment_copies(book_id)
return True
return False

def is_available(book_id):
for book in books:
if book.book_id == book_id and book.copies > 0:
return True
return False

def increment_copies(book_id):
for book in books:
if book.book_id == book_id:
book.copies += 1

def decrement_copies(book_id):
for book in books:
if book.book_id == book_id:
book.copies -= 1

def generate_report():
for transaction in transactions:
member = next(member for member in members if member.member_id ==
transaction.member_id)
book = next(book for book in books if book.book_id == transaction.book_id)
print(f"Member: {member.name}, Book: {book.title}, Due Date:
{transaction.due_date}")

# Initialize empty lists to store books, members, and transactions


books = []
members = []
transactions = []

#Step 4: Implement a User Interface

"""
You can implement a command-line interface (CLI) or
a graphical user interface (GUI) to interact with your library management system.
Here's a simple CLI for adding books, members, checking out and returning books,
and generating reports:
"""

while True:
print("Library Management System Menu:")
print("1. Add Book")
print("2. Add Member")
print("3. Checkout Book")
print("4. Return Book")
print("5. Generate Report")
print("6. Exit")

choice = input("Enter your choice: ")

if choice == '1':
book_id = input("Enter Book ID: ")
title = input("Enter Book Title: ")
author = input("Enter Author: ")
copies = int(input("Enter Number of Copies: "))
add_book(book_id, title, author, copies)
elif choice == '2':
member_id = input("Enter Member ID: ")
name = input("Enter Member Name: ")
contact = input("Enter Contact Information: ")
add_member(member_id, name, contact)
elif choice == '3':
member_id = input("Enter Member ID: ")
book_id = input("Enter Book ID: ")
if checkout_book(member_id, book_id):
print("Book successfully checked out.")
else:
print("Book not available or member does not exist.")
elif choice == '4':
book_id = input("Enter Book ID: ")
if return_book(book_id):
print("Book successfully returned.")
else:
print("Book not checked out or does not exist.")
elif choice == '5':
generate_report()
elif choice == '6':
break
else:
print("Invalid choice. Please try again.")

"""
This example provides a basic library management system
with data storage, functions to manage books, members, and transactions,
and a simple CLI for user interaction.
You can expand and customize it further to meet your specific requirements.
"""

You might also like