// CollectSortable.java (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-06-20 File created. // 2005-06-20 Last modification. // A solution to Exercise 15-7. // This version is made from CollectWithArrayList.java. // This version does not contain the features added to // CollectSolutions.java import java.util.* ; import java.io.* ; // Classes for file handling. class CollectionItem implements Comparable { 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 ; } public boolean is_equal_to( CollectionItem another_collection_item ) { return ( item_name.equals( another_collection_item.item_name ) && item_maker.equals( another_collection_item.item_maker ) && item_type.equals( another_collection_item.item_type ) && year_of_making == another_collection_item.year_of_making ) ; } 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 ) ; } public int compareTo( CollectionItem item_to_compare_to ) { return item_maker.compareTo( item_to_compare_to.item_maker ) ; } } // Only class Collection is different in this program when // you compare this to the original Collect.java. // Because the ArrayList method size() can be used to // count how many items are in a Collection, the data field // number_of_items_in_collection was removed from the class. // Neither it is necessary to have the MAXIMUM_NUMBER_OF_ITEMS // field, because ArrayList arrays can store quite a lot of // objects. class Collection { ArrayList items_in_this_collection = new ArrayList() ; public int get_number_of_items() { return items_in_this_collection.size() ; } 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 < items_in_this_collection.size() ) { if ( items_in_this_collection.get( item_index ). get_item_name().contains( given_item_name ) ) { item_has_been_found = true ; } else { item_index ++ ; } } if ( item_has_been_found == true ) { return items_in_this_collection.get( item_index ) ; } else { return null ; } } public void print_all_items() { int item_index = 0 ; while ( item_index < items_in_this_collection.size() ) { System.out.print( items_in_this_collection.get( item_index ).to_line_of_text() ) ; item_index ++ ; } } public void add_new_item( CollectionItem new_collection_item ) { if ( new_collection_item.get_item_name().length() == 0 ) { System.out.print( "\n Invalid collection item data!!!! \n" ) ; } else { items_in_this_collection.add( new_collection_item ) ; } } public void remove_all_items() { items_in_this_collection.clear() ; } 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 < items_in_this_collection.size() ) { if ( items_in_this_collection.get( item_index ). is_equal_to( item_to_remove ) ) { item_has_been_found = true ; } else { item_index ++ ; } } if ( item_has_been_found == true ) { items_in_this_collection.remove( item_index ) ; } } public Collection get_items_of_maker( String given_item_maker ) { Collection collection_to_return = new Collection() ; for ( int item_index = 0 ; item_index < items_in_this_collection.size() ; item_index ++ ) { if ( items_in_this_collection.get( item_index ). get_item_maker().contains( given_item_maker ) ) { collection_to_return.add_new_item( items_in_this_collection.get( 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 < items_in_this_collection.size() ; item_index ++ ) { if ( items_in_this_collection.get( item_index ). get_item_type().contains( given_item_type ) ) { collection_to_return.add_new_item( items_in_this_collection.get( item_index ) ) ; } } return collection_to_return ; } public void sort_collection() { Collections.sort( items_in_this_collection ) ; System.out.print( "\n Collection items have been sorted. \n\n" ) ; } public void store_to_file() { try { FileOutputStream file_output_stream = new FileOutputStream( "collection_items.data" ) ; DataOutputStream collection_file = new DataOutputStream( file_output_stream ) ; for ( int item_index = 0 ; item_index < items_in_this_collection.size() ; item_index ++ ) { items_in_this_collection.get( item_index ). store_to_file( collection_file ) ; } collection_file.close() ; System.out.print( "\n Collection items have been stored. \n\n" ) ; } 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( "collection_items.data" ) ; DataInputStream collection_file = new DataInputStream( file_input_stream ) ; items_in_this_collection.clear() ; CollectionItem item_from_file ; while ( collection_file.available() > 0 ) { item_from_file = new CollectionItem( collection_file ) ; items_in_this_collection.add( item_from_file ) ; } 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 = new Collection() ; 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_item() { System.out.print( "\n Give new collection item name: " ) ; String item_name_of_the_new_item = keyboard.nextLine() ; 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 " ) ; 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 ) ; } if ( items_to_print != null ) { items_to_print.print_all_items() ; } } 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 Choose what to do by typing in a letter " + "\n according to the following menu: \n" + "\n a Add a new collection item. " + "\n r Remove a collection item. " + "\n p Print data of collection items. " + "\n o Sort collection." + "\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 " ) ; 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_item() ; } 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 ) == 'o' ) { this_collection.sort_collection() ; } 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 ) == 'i' ) { initialize_collection_with_test_data() ; } } } } class CollectSortable { public static void main( String[] not_in_use ) { CollectionMaintainer collection_maintainer = new CollectionMaintainer() ; collection_maintainer.run() ; } }