前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python_案例综合:教材记录管理

python_案例综合:教材记录管理

作者头像
Mirror王宇阳
发布2020-11-13 10:52:09
8090
发布2020-11-13 10:52:09
举报
代码语言:javascript
复制
  1 class Book():
  2     def __init__(self,ISBN,Ftile,Author,Publisher):
  3         self.ISBN = ISBN
  4         self.Ftile = Ftile
  5         self.Author = Author
  6         self.Publisher = Publisher
  7 
  8     def show(self):
  9         print(self.ISBN,self.Ftile,self.Author,self.Publisher)
 10 
 11 class BookList():
 12     def __init__(self):
 13         self.books = []
 14 
 15     def show(self):
 16         print(self.ISBN, self.Ftile, self.Author, self.Publisher)
 17         for i in self.books:
 18             self.show()
 19 
 20     def __insert(self,s):
 21         i = 0
 22         while(i<len(self.books) and s.ISBN > self.books[i]):
 23             i=i+1
 24         self.books.insert(i,s)#指定位置添加一个元素
 25         print("ok")
 26 
 27     def __update(self,s):
 28         i = 0
 29         while(i<len(self.books) and s.ISBN != self.books[i]):
 30             i=i+1
 31         if(s.ISBN==self.books[i]):
 32             del self.books[i]
 33             self.books.insert(i,s)
 34             print("-- update @ ok --")
 35         else:
 36             print("--error--")
 37 
 38     def __delete(self):
 39         while(i<len(self.books) and s.ISBN != self.books[1]):
 40             i=i+1
 41         if(s.ISBN ==self.books[i]):
 42             del self.books[i]
 43             print("-- delete @ ok --")
 44         else:
 45             print("--error--")
 46 
 47     def insert(self):
 48         ISBN = input('ISBN:')
 49         Ftile = input('Ftile:')
 50         Author = input('Author:')
 51         Publisher = input('Publisher:')
 52         if (ISBN!='' and Ftile!=''):
 53               self.__insert(Book(ISBN,Ftile,Author,Publisher))
 54         else:
 55             print("--ISBN/Ftile^error--")
 56 
 57     def update(self):
 58         ISBN = input('ISBN:')
 59         Ftile = input('Ftile:')
 60         Author = input('Author:')
 61         Publisher = input('Publisher:')
 62         if (ISBN != '' or Ftile != '' or Author!='' or Publisher!=''):
 63             self.__update(Book(ISBN, Ftile, Author, Publisher))
 64         else:
 65             print("---error---")
 66 
 67     def delete(self):
 68         ISBN = input('ISBN:')
 69         Ftile = input('Ftile:')
 70         Author = input('Author:')
 71         Publisher = input('Publisher:')
 72         if (ISBN != '' or Ftile != '' or Author != '' or Publisher != ''):
 73             self.__delete(Book(ISBN, Ftile, Author, Publisher))
 74         else:
 75             print("---error---")
 76 
 77     def scan(self):
 78         while True:
 79             s = input(">>>")
 80             if s=='show':
 81                 self.show()
 82             elif s=='insert':
 83                 self.insert()
 84             elif s=='update':
 85                 self.update()
 86             elif s=='delete':
 87                 self.delete()
 88             elif s=='exit':
 89                 break
 90             else:
 91                 print(" ---- Error ---- ")
 92                 return 0
 93         self.File()
 94         #print(self.books)   》》》》[<__main__.Book object at 0x0000020B3A08CFD0>]
 95 
 96     def File(self):
 97         f = open('jiaocai.txt','wt+',encoding = 'utf-8')
 98         for i in self.books:
 99             f.write(i.ISBN+'\n')
100             f.write(i.Ftile+'\n')
101             f.write(i.Author+'\n')
102             f.write(i.Publisher+'\n')
103         #books = self.books
104         #f.write(str(books))#不可用
105         f.close()
106 
107 Bl=BookList()
108 Bl.scan()
109 #print(BookList.books)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-11-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档