// CollectSolutions.java Copyright (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-02-22 File created. // 2006-02-22 Last modification. // Solutions for exercises 14-10, 14-11, 14-12, 14-13, 14-14 // Exercise 14-14 was solved so that the user can select a // collection file via the main menu. This is probably the // best solution to the problem. // In this program, class Collection has a constructor which // automatically loads collection data from a file. import java.util.* ; import java.io.* ; // Classes for file handling. class CollectionItem { String item_name ; String item_maker ; String item_type ; int year_of_making ; public CollectionItem( String given_item_name, String given_item_maker, String given_item_type, int given_year_of_making ) { item_name = given_item_name ; item_maker = given_item_maker ; item_type = given_item_type ; year_of_making = given_year_of_making ; } public CollectionItem( DataInputStream file_containing_collection_data ) throws IOException { item_name = file_containing_collection_data.readUTF() ; item_maker = file_containing_collection_data.readUTF() ; item_type = file_containing_collection_data.readUTF() ; year_of_making = file_containing_collection_data.readInt() ; } public String get_item_name() { return item_name ; } public String get_item_maker() { return item_maker ; } public String get_item_type() { return item_type ; } public int get_year_of_making() { return year_of_making ; } // The following method is the solution to Exercise 14-13. public boolean equals( Object another_object ) { boolean objects_are_equal = false ; if ( another_object != null && another_object instanceof CollectionItem ) { CollectionItem another_collection_item = (CollectionItem) another_object ; if ( item_name == another_collection_item.item_name && item_maker == another_collection_item.item_maker && item_type == another_collection_item.item_type && year_of_making == another_collection_item.year_of_making ) { objects_are_equal = true ; } } return objects_are_equal ; } public String to_line_of_text() { return ( String.format( "\n %-30s%-20s%-15s%-10d", item_name, item_maker, item_type, year_of_making ) ) ; } public void store_to_file( DataOutputStream binary_file ) throws IOException { binary_file.writeUTF( item_name ) ; binary_file.writeUTF( item_maker ) ; binary_file.writeUTF( item_type ) ; binary_file.writeInt( year_of_making ) ; } } class Collection { static final int MAXIMUM_NUMBER_OF_ITEMS = 100 ; CollectionItem[] items_in_this_collection = new CollectionItem[ MAXIMUM_NUMBER_OF_ITEMS ] ; int number_of_items_in_collection = 0 ; boolean collection_data_has_been_modified = false ; String file_of_this_collection = null ; // The default constructor constructs a Collection object // that does not have a file associated with it. These kinds // of Collection objects are returned by some methods of this // class. public Collection() { } public Collection( String given_collection_file ) { file_of_this_collection = given_collection_file ; File given_file = new File( file_of_this_collection ) ; if ( file_of_this_collection != null && given_file.exists() ) { load_from_file() ; } } public int get_number_of_items() { return number_of_items_in_collection ; } public boolean collection_has_been_modified() { return collection_data_has_been_modified ; } public CollectionItem search_item_with_name( String given_item_name ) { boolean item_has_been_found = false ; int item_index = 0 ; while ( item_has_been_found == false && item_index < number_of_items_in_collection ) { if ( items_in_this_collection[ item_index ]. get_item_name().indexOf( given_item_name ) != -1 ) { item_has_been_found = true ; } else { item_index ++ ; } } if ( item_has_been_found == true ) { return items_in_this_collection[ item_index ] ; } else { return null ; } } public void print_all_items() { int item_index = 0 ; while ( item_index < number_of_items_in_collection ) { System.out.print( items_in_this_collection[ item_index ].to_line_of_text() ) ; item_index ++ ; } } public void add_new_item( CollectionItem new_collection_item ) { if ( number_of_items_in_collection >= MAXIMUM_NUMBER_OF_ITEMS ) { System.out.print( "\n Cannot add new collection items!!!! \n" ) ; } else if ( new_collection_item.get_item_name().length() == 0 ) { System.out.print( "\n Invalid collection item data!!!! \n" ) ; } else { items_in_this_collection[ number_of_items_in_collection ] = new_collection_item ; number_of_items_in_collection ++ ; collection_data_has_been_modified = true ; } } public void remove_all_items() { number_of_items_in_collection = 0 ; collection_data_has_been_modified = true ; } public void remove_one_item( CollectionItem item_to_remove ) { boolean item_has_been_found = false ; int item_index = 0 ; while ( item_has_been_found == false && item_index < number_of_items_in_collection ) { if ( items_in_this_collection[ item_index ]. equals( item_to_remove ) ) { item_has_been_found = true ; } else { item_index ++ ; } } if ( item_has_been_found == true ) { // Item will be removed by moving later items one // position upwards in the array of items. while ( item_index < ( number_of_items_in_collection - 1 ) ) { items_in_this_collection[ item_index ] = items_in_this_collection[ item_index + 1 ] ; item_index ++ ; } number_of_items_in_collection -- ; collection_data_has_been_modified = true ; } } public Collection get_items_of_maker( String given_item_maker ) { Collection collection_to_return = new Collection() ; for ( int item_index = 0 ; item_index < number_of_items_in_collection ; item_index ++ ) { if ( items_in_this_collection[ item_index ]. get_item_maker().indexOf( given_item_maker ) != -1 ) { collection_to_return.add_new_item( items_in_this_collection[ item_index ] ) ; } } return collection_to_return ; } public Collection get_items_of_type( String given_item_type ) { Collection collection_to_return = new Collection() ; for ( int item_index = 0 ; item_index < number_of_items_in_collection ; item_index ++ ) { if ( items_in_this_collection[ item_index ]. get_item_type().indexOf( given_item_type ) != -1 ) { collection_to_return.add_new_item( items_in_this_collection[ item_index ] ) ; } } return collection_to_return ; } // The following method was constructed to solve Exercise 14-11. public Collection get_items_with_name( String given_item_name ) { Collection collection_to_return = new Collection() ; for ( int item_index = 0 ; item_index < number_of_items_in_collection ; item_index ++ ) { if ( items_in_this_collection[ item_index ]. get_item_name().indexOf( given_item_name ) != -1 ) { collection_to_return.add_new_item( items_in_this_collection[ item_index ] ) ; } } return collection_to_return ; } public void store_to_file() { try { FileOutputStream file_output_stream = new FileOutputStream( file_of_this_collection ) ; DataOutputStream collection_file = new DataOutputStream( file_output_stream ) ; for ( int item_index = 0 ; item_index < number_of_items_in_collection ; item_index ++ ) { items_in_this_collection[ item_index ]. store_to_file( collection_file ) ; } collection_file.close() ; System.out.print( "\n Collection items have been stored. \n\n" ) ; collection_data_has_been_modified = false ; } catch ( Exception caught_exception ) { System.out.print("\n\n Error in writing file collection_items.data. " + "\n Collection items are not stored. \n\n" ) ; } } public void load_from_file() { try { FileInputStream file_input_stream = new FileInputStream( file_of_this_collection ) ; DataInputStream collection_file = new DataInputStream( file_input_stream ) ; int item_index = 0 ; CollectionItem item_from_file ; while ( collection_file.available() > 0 ) { item_from_file = new CollectionItem( collection_file ) ; items_in_this_collection[ item_index ] = item_from_file ; item_index ++ ; } number_of_items_in_collection = item_index ; collection_file.close() ; System.out.print( "\n Collection items have been loaded. \n\n" ) ; } catch ( FileNotFoundException caught_file_not_found_exception ) { System.out.print("\n File collection_items.data does not exist.\n" ) ; } catch ( Exception caught_exception ) { System.out.print("\n Error in reading collection_items.data.\n" ) ; } } } class CollectionMaintainer { Scanner keyboard = new Scanner( System.in ) ; Collection this_collection ; String collection_file_in_use ; public CollectionMaintainer() { File collection_file_name_file = new File("collection_file_name.txt" ); if ( collection_file_name_file.exists() ) { collection_file_in_use = read_collection_file_name() ; } else { // This else block is executed only once when this // program is being used for the first time, i.e., when // collection_file_name.txt does not exist yet. collection_file_in_use = "collection_items.data" ; store_collection_file_name() ; } // The constructor of class Collection automatically loads // data from the file that is supplied to it. this_collection = new Collection( collection_file_in_use ) ; } String read_collection_file_name() { String collection_file_name = null ; try { BufferedReader file_to_read = new BufferedReader( new FileReader( "collection_file_name.txt" ) ) ; collection_file_name = file_to_read.readLine() ; file_to_read.close() ; } catch ( Exception cought_exception ) { System.out.print( "\n Cannot read collection_file_name.txt" ) ; } return collection_file_name ; } void store_collection_file_name() { try { PrintWriter file_to_write = new PrintWriter( new FileWriter( "collection_file_name.txt" ) ) ; file_to_write.println( collection_file_in_use ) ; file_to_write.close() ; } catch ( Exception caught_exception ) { System.out.print( "\n Cannot write collection_file_name.txt" ) ; } } boolean user_confirms( String text_to_confirm ) { // This method returns true if user types in 'Y' or 'y'. boolean user_gave_positive_answer = false ; String user_response = "?????" ; while ( user_response.charAt( 0 ) != 'Y' && user_response.charAt( 0 ) != 'N' ) { System.out.print( text_to_confirm ) ; user_response = keyboard.nextLine().toUpperCase() ; if ( user_response.length() == 0 ) { user_response = "?????" ; } else if ( user_response.charAt( 0 ) == 'Y' ) { user_gave_positive_answer = true ; } } return user_gave_positive_answer ; } void add_new_collection_items() { boolean user_wants_to_add_more_collection_items = true ; while ( user_wants_to_add_more_collection_items == true ) { System.out.print( "\n Give new collection item name or press just" + "\n Enter to stop adding new items: " ) ; String item_name_of_the_new_item = keyboard.nextLine() ; if ( item_name_of_the_new_item.length() == 0 ) { user_wants_to_add_more_collection_items = false ; } else { System.out.print( " Give the artist name: " ) ; String item_maker_of_the_new_item = keyboard.nextLine() ; System.out.print( " Give collection item type: " ) ; String item_type_of_the_new_item = keyboard.nextLine() ; System.out.print( " Give the year of making: " ) ; int year_of_making_of_the_new_item = Integer.parseInt( keyboard.nextLine() ) ; CollectionItem new_collection_item = new 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 ) ; this_collection.add_new_item( new_collection_item ) ; System.out.print( "\n New item has been added to collection. " ) ; } } } void remove_collection_item() { System.out.print( "\n Give the name of the item to remove: " ) ; String item_name_from_user = keyboard.nextLine() ; CollectionItem item_to_remove = this_collection.search_item_with_name( item_name_from_user ) ; if ( item_to_remove != null ) { // An item was found in the collection. System.out.print( "\n This collection item was found: " + item_to_remove.to_line_of_text() ) ; if ( user_confirms( "\n Remove this item ( Y/N )?" ) ) { this_collection.remove_one_item( item_to_remove ) ; } else { System.out.print( "\n No item was removed. " ) ; } } else { System.out.print( "\n The item being searched was not found!!!!" ) ; } } void print_data_of_collection_items() { Collection items_to_print = null ; System.out.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 Print items that have a certain name." + "\n\n " ) ; String user_selection = keyboard.nextLine() ; if ( user_selection.charAt( 0 ) == 'a' ) { items_to_print = this_collection ; } else if ( user_selection.charAt( 0 ) == 't' ) { System.out.print( "\n Give the type of the items to be printed: " ) ; String item_type_from_user = keyboard.nextLine() ; items_to_print = this_collection. get_items_of_type( item_type_from_user ) ; } else if ( user_selection.charAt( 0 ) == 'm' ) { System.out.print( "\n Give the name of the maker of the item: " ) ; String item_maker_from_user = keyboard.nextLine() ; items_to_print = this_collection. get_items_of_maker( item_maker_from_user ) ; } else if ( user_selection.charAt( 0 ) == 'n' ) { System.out.print( "\n Give the name of the item: " ) ; String item_name_from_user = keyboard.nextLine() ; items_to_print = this_collection. get_items_with_name( item_name_from_user ) ; } if ( items_to_print != null ) { items_to_print.print_all_items() ; } } void select_new_collection_file() { File this_directory = new File( "." ) ; String[] all_files_in_this_directory = this_directory.list() ; String[] data_files_in_this_directory = new String[ all_files_in_this_directory.length ] ; // First we'll make a list of all .data files in this directory int data_file_index = 0 ; for ( int file_index = 0 ; file_index < all_files_in_this_directory.length ; file_index ++ ) { if ( all_files_in_this_directory[ file_index ].endsWith( ".data" ) ) { data_files_in_this_directory[ data_file_index ] = all_files_in_this_directory[ file_index ] ; data_file_index ++ ; } } int total_number_of_data_files = data_file_index ; System.out.print( "\n The following are .data files available: \n" ) ; for ( data_file_index = 0 ; data_file_index < total_number_of_data_files ; data_file_index ++ ) { System.out.print( "\n " + ( data_file_index + 1 ) + " " + data_files_in_this_directory[ data_file_index ] ); } System.out.print( "\n Select one of them with a number or give " + "\n a new file name: " ) ; String user_selection = keyboard.nextLine() ; int number_of_selected_file ; String selected_file_name ; try { // Any int number is accepted here. However, if the user gives // a number that is too large, the program terminates later // because of a null pointer exception. number_of_selected_file = Integer.parseInt( user_selection ) ; selected_file_name = data_files_in_this_directory[ number_of_selected_file - 1 ] ; } catch ( Exception caught_exception ) { // The user did not give a number. So we suppose that he or she // gave the name of a new file user_selection = user_selection.toLowerCase() ; if ( user_selection.length() > 5 && ! user_selection.contains( " " ) && user_selection.endsWith( ".data" ) ) { // The given file name is acceptable. selected_file_name = user_selection ; } else { System.out.print( "\n Not acceptable collection data file name" + "\n The name must end with .data" + "\n collection_items.data file will be loaded"); selected_file_name = "collection_items.data" ; } } collection_file_in_use = selected_file_name ; store_collection_file_name() ; // to file collection_file_name.txt this_collection = new Collection( selected_file_name ) ; } void initialize_collection_with_test_data() { this_collection.remove_all_items() ; this_collection.add_new_item( new CollectionItem( "Les Demoiselles d'Avignon","Pablo Picasso", "painting", 1907 ) ); this_collection.add_new_item( new CollectionItem( "The Third of May 1808", "Francisco de Goya", "painting", 1808 ) ); this_collection.add_new_item( new CollectionItem( "Dejeuner sur l'Herbe", "Eduard Manet", "painting", 1863 ) ); this_collection.add_new_item( new CollectionItem( "Mona Lisa", "Leonardo da Vinci", "painting", 1503 ) ); this_collection.add_new_item( new CollectionItem( "David", "Michelangelo", "statue", 1501 ) ); this_collection.add_new_item( new CollectionItem( "The Night Watch", "Rembrandt", "painting", 1642 ) ); this_collection.add_new_item( new CollectionItem( "Guernica", "Pablo Picasso", "painting", 1937 ) ); this_collection.add_new_item( new CollectionItem( "Ulysses", "James Joyce", "manuscript", 1922 )); this_collection.add_new_item( new CollectionItem( "The Egyptian", "Mika Waltari", "manuscript", 1946 )); this_collection.add_new_item( new CollectionItem( "For Whom the Bell Tolls", "Ernest Hemingway","manuscript", 1941 )); } public void run() { String user_selection = "???????" ; System.out.print( "\n This program is a system to help a collector" + "\n to maintain information about his or her" + "\n valuable collection items.\n" ) ; while ( user_selection.charAt( 0 ) != 'q' ) { System.out.print( "\n\n There are currently " + this_collection.get_number_of_items() + " items in the collection. " + "\n Current collection file: " + collection_file_in_use + "\n Choose what to do by typing in a letter " + "\n according to the following menu: \n" + "\n a Add new collection items. " + "\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 f Select collection file." + "\n i Initialize collection with test data. " + "\n q Quit the system.\n\n " ) ; user_selection = keyboard.nextLine() ; if ( user_selection.length() == 0 ) { System.out.print( "\n Please type in a letter." ) ; user_selection = "?" ; } else if ( user_selection.charAt( 0 ) == 'a' ) { add_new_collection_items() ; } else if ( user_selection.charAt( 0 ) == 'r' ) { remove_collection_item() ; } else if ( user_selection.charAt( 0 ) == 'p' ) { print_data_of_collection_items() ; } else if ( user_selection.charAt( 0 ) == 's' ) { this_collection.store_to_file() ; } else if ( user_selection.charAt( 0 ) == 'l' ) { this_collection.load_from_file() ; } else if ( user_selection.charAt( 0 ) == 'f' ) { // Selecting a new collection file. if ( this_collection.collection_has_been_modified() ) { System.out.print( "\n Current collection has been modified." ) ; if ( user_confirms( "\n Store to file Y/N ? " ) ) { this_collection.store_to_file() ; } } select_new_collection_file() ; } else if ( user_selection.charAt( 0 ) == 'i' ) { initialize_collection_with_test_data() ; } else if ( user_selection.charAt( 0 ) == 'q' ) { if ( this_collection.collection_has_been_modified() ) { System.out.print( "\n Collection has been modified." ) ; if ( user_confirms( "\n Store to file Y/N ? " ) ) { this_collection.store_to_file() ; } } } } } } class CollectSolutions { public static void main( String[] not_in_use ) { CollectionMaintainer collection_maintainer = new CollectionMaintainer() ; collection_maintainer.run() ; } }