/** * Write a description of class Library here. * * @author (your name) * @version (a version number or a date) */ // Import all required libraries. Not using .* as it is not good practice due to potential conflicts. import java.util.ArrayList; import java.util.Scanner; import java.io.File; import java.io.IOException; import java.awt.FileDialog; import java.awt.Frame; public class Library { private ArrayList itemList; // Initialise an ArrayList of name itemList to store LibraryItems /* * Constructor for objects of class Library */ public Library() { itemList = new ArrayList(); } /* * Appends a LibraryItem to the itemList. */ public void storeItem( LibraryItem item ) { itemList.add( item ); } /* * Prints to the terminal all items in the itemList */ public void printAllItems() { for( LibraryItem item : itemList ) { item.printDetails(); } } public void readItemData() //throws IOException { Frame frame = null; // Initialise a null frame FileDialog fileBox = new FileDialog( frame, "Open", FileDialog.LOAD ); // Initialise filebox with the null frame pointer fileBox.setVisible( true ); // Open a file selection dialog to the user. try { Scanner fileScanner = new Scanner( new File( fileBox.getDirectory() + fileBox.getFile() ) ); String typeFlag = ""; while( fileScanner.hasNextLine() ){ String lineItem = fileScanner.nextLine(); //System.out.println( lineItem ); // Ensure no comments or empty lines are included if ( lineItem.contains( "//" ) || lineItem.trim().isEmpty() ){} // Check current line is a candidate flag else if ( lineItem.startsWith("[" ) ) { if ( lineItem.toLowerCase().contains("book") ) { typeFlag = "book"; //System.out.println( "CHANGED FLAG TO READ BOOKS" ); } else if ( lineItem.toLowerCase().contains("periodical") ) { typeFlag = "periodical"; //System.out.println( "CHANGED FLAG TO READ PERIODICALS" ); } else { System.out.println( "Flag detected, but no accepted format...\n Cannot store item in Library. Changing Flag to generic and skipping the following: "); typeFlag = "generic"; } } // Could be a switch case to be more efficient // Check current flag for data processing. else { if ( typeFlag.equals( "book" ) ) { // Process Book Data Scanner detailScanner = new Scanner ( lineItem ).useDelimiter(","); // Create a new scanner to grab the values in a comma separated list LibraryItem book = new Book(); book.readItemData( detailScanner ); storeItem( book ); // Store the new LibraryItem in the itemList } else if ( typeFlag.equals( "periodical" ) ) { // Process Periodic Data Scanner detailScanner = new Scanner ( lineItem ).useDelimiter(","); LibraryItem periodical = new Periodical(); periodical.readItemData( detailScanner ); storeItem( periodical ); } else if ( typeFlag.equals( "generic" ) ) { // Output unaccepted lines System.out.println( lineItem ); } } } } catch( IOException e ) { // Catch any IO Exceptions that may occur from improper file selection. System.err.println( "Caught IOException: " + e.getMessage() + "\nAn I/O Exception has occurred, please check file is readable and correct format." ); } } }