Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. /*
  2. * Lab 11
  3. *
  4. * This code will read in a "flat" file containing data items for use in instantiating Bill objects.
  5. * A variety of Bill objects will be instantiated based on the input values.
  6. * Those objects will be added to a (globally declared) generic collection for holding/serialization
  7. * Once all objects have been instantiated, the (globally declared) collection will be output
  8. * as an XML-serialized document.
  9. *
  10. * (c) 2019, Terri Davis
  11. */
  12. // ---------------------- IMPORTS for File Handling/Management ------------------------------
  13.  
  14.  
  15. // -------------------- IMPORTS for Collection Handling/Management ---------------------------
  16. import java.util.List; // Interface provides methods for manipulating objects by index
  17. import java.util.ArrayList; // Class implements interfaces List & Collection
  18.  
  19. // --------------------------- IMPORT for XML Serialization ---------------------------------
  20. import java.io.BufferedWriter;
  21. import java.io.IOException;
  22. import java.nio.file.Files;
  23. import java.io.File;
  24. import java.nio.file.Paths;
  25. import java.util.NoSuchElementException;
  26. import java.util.Scanner;
  27. import javax.xml.bind.JAXB;
  28.  
  29.  
  30.  
  31. public class CreateBillObjects_Batch
  32. {
  33. /*
  34. * Declared globally, as it will be needed in both sub-methods. This avoids having to pass the reference
  35. * among methods.
  36. */
  37. private static List<Bill> billsCollection = new ArrayList<Bill>( );
  38.  
  39. public static void main( String[] args )
  40. {
  41. readInput_BuildBills( );
  42. serializeBills( );
  43. } // end main
  44.  
  45. private static void readInput_BuildBills( )
  46. {
  47. /*
  48. * 1. Set up "holding spaces" for various objects and variables we will use in the loop
  49. * 2. Declare and instantiate an object for input of the data "flat file" inputBills.txt
  50. * 3. Read each line from the input file, and using the data provided, instantiate a Bill object
  51. * 4. Add the new Bill object to the billsCollection (for later serialization)
  52. * 5. When all input has been read, remember to close the input file!!
  53. */
  54.  
  55. Bill newBill; // This is a "holding space" for the Bill objects we instantiate
  56.  
  57. try // Because "stuff" can happen during I/O...
  58. {
  59. /*
  60. * Declare and instantiate a Scanner object to read the input file.
  61. */
  62. Scanner inputFile = new Scanner(new File("inputBills.txt"));
  63.  
  64. // Modify Scanner object to use a comma (,) as the delimiter between inputs
  65. inputFile.useDelimiter( "," );
  66.  
  67. while( inputFile.hasNext( ) )
  68. {
  69. /*
  70. * Read input items from the file, and use that data to instantiate a new Bill object
  71. */
  72. newBill = new Bill( inputFile.next( ),
  73. inputFile.next( ),
  74. inputFile.nextDouble( ),
  75. inputFile.next( ),
  76. inputFile.next( ),
  77. inputFile.next( ) );
  78.  
  79. // Now that the new Bill object has been instantiated, add it to the (global) collection
  80. billsCollection.add( newBill );
  81.  
  82. } // end while loop
  83.  
  84. /*
  85. * ALWAYS CLOSE your file!
  86. */
  87. inputFile.close();
  88.  
  89. } // end try block
  90. catch( Exception xcptn )
  91. {
  92. System.err.printf( "There was an error involving the INPUT file. Refer to exception type & message for info.%n%s%n%s%n",
  93. xcptn.getClass( ).getName( ),
  94. xcptn.getMessage( ) );
  95. xcptn.printStackTrace( );
  96. } // end catch IOException
  97.  
  98. } // end readInput_BuildBills
  99.  
  100. private static void serializeBills( )
  101. {
  102. /*
  103. * Complete the XML Serialization process using the billsCollection
  104. *
  105. * 1. Instantiate a local copy of the "helper" code - this is necessary for the XML serialization
  106. * to complete properly.
  107. * 2. Declare and instantiate an output file
  108. * 3. Serialize the collection
  109. */
  110.  
  111. /*
  112. * Declare and instantiate a local copy of the "helper" code here
  113. */
  114. Bills bills = new Bills();
  115.  
  116.  
  117. try // Because "stuff" can happen during I/O...
  118. {
  119. /*
  120. * Declare and instantiate a BufferedWriter object for output. Use file name "outputBills.xml"
  121. */
  122. BufferedWriter output = Files.newBufferedWriter(Paths.get("outputBills.xml"));
  123.  
  124. /*
  125. * Copy the Bill object references from the (globally declared) collection to the collection in the
  126. * "helper" code. NOTE: This does not duplicate Bill objects, but simply duplicates references to
  127. * those objects.
  128. */
  129.  
  130. bills.getBills( ).addAll( billsCollection );
  131.  
  132. /*
  133. * Execute the actual serialization process using local copy of the "helper" code instantiated above
  134. * and the the BufferedWriter object declare/instantiated above
  135. */
  136.  
  137. JAXB.marshal(bills, output);
  138.  
  139. } // end try
  140. catch( IOException xcptn )
  141. {
  142. System.err.printf( "There was an error involving the OUTPUT file. Refer to exception type & message for info.%n%s%n%s%n",
  143. xcptn.getClass( ).getName( ),
  144. xcptn.getMessage( ) );
  145. xcptn.printStackTrace( );
  146. } // end catch IOException
  147.  
  148. } // end serializeBills
  149. } // end CreateBillObjects_Batch
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement