import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import random
# Initialize data for books and members
books_data = {
"Book_ID": ["B101", "B102", "B103", "B104", "B105"],
"Title": ["Python Basics", "Data Science", "Machine Learning", "AI for Beginners", "Web Development"],
"Author": ["Author A", "Author B", "Author C", "Author D", "Author E"],
"Price": [500, 700, 650, 800, 550],
"Quantity": [10, 5, 8, 6, 12],
"Read_Count": [20, 45, 30, 25, 50] # Random data for most-read books
members_data = {
"Member_ID": ["M001", "M002", "M003", "M004", "M005"],
"Name": ["John", "Alice", "Bob", "Eve", "Charlie"],
"Phone": ["1234567890", "9876543210", "5678901234", "6789012345", "7890123456"],
"Email": ["john@mail.com", "alice@mail.com", "bob@mail.com", "eve@mail.com", "charlie@mail.com"],
"Books_Issued": [2, 3, 1, 0, 4] # Random data for books issued
# Create DataFrames
books_df = pd.DataFrame(books_data)
members_df = pd.DataFrame(members_data)
cart = [] # Initialize cart
# Graph Functions
def plot_bar_chart():
"""Plot a bar chart showing the number of books available."""
plt.figure(figsize=(10, 6))
plt.bar(books_df["Title"], books_df["Quantity"], color='skyblue')
plt.title("Number of Books Available", fontsize=16)
plt.xlabel("Book Titles", fontsize=12)
plt.ylabel("Quantity", fontsize=12)
plt.xticks(rotation=45, ha='right')
plt.show()
def plot_line_chart():
"""Plot a line chart for most-read books."""
plt.figure(figsize=(10, 6))
plt.plot(books_df["Title"], books_df["Read_Count"], marker='o', color='green', linestyle='--')
plt.title("Most Read Books", fontsize=16)
plt.xlabel("Book Titles", fontsize=12)
plt.ylabel("Read Count", fontsize=12)
plt.grid()
plt.xticks(rotation=45, ha='right')
plt.show()
def plot_pie_chart():
"""Plot a pie chart showing the percentage of books issued by members."""
plt.figure(figsize=(8, 8))
issued_books = members_df["Books_Issued"]
labels = members_df["Name"]
plt.pie(issued_books, labels=labels, autopct='%1.1f%%', startangle=140, colors=plt.cm.Paired.colors)
plt.title("Books Issued by Members", fontsize=16)
plt.axis('equal')
plt.show()
# Cart Functions
def add_to_cart():
"""Add books to the cart."""
global cart, books_df
book_id = input("Enter the Book ID to add to the cart: ")
if book_id in books_df["Book_ID"].values:
book_index = books_df[books_df["Book_ID"] == book_id].index[0]
if books_df.at[book_index, "Quantity"] > 0:
cart.append(book_id)
print(f"Book '{book_id}' added to the cart.")
else:
print("Sorry, this book is out of stock!")
else:
print("Book ID not found!")
def view_cart():
"""View items in the cart."""
if not cart:
print("Your cart is empty!")
else:
print("Books in your cart:")
for book_id in cart:
book_details = books_df[books_df["Book_ID"] == book_id]
print(book_details.to_string(index=False))
def checkout_cart():
"""Checkout books in the cart."""
global books_df, cart
if not cart:
print("Your cart is empty!")
return
for book_id in cart:
book_index = books_df[books_df["Book_ID"] == book_id].index[0]
books_df.at[book_index, "Quantity"] -= 1
print(f"Book '{book_id}' has been checked out!")
cart = [] # Empty the cart
print("Thank you for checking out. Enjoy your books!")
# Export to CSV
def export_to_csv():
"""Export library and member data to CSV files."""
books_df.to_csv("books_data.csv", index=False)
members_df.to_csv("members_data.csv", index=False)
print("Data exported to 'books_data.csv' and 'members_data.csv'.")
# Main Menu Functions
def add_book():
"""Add a new book to the library."""
global books_df
book_id = input("Enter Book ID: ")
title = input("Enter Book Title: ")
author = input("Enter Author Name: ")
price = float(input("Enter Book Price: "))
quantity = int(input("Enter Quantity: "))
read_count = random.randint(0, 50) # Generate random read count
books_df = pd.concat([
books_df,
pd.DataFrame([[book_id, title, author, price, quantity, read_count]], columns=books_df.columns)
], ignore_index=True)
print("Book added successfully!")
def view_books():
"""View all books."""
if books_df.empty:
print("No books available!")
else:
print("\nBooks Available in the Library:")
print(books_df)
def main_menu():
"""Display the main menu."""
while True:
print("\nLibrary Management System")
print("1. Add Book")
print("2. View Books")
print("3. Add to Cart")
print("4. View Cart")
print("5. Checkout Cart")
print("6. Plot Bar Chart (Books Available)")
print("7. Plot Line Chart (Most Read Books)")
print("8. Plot Pie Chart (Books Issued)")
print("9. Export to CSV")
print("10. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
add_book()
elif choice == 2:
view_books()
elif choice == 3:
add_to_cart()
elif choice == 4:
view_cart()
elif choice == 5:
checkout_cart()
elif choice == 6:
plot_bar_chart()
elif choice == 7:
plot_line_chart()
elif choice == 8:
plot_pie_chart()
elif choice == 9:
export_to_csv()
elif choice == 10:
print("Exiting the system. Goodbye!")
break
else:
print("Invalid choice! Please try again.")
# Start the program
if __name__ == "__main__":
main_menu()