class Book(object):
def __init__(self, name, author, status, position):
self.name = name
self.author = author
self.status = status
self.position = position
def __str__(self):
return '书名:%s\t作者:%s\t状态:%s\t位置:%s' % (self.name, self.author, self.status, self.position)
class BookManager(object):
def __init__(self):
self.books = []
def start(self, book1, book2, book3):
self.books.append(book1)
self.books.append(book2)
self.books.append(book3)
def query(self):
for N in self.books:
print(N)
def add(self, book):
self.books.append(book)
print('\n图书增加成功!\n')
def borrow(self, bookname):
for N in self.books:
if N.name == bookname:
if N.status == '未借出':
N.status = '借出'
print('\n该图书的位置为%s!' % N.position)
print('图书%s借出成功!\n' % bookname)
break
else:
print('\n图书%s已经借出!\n' % bookname)
break
else:
print('\n抱歉,本图书馆没有图书%s!\n' % bookname)
def giveback(self, bookname):
for N in self.books:
if N.name == bookname:
if N.status == '借出':
N.status = '未借出'
print('\n请将该图书放于位置%s!' % N.position)
print('图书%s归还成功!\n' % bookname)
break
else:
print('\n图书%s尚未借出!请确认!\n' % bookname)
break
else:
print('\n此图书并非本馆图书!请确认!\n')
book1 = Book('Lang C', '潭浩强', '未借出', 'ISO9001')
book2 = Book('python', 'guido', '未借出', 'NFS9001')
book3 = Book('java', 'westos', '未借出', 'PKI7844')
def surface():
print("图书管理系统".center(30, '*') + '\n' +
'1.查询图书'.center(30, ' ') + '\n' +
'2.增加图书'.center(30, ' ') + '\n' +
'3.借阅图书'.center(30, ' ') + '\n' +
'4.归还图书'.center(30, ' ') + '\n' +
'5.退出系统'.center(30, ' ') + '\n' +
''.center(34, '*'))
Choice = ''
Library = BookManager()
Library.start(book1, book2, book3)
while True:
surface()
Choice = input('请输入您的操作:')
print()
if Choice == '1':
print("图书管理系统".center(30, '*'))
Library.query()
print("".center(34, '*')+'\n')
elif Choice == '2':
print("输入添加的图书信息".center(28, '*'))
add_bookname = input('添加图书的名字:')
add_bookauthor = input('添加图书的作者:')
add_bookstatus = '未借出'
add_bookposition = input('添加图书放置的位置:')
add_book = Book(add_bookname, add_bookauthor, add_bookstatus, add_bookposition)
print("".center(34, '*'))
Library.add(add_book)
elif Choice == '3':
print("图书管理系统".center(30, '*'))
bookname = input('请输入借阅的图书名:')
Library.borrow(bookname)
print("".center(34, '*'))
elif Choice == '4':
print("图书管理系统".center(30, '*'))
bookname = input('请输入归还的图书名:')
Library.giveback(bookname)
print("".center(34, '*'))
elif Choice == '5':
exit()
else:
print("您输入的选项有误!请重选!\n")
Python实现简单的图书管理系统
最新推荐文章于 2024-06-23 10:30:00 发布