# This code implements a Library application that lets user store and # retrieve books from a shelf. # # Tasks: # - Fix storeNewBook, and getBook # - Can you add a way to see all books in the library? # - Make a way to update a book? # - Make sure you cannot add a book with the same title (using compare?) # #from BookModule import Book import BookModule import shelve def storeNewBook(): 'Store a new book in the library' print 'Store New Book' # Ask the user for the input needed to create a book title = raw_input('Get title: ') author = raw_input('Get author: ') numPages = input('Get number of pages: ') # Create the book aBook = BookModule.Book(title, author, numPages) print 'The book is', aBook if bookInShelve(title): print 'ERROR: That book is already on the shelve!' else: # Add the book into the shelf based on the title (make sure to lowercase the title) library[title.lower()] = aBook # Make sure to update the list of titles listOfTitles = library['listOfTitles'] listOfTitles.append(title) def bookInShelve(title): 'Return true if the book in shelve, false otherwise' listOfTitles = library['listOfTitles'] if title in listOfTitles: return True else: return False def getBook(): 'Get information about a book from the library' print 'Get Book' # Get the title we want to see title = raw_input('Give me a title: ') if bookInShelve(title): # Look up the book in the library myBook = library[title.lower()] # Print out the book print myBook else: print 'Error: That book is not on the shelve' def updateBook(): 'Get the book from the library and update pieces of it' print 'UpdateBook' def main(): global library library = shelve.open('lib', writeback=True) # Open the lib shelve choice = 'z' while choice != 'q': print ''' ************************ MENU ------------------------ (s)tore a new book (g)et book info (u)pdate book info (q)uit ************************ ''' choice = raw_input('Enter choice:') if choice == 's': storeNewBook() elif choice =='g': getBook() elif choice =='u': updateBook() elif choice == 'q': '' # Do nothing, but we know this was a valid option else: print '\n\nSorry, %s is not a valid option \n\n' %(choice) print 'The library is closing!' library.close() main()