python-图书管理系统(精简版)

'''图书管理系统'''
class books(object):
    def __init__(self):
        self.books = []

    def menu(self):
        print("1.添加图书信息")
        print("2.显示图书信息")
        print("3.修改图书信息")
        print("4.删除图书信息")
        print("5.查找图书信息")
        print("0.退出程序")

    def add_book_info(self):
        bookname = input("请输入图书名:")
        bookprice = input("请输入图书价格:")
        bookauthor = input("请输入图书作者:")
        book = {"bookname":bookname, "bookprice":bookprice, "bookauthor":bookauthor}
        self.books.append(book)
        self.save_book_info()
        self.show_book_info()

    def show_book_info(self):
        self.read_book_info()
        for book in self.books:
            print("图书名:" + book["bookname"], "图书价格:" + book["bookprice"], "图书作者" + book["bookauthor"])

    def mod_book_info(self):
        getname = input("请输入你要修改的图书名:")
        for book in self.books:
            if getname == book["bookname"]:
                getinfo = input("请输入你要修改的标题:")
                if getinfo == "图书名":
                    newbookname = input("请输入新的图书名:")
                    book["bookname"] = newbookname
                elif getinfo == "图书价格":
                    newbookprice = input("请输入新的图书价格:")
                    book["bookprice"] = newbookprice
                elif getinfo == "图书作者":
                    newbookauthor = input("请输入新的图书作者:")
                    book["bookauthor"] = newbookauthor
                else:
                    print("请输入你想要修改的标题!")
        self.save_book_info()

    def del_book_info(self):
        getname = input("请输入你要删除的图书名:")
        flag = 0
        for book in self.books:
            if getname == book["bookname"]:
                self.books.remove(book)
            flag = 1
        if flag == 0:
            print("没有删除成功!")
        self.save_book_info()

    def search_book_info(self):
        getname = input("请输入你要查找的图书名:")
        flag = 0
        for book in self.books:
            if getname == book["bookname"]:
                print("图书名:" + book["bookname"], "图书价格:" + book["bookprice"], "图书作者" + book["bookauthor"])
            flag = 1
        if flag == 0:
            print("没有删除成功!")
        self.save_book_info()

    def save_book_info(self):
        f = open("book.txt", "w")
        for book in self.books:
            nbookname = book["bookname"]
            nbookprice = book["bookprice"]
            nbookauthor = book["bookauthor"]
            f.write(nbookname + "\t" + nbookprice + "\t" + nbookauthor + "\n")
        f.close()


    def read_book_info(self):
        self.books.clear()
        f = open("book.txt", "r")
        for line in f.readlines():
            line = line.strip()
            if len(line) == 0:
                continue
            data = line.split("\t")
            readfile = {"bookname":data[0], "bookprice":data[1], "bookauthor":data[2]}
            self.books.append(readfile)
        f.close()

    def operation(self):
        self.menu()
        self.read_book_info()
        while True:
            print("\t\t1.增加|2.显示|3.修改|4.删除|5.查找|0.退出\t\t")
            getnum = input("请输入你要进行的选项:")
            if getnum == "1":
                self.add_book_info()
            elif getnum == "2":
                self.show_book_info()
            elif getnum == "3":
                self.mod_book_info()
            elif getnum == "4":
                self.del_book_info()
            elif getnum == "5":
                self.search_book_info()
            elif getnum == "0":
                print("程序运行结束!")
                break
            else:
                print("输入操作有误,请重新输入操作选项!")
                continue

if __name__ == '__main__':
    books().operation()
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值