Project File LMS 2nd Part
Project File LMS 2nd Part
import sqlite3
from datetime import datetime, timedelta
def initialize_library_database():
# Connect to SQLite database
conn = sqlite3.connect('library.db')
cursor = conn.cursor()
conn.commit()
conn.close()
cursor.execute(query, params)
books = cursor.fetchall()
conn.close()
print("\nBooks Available:")
print("ID | Title | Author | Available Copies")
print("-" * 40)
for book in books:
print(f"{book[0]} | {book[1]} | {book[2]} |
{book[3]}")
conn.close()
def return_book(issue_id):
conn = sqlite3.connect('library.db')
cursor = conn.cursor()
if issue:
book_id, due_date = issue
return_date = datetime.now()
cursor.execute('UPDATE issued_books SET
return_date = ? WHERE id = ?',
(return_date.strftime('%Y-%m-%d'), issue_id))
cursor.execute('UPDATE books SET available =
available + 1 WHERE id = ?', (book_id,))
due_date_obj = datetime.strptime(due_date,
'%Y-%m-%d')
if return_date > due_date_obj:
late_days = (return_date -
due_date_obj).days
print(f"Book returned late by
{late_days} days. Late fee: ${late_days *
1.0:.2f}.")
else:
print("Book returned successfully.")
conn.commit()
else:
print("Invalid issue ID or book already
returned.")
conn.close()
def display_issued_books():
conn = sqlite3.connect('library.db')
cursor = conn.cursor()
cursor.execute('SELECT issued_books.id,
books.title, issued_books.issued_to,
issued_books.issue_date, issued_books.due_date,
issued_books.return_date '
'FROM issued_books INNER JOIN books ON
issued_books.book_id = books.id')
issued_books = cursor.fetchall()
conn.close()
print("\nIssued Books:")
print("ID | Title | Issued To | Issue Date | Due
Date | Return Date")
print("-" * 70)
for record in issued_books:
print(f"{record[0]} | {record[1]} |
{record[2]} | {record[3]} | {record[4]} | {record[5]
if record[5] else 'Not Returned'}")
# Initialize database
initialize_library_database()
predefined_inputs = iter([
"1", "The Great Gatsby", "F. Scott
Fitzgerald", "5",
"2", "The Great Gatsby", "",
"3", "1", "John Doe",
"5",
"4", "1",
"6"
])
while True:
print("\nLibrary Management System")
for choice in menu_choices:
print(choice)
try:
choice = next(predefined_inputs) #
Simulate input
print(f"Choice: {choice}")
except StopIteration:
print("No more inputs. Exiting...")
break
if choice == '1':
title = next(predefined_inputs)
author = next(predefined_inputs)
copies = int(next(predefined_inputs))
add_book(title, author, copies)
elif choice == '2':
title = next(predefined_inputs)
author = next(predefined_inputs)
search_books(title, author)
elif choice == '3':
book_id = int(next(predefined_inputs))
issued_to = next(predefined_inputs)
issue_book(book_id, issued_to)
elif choice == '4':
issue_id = int(next(predefined_inputs))
return_book(issue_id)
elif choice == '5':
display_issued_books()
elif choice == '6':
print("Exiting...")
break
else:
print("Invalid choice. Please try
again.")
if __name__ == "__main__":
main_menu()
Output:
Books Available:
ID | Title | Author | Available Copies
----------------------------------------
1 | The Great Gatsby | F. Scott Fitzgerald | 5
Library Management System
1. Add Book
2. Search Books
3. Issue Book
4. Return Book
5. Display Issued Books
6. Exit
Choice: 3
Book issued successfully. Due date: 2025-01-13.
Issued Books:
ID | Title | Issued To | Issue Date | Due Date | Return Date
----------------------------------------------------------------------
1 | The Great Gatsby | John Doe | 2024-12-30 | 2025-01-13 | Not Returned