相关代码:
# @Time: 2024/1/23 16:16
# @Author: 马龙强
# @File: 图书管理系统.py
# @software: PyCharm
class Book():
def __init__(self,name,auther,status,bookindex):
self.name = name
self.auther = auther
self.status = status
self.bookindex = bookindex
def __str__(self):
if self.status == 1:
stats = '未借出'
elif self.status == 0:
stats = '已借出'
else:
stats = '状态异常'
return '书名:《{}》,作者:{},状态:<{}>,位置:{}'.format(self.name,self.auther,self.status,self.bookindex)
class BookMange():
books = []
def start(self):
self.books.append(Book('斗罗大陆', '唐家三少', 1, 'ISO9001'))
self.books.append(Book('程序设计', '嘿嘿嘿嘿', 1, 'ISO9002'))
self.books.append(Book('感冒颗粒', '哈哈哈哈', 1, 'ISO9003'))
def Menu(self):
self.start()
while True:
print("""
图书管理系统
1.查询图书
2.增加图书
3.借阅图书
4.归还图书
5.退出系统
""")
choice = input('请选择:')
if choice == '1':
self.showallbook()
elif choice == '2':
self.addbook()
elif choice == '3':
self.borrowBook()
elif choice == '4':
self.returnbook()
elif choice == '5':
print('欢迎下次使用!!!!')
break
else:
print('请输入正确号码<1-5>')
def showallbook(self):
for book in self.books:
print(book)
def addbook(self):
name = input('图书名字:')
auther = input('作者:')
bookindex = input('位置:')
self.books.append(Book(name,auther,1,bookindex))
print('图书《{}》增加成功!'.format(name))
def checkbook(self,name):
for book in self.books:
if book.name == name:
return book
else:
return None
def borrowBook(self):
name = input('借阅图书名称:')
ret = self.checkbook(name)
if ret:
if ret.status == 0:
print(f'书籍《{name}》已借出!')
else:
ret.status = 0
print(f'书籍《{name}》借阅成功!')
else:
print(f'书籍《{name}》不存在!')
def returnbook(self):
name = input('归还图书名称:')
ret = self.checkbook(name)
if ret:
if ret.status == 0:
ret.status = 1
print(f'书籍《{name}》已归还!')
else:
print(f'书籍《{name}》未借出!')
else:
print(f'书籍《{name}》不存在!')
manager = BookMange()
manager.Menu()
一、查询图书
二、增加图书
三、借阅图书
四、归还图书
五、退出系统