护栏板销售网站怎么做/全网营销的公司
夜光序言:
一步一微笑,一步一伤心,一步一劫难,尽管记忆再悲伤,我却笑着,不愿遗忘。
正文:
6.6 实践项目 教材记录管理
6.6.1 项目目标
这个项目是通过面向对象的方法设计教材类 Book,包含一个教材 ISBN、名称(Title)、 作者(Author)、出版社(Publisher),然后设计教材记录管理类 BookList 来管理一组教材记录。
程序运行后显示">"的提示符号,在">"后面可以输入 show、insert、update、delete 等命 令实现记录的显示、插入、修改、删除等功能,执行一个命令后继续显示">"提示符号,如 果输入 exit 就退出系统,输入的命令不正确时会提示正确的输入命令,操作过程类似学生记录管理项目。
在程序启动时会读取 books.txt 的教程记录,在程序结束时会把记录存储在 books.txt 文 件中。
6.6.2 项目实践
class Book:def __init__(self, ISBN, Title, Author, Publisher):self.ISBN = ISBNself.Title = Titleself.Author = Authorself.Publisher = Publisherdef show(self):print("%-16s %-16s %-16s %-16s" % (self.ISBN, self.Title, self.Author,self.Publisher))class BookList:def __init__(self):self.books = []def show(self):print("%-16s %-16s %-16s %-16s" % ("ISBN", "Title", "Author", "Publisher"))for s in self.books:s.show()def __insert(self, s):i = 0while (i < len(self.books) and s.ISBN > self.books[i].ISBN):i = i + 1if (i < len(self.books) and s.ISBN == self.books[i].ISBN):print(s.ISBN + " 已经存在")return Falseself.books.insert(i, s)print("增加成功") return Truedef __update(self, s):flag = Falsefor i in range(len(self.books)):if (s.ISBN == self.books[i].ISBN):self.books[i].Title = s.Titleself.books[i].Author = s.Authorself.books[i].Publisher = s.Publisherprint("修改成功")flag = Truebreakif (not flag):print("没有这个教材")return flagdef __delete(self, ISBN):flag = Falsefor i in range(len(self.books)):if (self.books[i].ISBN == ISBN):del self.books[i]print("删除成功")flag = Truebreakif (not flag):print("没有这个教材")return flagdef delete(self):ISBN = input("ISBN=")if (ISBN != ""):self.__delete(ISBN)def insert(self):ISBN = input("ISBN=")Title = input("Title=")Author = input("Author=")Publisher = input("Publisher=")if ISBN != "" and Title != "":self.__insert(Book(ISBN, Title, Author, Publisher))else:print("ISBN、教材名称不能空")def update(self): ISBN = input("ISBN=")Title = input("Title=")Author=input("Author=")Publisher = input("Publisher=")if ISBN != "" and Title != "":self.__update(Book(ISBN, Title, Author, Publisher))else:print("ISBN、教材名称不能空")def save(self):try:f = open("books.txt", "wt")for b in self.books:f.write(b.ISBN + "\n")f.write(b.Title + "\n")f.write(b.Author+ "\n")f.write(b.Publisher+"\n")f.close()except Exception as err:print(err)def read(self):self.books=[]try:f = open("books.txt", "rt")while True:ISBN = f.readline().strip("\n")Title = f.readline().strip("\n")Author = f.readline().strip("\n")Publisher = f.readline().strip("\n")if ISBN != "" and Title!="" and Author!="" and Publisher!="":b = Book(ISBN,Title,Author,Publisher)self.books.append(b)else:breakf.close()except:passdef process(self):self.read()while True:s = input(">")if (s == "show"): self.show()elif (s == "insert"):self.insert()elif (s == "update"):self.update()elif (s == "delete"):self.delete()elif (s == "exit"):breakelse:print("show: show Books")print("insert: insert a new Book")print("update: insert a new Book")print("delete: delete a Book")print("exit: exit")self.save()books = BookList()books.process()
夜光:这个程序中先设计教材类 Book,然后设计教材记录管理类 BookList,在这个类中有一 个 books=[]是一个列表,列表的每个元素是一个 Book 对象,这样就记录了一组教材。 增加记录的函数是 insert 与__insert,其中 insert 完成教材信息的输入,__insert 完成教材的真正插入,插入时通过扫描教材编号 ISBN 确定插入新教材的位置,保证插入的教材时按 ISBN 从小到大排列的。
更新记录的函数是 update 与__update,其中 update 完成教材信息的输入,__update 完成 教材的记录的真正更新,更新时通过扫描教材编号 ISBN 确定教材的位置,编号不能更新。 删除记录的函数是 delete 与__delete,其中 delete 完成教材编号的输入,__delete 完成教 材的记录的真正删除。 process 函数启动一个无限循环,不断显示命令提示符号">",等待输入命令,能接受的 命令是 show、insert、update、delete、exit,其它输入无效。
程序咋结束时会把教材记录存储到 books.txt 文件中,而且一本教材占 4 行,顺序是:
ISBN
Title
Author
Publisher
在下次程序启动时会从 books.txt 中读出存储的记录到内存列表 books 中,这个功能十 分类似一个数据库的功能,只不过存储数据的是一个文件而不是数据库~~