要求: 学校二手书是个大市场,钟老师决定涉足这个领域,她需要写一个图书管理系统帮 她管理书籍的借阅情况,她希望这个程序可以做到:
1. 查询书籍:可以一键查询系统里所有书籍的信息和借阅情况】
2. 添加书籍:往系统添加书籍时,需要输入书籍的基本信息(书名,作者,推荐理 由) 】
3. 借阅书籍:当书籍的状态是“未借出”的时候,书籍才可以借,借出以后的书籍状 态为“已借出” 4. 归还书籍:归还成功后书籍的状态会更改成“未借出”,下一个同学可以再借了。
1. 考虑对书籍进行分类 继承 Book 类,创建一些子类,例如:FictionBook,改造其初始化方法,增加参数 type=‘玄幻类’ 再创建一个子类的实例,利用__str__()打印实例的相关信息
2. 让用户输入作者名,就能打印出系统里该作者所有书籍的相关信息。
3. 书籍基础信息可以是 txt 文件或者 excel
源码:
import os.path
filename='book.txt'
class Book():
def __init__(self,name,othor,reason,feeling):
self.name=name
self.othor=othor
self.reason=reason
self.felling=feeling
class FictionBook(Book):
def __init__(self,name,othor,reason,feeling,type):
super().__init__(name,othor,reason,feeling)
self.type=type
def __str__(self):
return '{\'书名\':%s,\'作者\':%s,\'推荐原因\':%s,\'状态\':%s,\'类型\':%s}'%(self.name,self.othor,self.reason,self.felling,self.type)
def main():
while True:
menum()
choice=int(input('请选择'))
if choice in[0,1,2,3,4,5]:
if choice==0:
answer=input('您确定要退出系统吗?y/n')
if answer=='y' or answer=='Y':
print('谢谢您的使用!')
break
else:
continue
elif choice==1:
search()
elif choice==2:
insert()
elif choice==3:
borrow()
elif choice==4:
turn()
def menum():
print('====================学生信息管理系统========================')
print('\t\t\t\t\t1.查找书籍信息')
print('\t\t\t\t\t2.插入书籍信息')
print('\t\t\t\t\t3.借书')
print('\t\t\t\t\t4.还书')
print('\t\t\t\t\t0.退出')
print('=====================design by group one ================')
def search():
book_lst = []
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
book = rfile.readlines()
for item in book:
d=dict(eval(item))
book_lst.append(d)
if book_lst:
show_book(book_lst)
else:
print('暂未保存过数据!!!')
def show_book(lst):
if len(lst)==0:
print('没有查询到书籍信息,无数据显示!!!')
return
format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^8}'
print(format_title.format('书名', '作者', '推荐原因', '状态', '类型'))
format_data = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^8}'
for item in lst:
print(format_data.format(item.get('书名'),item.get('作者'),item.get('推荐原因'),item.get('状态'),item.get('类型')))
def insert():
book_list=[]
while True:
name = input('请输入你要添加的书名:')
if not name:
break
othor = input('请输入作者的名字:')
if not othor:
break
reason=input('请输入推荐的原因')
if not reason:
break
feeling = input('请输入书的状态')
if not feeling:
break
type=input('请输入书的类型')
if not type:
break
fictionBook=FictionBook(name,othor,reason,feeling,type)
book_list.append(fictionBook)
answer=input('是否继续添加?y/n\n')
if answer=='y':
continue
else:
break
save(book_list)
print('书籍信息录入完毕!!!')
def save(list):
try:
book_txt = open(filename, 'a', encoding='UTF-8')
except:
book_txt = open(filename, 'w', encoding='UTF-8')
for item in list:
book_txt.write(str(item) + '\n')
book_txt.close()
def borrow():
search()
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rflie:
book_old=rflie.readlines()
else :
return
book_name=input('请输入你要借的书的name:')
with open(filename,'w',encoding='utf-8') as wfile:
for item in book_old:
b=dict(eval(item))
if b['书名']==int(book_name) and b['状态']==1:
a=input('读书使人快乐,您确定要借走这本书吗?y/n\n')
if a=='y':
b['type']='-1'
wfile.write(str(b)+'\n')
print('恭喜您,借书成功!!!')
else :
wfile.write(str(b) + '\n')
answer=input('你还要借书吗?y/n')
if answer=='y':
borrow()
def turn():
search()
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rflie:
book_old=rflie.readlines()
else:
return
book_name=input('请输入您要还书的name:')
with open(filename,'w',encoding='utf-8') as wfile:
for item in book_old:
b=dict(eval(item))
if b['书名']==int(book_name) and b['状态']==-1:
a=input('读书使人快乐,您确定要归还这本书吗?y/n\n')
if a=='y':
b['type']='-1'
wfile.write(str(b)+'\n')
print('恭喜您,还书成功!!!')
break
else :
wfile.write(str(b) + '\n')
if __name__ == '__main__':
main()
思路:
一:Book类和FictiBook类
Book类当中有四个属性,分别是书名,作者,推荐的原因和状态
FictionBook继承Book类,新增type属性,重写了str()方法。
二:定义了整体框架的几个函数
menum()实现了菜单的打印。
main()调用了其余函数,由用户进行功能的选择。
三:解决问题的几个函数
insert()获取键盘输入然后实例化书籍对象。调用save()函数
save(lst)接收到一个装着书籍对象的列表。把其写进book.txt文件当中
search()把book.txt文件中的数据写进一个列表当中调用show_book()函数
show_book(lst) 把接收到的列表的数据打印到控制台
borrow()函数完成借书的功能
turn()完成还书的功能
四:实现
五.问题有点多
在输入不是整形的数据时,会报错。换句话说,只能输入整数型数据。。。。