def returnBook(): name = input("Enter name of borrower: ") borrowBookFile = "Borrow-" + name + ".txt" try: with open(borrowBookFile, "r") as f: lines = f.readlines() lines = [borrowBookFile.strip("$") for borrowBookFile in lines] with open(borrowBookFile, "r") as f: data = f.read() print("\t\tCurrent Book Holdings\t\t\t") print(data) except: print("The borrower " + name + " does not exist in System Currently") returnBook() returnBookFile = "Return-" + name + ".txt" with open(returnBookFile, "w+") as f: f.write(" Library Management System \n") f.write(" Returned By: " + name + "\n") f.write(" Date: " + dt.getDate() + " Time:" + dt.getTime() + "\n\n") f.write("S.N.\t\tBookname\t\n") for i in range(3): if ListSplit.bookname[i] in data: with open(returnBookFile, "a") as f: f.write(str(i + 1) + "\t\t" + ListSplit.bookname[i] + "\t\n") #ListSplit.quantity[i]=int(ListSplit.quantity[i])+1 bookReturn = input(" Enter book Name you want to Return : ") if bookReturn in data: with open("Stock.txt", "w+") as f: for i in range(3): if (bookReturn == ListSplit.bookname[i]): ListSplit.quantity[i] = int(ListSplit.quantity[i]) + 1 f.write(ListSplit.bookname[i] + "," + ListSplit.authorname[i] + "," + str(ListSplit.quantity[i]) + "\n") with open(borrowBookFile, "r") as f: lines = f.readlines() with open(borrowBookFile, "w+") as f: for line in lines: if bookReturn in line.strip("\n"): pass else: f.write(line) with open(returnBookFile, "r") as f: lines = f.readlines() with open(returnBookFile, "w+") as f: for line in lines: if bookReturn in line.strip("\n"): pass else: f.write(line) else: print("Given Book " + bookReturn + " is not available in the list")
def returnBook(): name = input("Enter name of borrower: ") a = "Borrow-" + name + ".txt" try: with open(a, "r") as f: lines = f.readlines() lines = [a.strip("$") for a in lines] with open(a, "r") as f: data = f.read() print(data) except: print("The borrower name is incorrect") returnBook() b = "Return-" + name + ".txt" with open(b, "w+") as f: f.write(" Library Management System \n") f.write(" Returned By: " + name + "\n") f.write(" Date: " + dt.getDate() + " Time:" + dt.getTime() + "\n\n") f.write("S.N.\t\tBookname\t\tCost\n") total = 0.0 for i in range(3): if ListSplit.book[i] in data: with open(b, "a") as f: f.write( str(i + 1) + "\t\t" + ListSplit.book[i] + "\t\t$" + ListSplit.cost[i] + "\n") ListSplit.q[i] = int(ListSplit.q[i]) + 1 total += float(ListSplit.cost[i]) print("\t\t\t\t\t\t\t" + "$" + str(total)) print("Is the book return date expired?") print("Press Y for Yes and N for No") stat = input() if (stat.upper() == "Y"): print("By how many days was the book returned late?") day = int(input()) fine = 2 * day with open(b, "a") as f: f.write("\t\t\t\t\tFine: $" + str(fine) + "\n") total = total + fine print("\nFinal Total: " + "$" + str(total)) with open(b, "a") as f: f.write("\t\t\t\t\tTotal: $" + str(total)) with open("Stock.txt", "w+") as f: for i in range(3): f.write(ListSplit.book[i] + "," + ListSplit.author[i] + "," + str(ListSplit.q[i]) + "," + "$" + ListSplit.cost[i] + "\n")
def borrowBook(): success = False while (True): firstName = input("Enter the first name of the borrower: ") if firstName.isalpha(): break print("please input alphabet from A-Z") while (True): lastName = input("Enter the last name of the borrower: ") if lastName.isalpha(): break print("please input alphabet from A-Z") t = "Borrow-" + firstName + ".txt" with open(t, "w+") as f: f.write(" Library Management System \n") f.write(" Borrowed By: " + firstName + " " + lastName + "\n") f.write(" Date: " + dt.getDate() + " Time:" + dt.getTime() + "\n\n") f.write("S.N. \t\t Bookname \t Authorname \n") while success == False: print("Please select a option below:") for i in range(len(ListSplit.bookname)): print("Enter", i, "to borrow book", ListSplit.bookname[i]) try: a = int(input()) try: if (int(ListSplit.quantity[a]) > 0): print("Book is available") with open(t, "a") as f: f.write("1. \t\t" + ListSplit.bookname[a] + "\t\t " + ListSplit.authorname[a] + "\n") ListSplit.quantity[a] = int(ListSplit.quantity[a]) - 1 with open("Stock.txt", "w+") as f: for i in range(3): f.write(ListSplit.bookname[i] + "," + ListSplit.authorname[i] + "," + str(ListSplit.quantity[i]) + "," + "$" + ListSplit.cost[i] + "\n") #multiple book borrowing code loop = True count = 1 while loop == True: choice = str( input( "Do you want to borrow more books? However you cannot borrow same book twice. Press y for yes and n for no." )) if (choice.upper() == "Y"): count = count + 1 print("Please select an option below:") for i in range(len(ListSplit.bookname)): print("Enter", i, "to borrow book", ListSplit.bookname[i]) a = int(input()) if (int(ListSplit.quantity[a]) > 0): print("Book is available") with open(t, "a") as f: f.write( str(count) + ". \t\t" + ListSplit.bookname[a] + "\t\t " + ListSplit.authorname[a] + "\n") ListSplit.quantity[a] = int( ListSplit.quantity[a]) - 1 with open("Stock.txt", "w+") as f: for i in range(3): f.write(ListSplit.bookname[i] + "," + ListSplit.authorname[i] + "," + str(ListSplit.quantity[i]) + "," + "$" + ListSplit.cost[i] + "\n") success = False else: loop = False break elif (choice.upper() == "N"): print("Thank you for borrowing books from us. ") print("") loop = False success = True else: print("Please choose as instructed") else: print("Book is not available") borrowBook() success = False except IndexError: print("") print("Please choose book acording to their number.") except ValueError: print("") print("Please choose as suggested.")