# 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 # - Print out the list of book, but in order of number of pages? # #from BookModule import Book import BookModule import shelve def bookAlreadyInLibrary(aBook): 'Determine if the book is in the library' listOfTitles = library['listOfTitles'] if aBook.getTitle() in listOfTitles: return True else: return False 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('Title is: ') author = raw_input('Author is: ') numPages = input('Num pages is: ') # Create the book aBook = BookModule.Book(title, author, numPages) print 'The book is', aBook if bookAlreadyInLibrary(aBook): print 'ERROR: This book is already present! Cannot re-add' else: # Add the book into the shelf based on the title (make sure to lowercase the title) library[title.lower()] = aBook # Make sure to keep the list of titles up to date! listOfTitles = library['listOfTitles'] listOfTitles.append(title) def getBook(): 'Get information about a book from the library' print 'Get Book' # Get the title we want to see title = raw_input('Title is: ') # Look up the book in the library myBook = library[title.lower()] # Show the book information print myBook def updateBook(): 'Get the book from the library and update pieces of it' print 'UpdateBook' # Ask the user for the book to update title = raw_input('Get title: ') # Get the new info author = raw_input('Author is: ') numPages = input('Num pages is: ') # Create the book aBook = BookModule.Book(title, author, numPages) # Check if the book is present in the library if bookAlreadyInLibrary(aBook): # Replace the old with the new library[aBook.getTitle().lower()] = aBook else: print "ERROR: The book is not in the library! Cannot update it!" def main(): global library choice = 'z' library = shelve.open('lib', writeback=True) # Opens my library 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': None # 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() # Close the library main()