零基础 学 python开发 (Genius套餐A) 三十六

版权声明:Genius https://blog.csdn.net/weixin_41987706/article/details/90181576

夜光序言:

 

 

一步一微笑,一步一伤心,一步一劫难,尽管记忆再悲伤,我却笑着,不愿遗忘。

 

 

 

 

 

 

正文:

6.6 实践项目 教材记录管理

6.6.1 项目目标

这个项目是通过面向对象的方法设计教材类 Book,包含一个教材 ISBN、名称(Title)、 作者(Author)、出版社(Publisher),然后设计教材记录管理类 BookList 来管理一组教材记录。

程序运行后显示">"的提示符号,在">"后面可以输入 showinsertupdatedelete 等命 令实现记录的显示、插入、修改、删除等功能,执行一个命令后继续显示">"提示符号,如 果输入 exit 就退出系统,输入的命令不正确时会提示正确的输入命令,操作过程类似学生记录管理项目。

在程序启动时会读取 books.txt 的教程记录,在程序结束时会把记录存储在 books.txt 文 件中。


6.6.2 项目实践

class Book:

def __init__(self, ISBN, Title, Author, Publisher):

self.ISBN = ISBN

self.Title = Title

self.Author = Author

self.Publisher = Publisher

def 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 = 0

while (i < len(self.books) and s.ISBN > self.books[i].ISBN):

i = i + 1

if (i < len(self.books) and s.ISBN == self.books[i].ISBN):

print(s.ISBN + " 已经存在")

return False

self.books.insert(i, s)

print("增加成功") return True

def __update(self, s):

flag = False

for i in range(len(self.books)):

if (s.ISBN == self.books[i].ISBN):

self.books[i].Title = s.Title

self.books[i].Author = s.Author

self.books[i].Publisher = s.Publisher

print("修改成功")

flag = True

break

if (not flag):

print("没有这个教材")

return flag

def __delete(self, ISBN):

flag = False

for i in range(len(self.books)):

if (self.books[i].ISBN == ISBN):

del self.books[i]

print("删除成功")

flag = True

break

if (not flag):

print("没有这个教材")

return flag

def 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:

break

f.close()

except:

pass

def 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"):

break

else:

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 函数启动一个无限循环,不断显示命令提示符号">",等待输入命令,能接受的 命令是 showinsertupdatedeleteexit,其它输入无效。

程序咋结束时会把教材记录存储到 books.txt 文件中,而且一本教材占 4 行,顺序是:

ISBN

Title

Author

Publisher

在下次程序启动时会从 books.txt 中读出存储的记录到内存列表 books 中,这个功能十 分类似一个数据库的功能,只不过存储数据的是一个文件而不是数据库~~

猜你喜欢

转载自blog.csdn.net/weixin_41987706/article/details/90181576