# Collect.py Copyright (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-05-15 File created. # 2022-12-18 Converted to Python 3. # This program does not store collection data in binary form. # Instead, the functions of the pickle module are used to # store and load CollectionItem objects to/from a file. import pickle # a mechanism for storing objects to a file class CollectionItem : def __init__( self, given_item_name, given_item_maker, given_item_type, given_year_of_making ) : self.item_name = given_item_name self.item_maker = given_item_maker self.item_type = given_item_type self.year_of_making = given_year_of_making def get_item_name( self ) : return self.item_name def get_item_maker( self ) : return self.item_maker def get_item_type( self ) : return self.item_type def get_year_of_making( self ) : return self.year_of_making def is_equal_to( self, another_collection_item ) : return self.item_name == another_collection_item.item_name and \ self.item_maker == another_collection_item.item_maker and \ self.item_type == another_collection_item.item_type and \ self.year_of_making == another_collection_item.year_of_making def to_line_of_text( self ) : return "\n %-30s%-20s%-15s%-10d" % ( self.item_name, self.item_maker, self.item_type, self.year_of_making ) class Collection : def __init__( self ) : self.items_in_this_collection = [] def get_number_of_items( self ) : return len( self.items_in_this_collection ) def search_item_with_name( self, given_item_name ) : item_has_been_found = False item_index = 0 while item_has_been_found == False and \ item_index < len( self.items_in_this_collection ) : if self.items_in_this_collection[ item_index ]. \ get_item_name().find( given_item_name ) != -1 : item_has_been_found = True else : item_index += 1 if item_has_been_found == True : return self.items_in_this_collection[ item_index ] else : return None def print_all_items( self ) : item_index = 0 while item_index < len( self.items_in_this_collection ) : print( self.items_in_this_collection[ item_index ].to_line_of_text(), end="" ) item_index += 1 def add_new_item( self, new_collection_item ) : if len( new_collection_item.get_item_name() ) == 0 : print( "\n Invalid collection item data!!!! " ) else : self.items_in_this_collection.append( new_collection_item ) def remove_all_items( self ) : self.items_in_this_collection = [] def remove_one_item( self, item_to_remove ) : item_has_been_found = False item_index = 0 while item_has_been_found == False and \ item_index < len( self.items_in_this_collection ) : if self.items_in_this_collection[ item_index ]. \ is_equal_to( item_to_remove ) : item_has_been_found = True else : item_index += 1 if item_has_been_found == True : del self.items_in_this_collection[ item_index : item_index + 1 ] def get_items_of_maker( self, given_item_maker ) : collection_to_return = Collection() for item_in_collection in self.items_in_this_collection : if item_in_collection.get_item_maker().find( \ given_item_maker ) != -1 : collection_to_return.add_new_item( item_in_collection ) return collection_to_return def get_items_of_type( self, given_item_type ) : collection_to_return = Collection() for item_in_collection in self.items_in_this_collection : if item_in_collection.get_item_type().find( \ given_item_type ) != -1 : collection_to_return.add_new_item( item_in_collection ) return collection_to_return def store_to_file( self ) : collection_file = open( "collection_items.data", "wb" ) pickle.dump( len( self.items_in_this_collection ), collection_file ) for item_in_collection in self.items_in_this_collection : pickle.dump( item_in_collection, collection_file ) collection_file.close() print( "\n Collection items have been stored. " ) def load_from_file( self ) : collection_file = open( "collection_items.data", "rb" ) number_of_items_to_load = pickle.load( collection_file ) self.items_in_this_collection = [] number_of_loaded_items = 0 while number_of_loaded_items < number_of_items_to_load : item_from_file = pickle.load( collection_file ) self.items_in_this_collection.append( item_from_file ) number_of_loaded_items += 1 collection_file.close() class CollectionMaintainer : this_collection = Collection() def user_confirms( self, text_to_confirm ) : # This method returns True if user types in 'Y' or 'y'. user_gave_positive_answer = False user_response = "?????" while user_response[ 0 ] != 'Y' and \ user_response[ 0 ] != 'N' : print( text_to_confirm, end="" ) user_response = input().upper() if len( user_response ) == 0 : user_response = "?????" elif user_response[ 0 ] == 'Y' : user_gave_positive_answer = True return user_gave_positive_answer def add_new_collection_item( self ) : print( "\n Give new collection item name: ", end="" ) item_name_of_the_new_item = input() print( " Give the artist name: ", end="" ) item_maker_of_the_new_item = input() print( " Give collection item type: ", end="" ) item_type_of_the_new_item = input() print( " Give the year of making: ", end="" ) year_of_making_of_the_new_item = int( input() ) new_collection_item = \ CollectionItem( item_name_of_the_new_item, item_maker_of_the_new_item, item_type_of_the_new_item, year_of_making_of_the_new_item ) self.this_collection.add_new_item( new_collection_item ) print( "\n New item has been added to collection.", end="" ) def remove_collection_item( self ) : print( "\n Give the name of the item to remove: ", end="" ) item_name_from_user = input() item_to_remove = \ self.this_collection.search_item_with_name( item_name_from_user ) if item_to_remove != None : # An item was found in the collection. print( "\n This collection item was found: %s" % \ item_to_remove.to_line_of_text(), end="" ) if self.user_confirms( "\n Remove this item ( Y/N )? " ) : self.this_collection.remove_one_item( item_to_remove ) else : print( "\n No item was removed.", end="" ) else : print( "\n The item being searched was not found!!!!", end="" ) def print_data_of_collection_items( self ) : items_to_print = None print( "\n Type in a letter according to menu: \n" \ "\n a Print all collection items. " \ "\n t Print certain types of items. " \ "\n m Print items according to maker's name." \ "\n\n ", end="" ) user_selection = input() if user_selection[ 0 ] == 'a' : items_to_print = self.this_collection elif user_selection[ 0 ] == 't' : print( "\n Give the type of the items to be printed: ", end="" ) item_type_from_user = input() items_to_print = self.this_collection. \ get_items_of_type( item_type_from_user ) elif user_selection[ 0 ] == 'm' : print( "\n Give the name of the maker of the item: ", end="" ) item_maker_from_user = input() items_to_print = self.this_collection. \ get_items_of_maker( item_maker_from_user ) if items_to_print != None : items_to_print.print_all_items() def initialize_collection_with_test_data( self ) : self.this_collection.remove_all_items() self.this_collection.add_new_item( CollectionItem( "Les Demoiselles d'Avignon","Pablo Picasso", "painting", 1907 ) ) self.this_collection.add_new_item( CollectionItem( "The Third of May 1808", "Francisco de Goya", "painting", 1808 ) ) self.this_collection.add_new_item( CollectionItem( "Dejeuner sur l'Herbe", "Eduard Manet", "painting", 1863 ) ) self.this_collection.add_new_item( CollectionItem( "Mona Lisa", "Leonardo da Vinci", "painting", 1503 ) ) self.this_collection.add_new_item( CollectionItem( "David", "Michelangelo", "statue", 1501 ) ) self.this_collection.add_new_item( CollectionItem( "The Night Watch", "Rembrandt", "painting", 1642 ) ) self.this_collection.add_new_item( CollectionItem( "Guernica", "Pablo Picasso", "painting", 1937 ) ) self.this_collection.add_new_item( CollectionItem( "Ulysses", "James Joyce", "manuscript", 1922 )) self.this_collection.add_new_item( CollectionItem( "The Egyptian", "Mika Waltari", "manuscript", 1946 )) self.this_collection.add_new_item( CollectionItem( "For Whom the Bell Tolls", "Ernest Hemingway","manuscript", 1941 )) def run( self ) : user_selection = "???????" print( "\n This program is a system to help a collector" \ "\n to maintain information about his or her" \ "\n valuable collection items." ) while user_selection[ 0 ] != 'q' : print( "\n\n There are currently %d items in the collection." % \ self.this_collection.get_number_of_items(), end="" ) print( "\n Choose what to do by typing in a letter " \ "\n according to the following menu: " ) print( "\n a Add a new collection item. " \ "\n r Remove a collection item. " \ "\n p Print data of collection items. " \ "\n s Store collection data to file. " \ "\n l Load collection data from file. " \ "\n i Initialize collection with test data. " \ "\n q Quit the system.\n\n ", end="" ) user_selection = input() if len( user_selection ) == 0 : print( "\n Please type in a letter.", end="" ) user_selection = "?" elif user_selection[ 0 ] == 'a' : self.add_new_collection_item() elif user_selection[ 0 ] == 'r' : self.remove_collection_item() elif user_selection[ 0 ] == 'p' : self.print_data_of_collection_items() elif user_selection[ 0 ] == 's' : self.this_collection.store_to_file() elif user_selection[ 0 ] == 'l' : self.this_collection.load_from_file() elif user_selection[ 0 ] == 'i' : self.initialize_collection_with_test_data() # The main program. collection_maintainer = CollectionMaintainer() collection_maintainer.run()