Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
107 lines (93 loc) · 3.35 KB
/
Copy pathmain.py
File metadata and controls
107 lines (93 loc) · 3.35 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
96
97
98
99
100
101
102
103
104
105
106
107
import pymysql as mc
connection = mc.connect(host="localhost",user="root",password="1234",database="CSProject")
cursor = connection.cursor()
def view_books():
cursor.execute("select * from Books")
rows = cursor.fetchall()
if rows:
print("\n===== |BOOKS TABLE| =====\n")
for row in rows:
print(row)
else:
print("No books found...")
def view_loans():
cursor.execute("select * from BookLoan")
rows = cursor.fetchall()
if rows:
print("\n===== |LOANS TABLE| =====\n")
for row in rows:
print(row)
else:
print("No loans found...")
def add_loan():
loan_id = input("Enter Loan ID: ")
book_id = input("Enter Book ID: ")
member_id = input("Enter Member ID: ")
loan_date = input("Enter Loan Date (YYYY-MM-DD): ")
return_date = input("Enter Due Date (YYYY-MM-DD): ")
cursor.execute("select TotalCopies from Books where BookID = %s", (book_id,))
copies = cursor.fetchone()
if not copies:
print("Book ID not found.")
return
count = copies[0]
if count <= 0:
print("Book unavailable...")
return
cursor.execute("insert into BookLoan values(%s, %s, %s, %s, %s)", (loan_id, book_id, member_id, loan_date, return_date))
cursor.execute("update Books set TotalCopies = TotalCopies - 1 where BookID = %s", (book_id,))
connection.commit()
print("Loan added successfully...")
def return_loan():
loan_id = input("Enter Loan ID: ")
cursor.execute("select LoanID from BookLoan where LoanID = %s", (loan_id,))
loan = cursor.fetchone()
if not loan:
print("Loan ID is invalid..")
return
cursor.execute("select BookID from BookLoan where LoanID = %s", (loan_id,))
book = cursor.fetchone()
if book:
cursor.execute("update Books set TotalCopies = TotalCopies + 1 where BookID = %s", (book[0],))
cursor.execute("delete from BookLoan where LoanID = %s", [loan_id])
connection.commit()
print("Thank you for returning the book..")
print("Your loan has been repayed...")
def new_book():
book_id = input("Enter the BookID: ")
book_name = input("Enter the Book's Name: ")
author = input("Enter the Author's Name: ")
count = int(input("Enter the number of copies: "))
cursor.execute("insert into Books values(%s, %s, %s, %s)", [book_id, book_name, author, count])
connection.commit()
print("Book successfully added in stock...")
while True:
print("\n====== |LIBRARY MENU| ======\n")
print("1. View Books Table")
print("2. View Loans Table")
print("3. Add a Loan")
print("4. Return a Loan")
print("5. Add a New Book")
print("6. Exit\n")
choice = int(input("Enter your choice: "))
if choice == 1:
view_books()
elif choice == 2:
view_loans()
elif choice == 3:
add_loan()
elif choice == 4:
return_loan()
elif choice == 5:
new_book()
elif choice == 6:
print("Successfully Exitted...")
break
else:
print("Please Try Again..")
task = input("\nDo you wish to continue(y/n): ")
if task.lower() == 'n':
print("Successfully Exitted...")
break
cursor.close()
connection.close()