注:创建一个图书管理系统,实现可以注册,登录,查找,搜索,退出。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
from turtle import update
import books as books
from openpyxl import *
wb = load_workbook("../file/library.xlsx")
userSheet = wb['user']
booksSheet = wb['books']
# 用户管理
# books = []
def querryAll():
books = []
for i in range(2, booksSheet.max_row + 1):
# print((i))
book = [booksSheet[i][0].value, booksSheet[i][1].value, booksSheet[i][2].value, booksSheet[i][3].value,
booksSheet[i][4].value]
books.append(book)
return books
def querryById(id):
books = []
for i in range(2, booksSheet.max_row + 1):
if str(booksSheet[i][0].value) == id:
book = [booksSheet[i][0].value, booksSheet[i][1].value, booksSheet[i][2].value, booksSheet[i][3].value,
booksSheet[i][4].value]
books.append(book)
return books
def showBooks(books):
for i in books:
print("编号:%s,书名:《%s》,作者:%s,出版社:%s,价格:%s" % (i[0], i[1], i[2], i[3], i[4]))
def querry():
print("1.查询所有图书")
print("2.按编号查询图书")
check = input("请选择你要使用的功能")
if check == "1":
books = querryAll()
showBooks(books)
elif check == "2":
id = input("请输入你想查找的图书编号:")
books = querryById(id)
showBooks(books)
def findMaxId():
max = booksSheet[2][0].value
for i in range(2, booksSheet.max_row + 1):
if booksSheet[i][0].value > max:
max = booksSheet[i][0].value
return max
def add():
bookName = input("请输入你要添加的书名:")
bookAutor = input("请输入你要添加的作者:")
press = input("请输入你要添加的出版社:")
price = input("请输入你要添加的价格")
print("你所添加的图书信息为:书名:《%s》,作者:%s,出版社:%s,价格:%s" % (bookName, bookAutor, press, price))
check = input("确认添加请按”1“")
if check == "1":
findMaxId()
maxId = findMaxId()
book = [maxId + 1, bookName, bookAutor, press, price]
booksSheet.append(book)
wb.save(filename="../file/library.xlsx")
print("添加成功")
def update():
id = input("请输入你要修改的编号")
books = querryById(id)
if len(books) > 0:
showBooks(books)
check = input("确认修改请按1")
bookName = input("请输入你要修改的书名:")
bookAutor = input("请输入你要修改的作者:")
press = input("请输入你要修改的出版社:")
price = input("请输入你要修改的价格")
# print("你所修改的图书信息为:书名:《%s》,作者:%s,出版社:%s,价格:%s" % (bookName, bookAutor, press, price))
for i in range(2, booksSheet.max_row+1):
if str(booksSheet[i][0].value) == id:
booksSheet[i][1].value = bookName
booksSheet[i][2].value = bookAutor
booksSheet[i][3].value = press
booksSheet[i][4].value = price
print("修改成功!")
wb.save(filename="../file/library.xlsx")
else:
print("没有找到你想修改的图书")
def delete():
id = input("请输入你要删除的编号")
books = querryById(id)
if len(books) > 0:
showBooks(books)
check = input("确认删除请按1")
for i in range(2, booksSheet.max_row+1):
if str(booksSheet[i][0].value) == id:
booksSheet.delete_rows(i)
wb.save(filename="../file/library.xlsx")
print("删除成功!")
else:
print("没有找到你想删除的图书")
def menu():
while True:
print("1.查询")
print("2.增加")
print("3.修改")
print("4.删除")
print("5.退出")
check = input("请选择你想使用的功能")
if check == "1":
querry()
elif check == "2":
add()
elif check == "3":
update()
elif check == "4":
delete()
elif check == "5":
wb.save(filename="../file/library.xlsx")
wb.close()
print("欢迎下次使用")
sys.exit()
def regist():
while True:
userName = input("请输入用户名:")
password = input("请输入密码:")
realPasswoed = input("请确认密码:")
if password == realPasswoed:
user = [userName, password]
userSheet.append(user)
wb.save(filename="../file/library.xlsx")
print("注册成功")
break
else:
print("两次输入的密码不一致,请重新输入")
def login():
userName = input("请输入用户名:")
password = input("请输入密码:")
flag = False
for i in range(2, userSheet.max_row + 1):
if str(userSheet[i][0].value) == userName and str(userSheet[i][1].value) == password:
flag = True
if flag:
print("登录成功")
menu()
else:
print("登录失败")
def main():
while True:
print("==========欢迎使用图书管理系统==========")
print("1.登录")
print("2.注册")
check = input("请选择你想使用的功能")
if check == "1":
login()
elif check == "2":
regist()
# 登录
main()
# 注册
# querry()
# add()
# querry()
# showBooks()
# findMaxId()