-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomerPolicyReport.java
383 lines (332 loc) · 18.8 KB
/
CustomerPolicyReport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Read in TWO serialized files of objects:
* Customer
* Policy
* Store objects in collections - each file/type in a separate collection
* Sort each collection into ASCENDING ORDER by custNumber
*
* Process match/merge report:
* All Policy objects
* By Customer
*
* (c) 2019, Terri Davis
*/
/******************************************************************************************************************
*****************************************************************************************************************
****************** ADD NECESSARY IMPORT STATEMENTS HERE
*****************************************************************************************************************
******************************************************************************************************************/
/************************************************************************************************************
* IMPORTS FOR I/O MANAGEMENT & DESERIALIZATION
************************************************************************************************************/
/************************************************************************************************************
* IMPORTS FOR COLLECTION MANAGEMENT
************************************************************************************************************/
public class CustomerPolicyReport
{
/******************************************************************************************************************
*****************************************************************************************************************
****************** ADD DECLARATIONS FOR INPUT FILES AND COLLECTIONS TO STORE OBJECTS
*****************************************************************************************************************
******************************************************************************************************************/
/*-----------------------------------------------------------------------------------------------------
GLOBAL DECLARATIONS AND/OR INSTANTIATIONS AND/OR INITIALIZATIONS
-----------------------------------------------------------------------------------------------------*/
// DECLARATION ONLY of BufferedReaders; INSTANTIATION to be completed in openFiles methos
;
// DECLARE ONLY copies of XML SERIALIZATION HELPER code
// DECLARATION & INSTANTIATION of (LOCAL) Collection objects... This is done for ease of use later...
// DECLARE and INITIALIZE variable to hold ACCUMULATED EXPOSURE value
/**************************************************************************************************************
* main method
*************************************************************************************************************/
public static void main( String[] args)
{
/*
* The try block executes the following processes:
* Open input files
* create and output report entries
* close input files
* NOTE: All serialized objects in the XML documents have already undergone data validation and instantiation;
* we will not need to check for CustomerException or PolicyException in this code.
*/
try
{
openFiles();
processReport();
closeFiles();
} // end try
catch( IOException xcptn ) // If there is any error in opening,reading, or closing files
{
System.err.println( "Ending process." );
} // end catch
catch( Exception xcptn ) // Any/all other exceptions passed up to main
{
System.err.printf( "Serious error. Ending proces. %s",
xcptn.getClass( ).getName( ) );
xcptn.printStackTrace( );
}
}// end main
/***************************************************************************************************************
* Open the input files
**************************************************************************************************************/
/*
* Locate the physical file; define it as an input stream; define the input stream as an object stream.
* Prepare to deseralize objects from the file.
*/
public static void openFiles( )
throws IOException
{
/******************************************************************************************************************
*****************************************************************************************************************
****************** ADD CODE TO OPEN INPUT FILES, INCLUDED NECESSARY EXCPETION HANDLING
*****************************************************************************************************************
******************************************************************************************************************/
try
{
/*
* Instantiate one BufferedReader object for each input XML document
*/
} // end try
catch ( IOException xcptn )
{
System.err.println( "Error opening input file." );
xcptn.printStackTrace( );
throw xcptn;
} // end catch
} // end method openFile
/***************************************************************************************************************
* Manage the actual reporting process
**************************************************************************************************************/
/*
* Deserialize Customer file; store objects in collection. Sort collection when file is completely deserialized.
*
* Deserialize Policy file; store objects in collection. Sort collection when file is completely deserialized.
*
* Process match/merge based on Customer.
* For each Customer:
* Output Customer information.
* Output Policy information
* Output CostBasis for Policy
*
*/
public static void processReport( )
throws Exception
{
try
{
prepCustomerCollection( );
prepPolicyCollection( );
reportExposures( );
/*
* Output final total of all Poicy book values
* This can be placed in other locations and still work...
*/
System.out.printf( "%n%n%n%n\t\tTotal Exposure of All Contracts: $%,.2f%n",
totalExposure );
} // end try block
catch( Exception xcptn )
{
System.err.printf( "Serious error. Ending proces. %s",
xcptn.getClass( ).getName( ) );
xcptn.printStackTrace( );
}
} // end method processRecords
/***************************************************************************************************************
* Close the input files
**************************************************************************************************************/
public static void closeFiles( )
throws IOException
{
/******************************************************************************************************************
*****************************************************************************************************************
****************** ADD CODE TO CLOSE INPUT FILES, INCLUDED NECESSARY EXCPETION HANDLING
*****************************************************************************************************************
******************************************************************************************************************/
} // end method closeFiles
/***************************************************************************************************************
* Prepare the Customer collection
**************************************************************************************************************/
/*
* Deserialize all objects from serialized Customer file
* As each object is deserialized (read), add it to the collection holding Customer objects
* Once all Customer objects have been stored in the collection,
* Sort the collection in ascending order, based on custNumber value of each object.
*/
public static void prepCustomerCollection( )
throws ClassNotFoundException,
IOException
{
/*****************************************************************************************************************
*****************************************************************************************************************
****************** ADD CODE TO:
****************** DESERIALIZE CUSTOMER OBJECTS
****************** ADD OBJECTS TO COLLECTION
****************** HANDLE NECESSARY EXCEPTIONS
****************** SORT COMPLETED COLLECTION INTO ASCENDING ORDER BY custNumber
*****************************************************************************************************************
*****************************************************************************************************************/
/*
* Instantiate globally declared copies of HELPER code, using JAXB.unmarshal method
*/
/*
* Copy DESERIALIZED Object Collection to local Collection
*/
/*
* Sort (local) Collection object into ASCENDING ORDER, based on custId values
*/
} // end prepCustomerCollection
/***************************************************************************************************************
* Prepare the Policy collection
**************************************************************************************************************/
/*
* Deserialize all objects from serialized Policy file
* As each object is deserialized (read), add it to the collection holding Policy objects
* Once all Policy objects have been stored in the collection,
* Sort the collection in ascending order, based on custNumber value of each object.
*/
public static void prepPolicyCollection( )
throws ClassNotFoundException,
IOException
{
/*****************************************************************************************************************
*****************************************************************************************************************
****************** ADD CODE TO:
****************** DESERIALIZE SECURITY OBJECTS
****************** ADD OBJECTS TO COLLECTION
****************** HANDLE NECESSARY EXCEPTIONS
****************** SORT COMPLETED COLLECTION INTO ASCENDING ORDER BY custNumber
*****************************************************************************************************************
*****************************************************************************************************************/
/*
* Instantiate globally declared copies of HELPER code, using JAXB.unmarshal method
*/
/*
* Copy DESERIALIZED Object Collection to local Collection
*/
/*
* Sort (local) Collection object into ASCENDING ORDER, based on owner values
*/
} // end prepPolicyCollection
/***************************************************************************************************************
* Report Book Values
**************************************************************************************************************/
/*
* Once both input files have been deserialized, both collections built, and both sorted,
* Use a matching algorithm to report contract information for all Customer objects
*/
public static void reportExposures( )
{
/*
* Declare an object to use to move through the collection of Policy objects
* Using that object, pick up the 'next' Policy object available, assign it the name contract
* Retrieve the owner value from that object and store it in a variable named 'holdOwner'
*/
/*****************************************************************************************************************
*****************************************************************************************************************
****************** ADD THE FOLLOWING CODE =ABOVE= THE CALL TO findCustomer:
****************** Declare an iterator for use with the Policy collection
****************** Use that iterator to retrieve a Policy object; name the object retrieved "contract"
****************** Retrieve the owner of the contract object; store it as "holdOwner"
*****************************************************************************************************************
*****************************************************************************************************************/
/*
* Declare an object to use to move through the collection of Policy objects
* Using that object, pick up the 'next' Policy object available, assign it the name contract
* Retrieve the custNumber value from that object and store it in a variable named 'holdOwner'
*/
findCustomer( holdOwner ); // Pass the owner value retrieved from the Policy object
try
{
while( true ) // This is effectively an infinite loop that will end when an
{ // exception is thrown to indicate we've "run out" of Policy
// objects in the localPolicies collection
while( contract.getPolOwner( ).matches( holdOwner ) ) // While we have more Policy objects for this Customer
{
double exposure = contract.calcExposure( ); // We'll need this in a couple of places in the loop
// Output a line describing this Policy
System.out.printf( "\t%-10s %-5s carries an exposure of %15s%n",
contract.getClass( ).getName( ),
contract.getPolNumber( ),
String.format( "$%,.2f", exposure ) );
// Add the current book value of this Policy to the total assets managed
totalExposure = totalExposure + exposure;
// Move to the next Policy
contract = policyIterator.next( );
} // end INNER while (we've found all Policy objects belonging to this Customer)
// This is just an indication (or "flag" ) that we're done with this Customer's Policy objects
System.out.printf( "=========================================================================%n%n" );
holdOwner = contract.getPolOwner( );
findCustomer( holdOwner );
} // end OUTER while (we've run out of Policy objects to process)
} // end try block
catch( NoSuchElementException endOfCollection )
{
/*
* This Exception is EXPECTED to be thrown when we "run out" of Policy objects; it ends the outer while loop
*/
System.out.printf( "%n%n\t\t\t ***** END OF POLICY REPORT *****%n" );
} // end catch NoSuchElementException
} // end reportExposures
/***************************************************************************************************************
* Find Customer
**************************************************************************************************************/
/*
* Using the target custNumber passed into the method from the calling code...
* Search the sorted Customer objects for the one matching the provided target object
* Assign the located Customer object to the reference found
* Using the Customer reference found, produce the report header
*/
public static void findCustomer( String findCust )
{
try
{
/*
* Set up for Customer search.
* The "important" piece is the custNumber -> findCust.All other information is essentially filler and
* will not be used in the search process.
*/
/******************************************************************************************************************
*****************************************************************************************************************
****************** ADD CODE TO SEARCH CUSTOMER COLLECTION FOR DESIRED CUSTOMER OBJECT,
****************** AND TO THEN RETRIEVE THE LOCATED OBJECT. NAME THE RETRIEVED OBJECT "found"
*****************************************************************************************************************
******************************************************************************************************************/
/*
* Call binarySearch (API) method. Method returns an integer representing the location in the
* Collection of the target object.
* If the return value is > -1, the object was found; -1 would indicate the object does not exist
* in the Collection
*/
/*
* Test the return from binarySearch. The process will actually work without this, but it's
* good practice (and solid coding)...
*/
if( ............ )
{ // We found the Customer we were looking for....
found = localCustomers.get( foundIt );
// Print the first report break header
System.out.printf( "%n%-20s %-20s Customer %s, TID %s%n",
found.getCustFirstName( ),
found.getCustLastName( ),
found.getCustId( ),
found.getCustTaxId( ) );
}
else // This won't actually happen in our case....
{ // The Customer we want does not exist within the Collection
// Print the Customer identification line
System.out.printf( "%-20s %-20s Customer %s, TIN %s%n",
"Missing",
"Customer",
"XXXXXXX",
"XXXXXXXXX" );
} // end negative branch
}
catch( CustomerException xcptn )
{
System.err.printf( "Error building target Customer object.%n" );
System.err.printf( "\t\t%s%n",
xcptn.getMessage( ) );
} // end catch
} // end findCustomer
} // end class CustomerPolicyReport