程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python implements a simple library management system

編輯:Python
user = [['aaa', '123456'], ['bbb', '123456789'],[' librarian ','55555'] ]# Account and password
i = 0# Global variables
f = 0
class Book(object): # Define a Book class
def __init__(self, name, author, status, bookindex):# Title , author , Have you lent it ,id
self.name = name
self.author = author
self.status = status
self.bookindex = bookindex
def __str__(self):# Judge whether the book is lent
if self.status == 1:
stats = ' Not lent '
elif self.status == 0:
stats = ' Lent out '
else:
stats = ' Abnormal state '
return ' Title : 《%s》 author : %s state : <%s> Location : %s' \
% (self.name, self.author, stats, self.bookindex)
class BookManage(object):# Library system
books = []# Define a list of books
def start(self):
# Add books
self.books.append(Book(' Devour the stars ', ' tomato ', 1, 'ISO9001'))
self.books.append(Book(' Seal the throne ', ' Three little Tang family ', 1, 'NFS8102'))
self.books.append(Book(' The other side of deep space ', ' Chendong ', 1, 'PKA7844'))
# 0: Lend 1: There is
def Menu(self):# system menu
self.start()
while True:
print("""
The book management system
1. Search for books
2. Borrow books
3. Return books
4. Exit the system
""")
choice = input(' Please select :')
if choice == '1':
self.showAllBook() # Call the function that displays all books
elif choice == '2':
self.borrowBook() # Call the function of borrowing books
elif choice == '3':
self.returnBook() # Call the return function
elif choice == '4':
print(' Welcome to use next time ...')
exit()
else:
print(' Please enter the correct choice ')
continue
def showAllBook(self):# Functions that display all books
for book in self.books:
print(book)
def borrowBook(self):# Borrowing function
name = input(' Name of borrowed books : ')
ret = self.checkBook(name)
print(ret)
# Judge whether the book exists , If there is , Judge whether the book has been lent , If not lent , Borrow and change its status to 0
if ret != None:
if ret.status == 0:
print(' Books 《%s》 It has been lent out ' % name)
else:
ret.status = 0
print(' Books 《%s》 Borrowing succeeded ' % name)
else:
print(' Books 《%s》 non-existent ' % name)
def returnBook(self):# Function of returning books
name = input(' Return book name :')
ret = self.checkBook(name)
if ret != None:
if ret.status == 0:
ret.status = 1
print(' Books 《%s》 Successful return ' % name)
print(ret)
else:
print(' Books 《%s》 Not lent ' % name)
else:
print(' Books 《%s》 non-existent ' % name)
class BookManage2(BookManage):# Librarian's library management system , Inherit
books = []
def Menu(self):# The start menu
self.start()
while True:
print("""
The book management system
1. Search for books
2. Add books
3. Borrow books
4. Return books
5. Exit the system
""")
choice = input(' Please select :')
if choice == '1':
self.showAllBook() # Call the function that displays all books
elif choice == '2':
self.addBook() # Call the function to add books
elif choice == '3':
self.borrowBook() # Call the function of borrowing books
elif choice == '4':
self.returnBook() # Call the return function
elif choice == '5':
print(' Welcome to use next time ...')
exit()
else:
print(' Please enter the correct choice ')
continue
def addBook(self):# Add books
name = input(' The name of the book :')
self.books.append(Book(name, input(' author :'), 1, input(' Storage location :')))
print(' The book 《%s》 Increase success ' % name)
# The following is the main function
print('-'*10, ' Welcome to the library management system ', '-'*10)
print(' Please select the operation you want to perform below ')
while i < 4:
xuan = input(' Please select :1\n Input selection :')
if xuan == "1":
l = 0
while True:
cc = input(' Please select the type of operation you want :\n Log in to an ordinary account :1\t Log in to the administrator account :2\t Go back to the previous level :3\n:')
if cc == "1":
fu = input(' Please enter your account number :')
ru = input(' Please enter your password :')
if [fu, ru] in user:
print(' welcome {} Enter the library management system '.format(fu))
manager = BookManage() # Class instantiation
manager.Menu()
else:
print(' Wrong account password , Please re-enter your account password ')
elif cc == "2":
jia = input(' Please enter the administrator's password :')
if [' librarian ', jia] in user:
print(' Welcome librarians to the library management system ')
print(10*'*', " Hello, administrator , Choose what you want to do ", "*"*10)
manager = BookManage2() # Class instantiation
manager.Menu()
else:
print(' Administrator password error , Please re-enter the administrator password ')
elif cc == "3":
break
else:
print(" Input error, please input ")
else:
print(" Input error, please input again ")


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved