# L-16 MCS 260 Wed 17 Feb 2010 : a library # # We demonstrate the use of files with a simple model # of a library. from bkform import * def show_menu(): "shows the menu and prompts for a choice" print "Welcome to our library! Choose:" print " 0. leave the program" print " 1. show all books in the collection" print " 2. add a new book to the collection" print " 3. check out a book of the library" print " 4. return a book to the library" c = raw_input("Type 0, 1, 2, 3, or 4 : "); return c def number_of_books(): "returns the number of books in the collection" file = open('books','r') collection = file.readlines() N = len(collection) file.close() return N def show_books(): "shows the books currently in the collection" file = open('books','r') collection = file.readlines() for book in collection: s = ' ' + str(get_key(book)) s += ' ' + get_title(book) if get_status(book): print s + ' available' else: print s + ' checked out' file.close() def add_book(): "adds a book to the collection" N = number_of_books() title = raw_input('Give title : ') s = pack(N+1,1,title) file = open('books','a') file.write(s) file.close() def checkout(): "checks out a book" show_books() k = input('give book number : ') file = open('books','r') collection = file.readlines() file.close() file = open('books','w') for book in collection: if get_key(book) == k: if not get_status(book): print get_title(book) + ' is unavailable' file.write(book) else: file.write(pack(k,0,get_title(book))) else: file.write(book) file.close() def checkin(): "return a book to the library" k = input('give book number : ') file = open('books','r') collection = file.readlines() file.close() file = open('books','w') for book in collection: if get_key(book) == k: d = pack(k,1,get_title(book)) file.write(d) else: file.write(book) file.close() def main(): while True: choice = show_menu() if choice == '0': break if choice == '1': show_books() if choice == '2': add_book() if choice == '3': checkout() if choice == '4': checkin() main()