Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Lab 11
- *
- * This code will read in a "flat" file containing data items for use in instantiating Bill objects.
- * A variety of Bill objects will be instantiated based on the input values.
- * Those objects will be added to a (globally declared) generic collection for holding/serialization
- * Once all objects have been instantiated, the (globally declared) collection will be output
- * as an XML-serialized document.
- *
- * (c) 2019, Terri Davis
- */
- // ---------------------- IMPORTS for File Handling/Management ------------------------------
- // -------------------- IMPORTS for Collection Handling/Management ---------------------------
- import java.util.List; // Interface provides methods for manipulating objects by index
- import java.util.ArrayList; // Class implements interfaces List & Collection
- // --------------------------- IMPORT for XML Serialization ---------------------------------
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.io.File;
- import java.nio.file.Paths;
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- import javax.xml.bind.JAXB;
- public class CreateBillObjects_Batch
- {
- /*
- * Declared globally, as it will be needed in both sub-methods. This avoids having to pass the reference
- * among methods.
- */
- private static List<Bill> billsCollection = new ArrayList<Bill>( );
- public static void main( String[] args )
- {
- readInput_BuildBills( );
- serializeBills( );
- } // end main
- private static void readInput_BuildBills( )
- {
- /*
- * 1. Set up "holding spaces" for various objects and variables we will use in the loop
- * 2. Declare and instantiate an object for input of the data "flat file" inputBills.txt
- * 3. Read each line from the input file, and using the data provided, instantiate a Bill object
- * 4. Add the new Bill object to the billsCollection (for later serialization)
- * 5. When all input has been read, remember to close the input file!!
- */
- Bill newBill; // This is a "holding space" for the Bill objects we instantiate
- try // Because "stuff" can happen during I/O...
- {
- /*
- * Declare and instantiate a Scanner object to read the input file.
- */
- Scanner inputFile = new Scanner(new File("inputBills.txt"));
- // Modify Scanner object to use a comma (,) as the delimiter between inputs
- inputFile.useDelimiter( "," );
- while( inputFile.hasNext( ) )
- {
- /*
- * Read input items from the file, and use that data to instantiate a new Bill object
- */
- newBill = new Bill( inputFile.next( ),
- inputFile.next( ),
- inputFile.nextDouble( ),
- inputFile.next( ),
- inputFile.next( ),
- inputFile.next( ) );
- // Now that the new Bill object has been instantiated, add it to the (global) collection
- billsCollection.add( newBill );
- } // end while loop
- /*
- * ALWAYS CLOSE your file!
- */
- inputFile.close();
- } // end try block
- catch( Exception xcptn )
- {
- System.err.printf( "There was an error involving the INPUT file. Refer to exception type & message for info.%n%s%n%s%n",
- xcptn.getClass( ).getName( ),
- xcptn.getMessage( ) );
- xcptn.printStackTrace( );
- } // end catch IOException
- } // end readInput_BuildBills
- private static void serializeBills( )
- {
- /*
- * Complete the XML Serialization process using the billsCollection
- *
- * 1. Instantiate a local copy of the "helper" code - this is necessary for the XML serialization
- * to complete properly.
- * 2. Declare and instantiate an output file
- * 3. Serialize the collection
- */
- /*
- * Declare and instantiate a local copy of the "helper" code here
- */
- Bills bills = new Bills();
- try // Because "stuff" can happen during I/O...
- {
- /*
- * Declare and instantiate a BufferedWriter object for output. Use file name "outputBills.xml"
- */
- BufferedWriter output = Files.newBufferedWriter(Paths.get("outputBills.xml"));
- /*
- * Copy the Bill object references from the (globally declared) collection to the collection in the
- * "helper" code. NOTE: This does not duplicate Bill objects, but simply duplicates references to
- * those objects.
- */
- bills.getBills( ).addAll( billsCollection );
- /*
- * Execute the actual serialization process using local copy of the "helper" code instantiated above
- * and the the BufferedWriter object declare/instantiated above
- */
- JAXB.marshal(bills, output);
- } // end try
- catch( IOException xcptn )
- {
- System.err.printf( "There was an error involving the OUTPUT file. Refer to exception type & message for info.%n%s%n%s%n",
- xcptn.getClass( ).getName( ),
- xcptn.getMessage( ) );
- xcptn.printStackTrace( );
- } // end catch IOException
- } // end serializeBills
- } // end CreateBillObjects_Batch
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement