python_案例综合:教材记录管理
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)