Java Reference
In-Depth Information
return output.toString();
}
private void formatDetails(Statement statement, StringBuilder output) {
output.append(String.format(CASH_DETAIL_FORMAT,
new Object[] { statement.getCustomer().getAccount()
.getCashBalance() }));
for (Transaction transaction : statement.getStocks()) {
output.append(String.format(SECURITY_HOLDING_FORMAT, new Object[] {
transaction.getTicker(), transaction.getQuantity(),
moneyFormatter.format(transaction.getDollarAmount()) }));
}
}
private void formatSummary(Statement statement, StringBuilder output) {
output.append(String.format(SUMMARY_HEADER_FORMAT,
new Object[] { statement.getCustomer().getAccount()
.getAccountNumber() }));
output.append(String.format(
SUMMARY_FORMAT,
new Object[] {
moneyFormatter.format(statement.getSecurityTotal()),
moneyFormatter.format(statement.getCustomer()
.getAccount().getCashBalance()),
moneyFormatter.format(statement.getCustomer()
.getAccount().getCashBalance().doubleValue()
+ statement.getSecurityTotal().doubleValue()) }));
}
private void formatAddress(Statement statement, StringBuilder output) {
Customer customer = statement.getCustomer();
output.append(String.format(ADDRESS_FORMAT,
new Object[] { customer.getFirstName(), customer.getLastName(),
customer.getAddress().getAddress1(),
customer.getAddress().getCity(),
customer.getAddress().getState(),
customer.getAddress().getZip() }));
}
}
Although there is quite a bit of code here, you can quickly see that all it consists of is multiple calls to
String.format for the various types of objects you're working with. When the FlatFileItemWriter calls the
aggregate method on the LineAggregator, it gets back the entire statement, formatted and ready to be
written to your file.
The main part of the statement is formatted, but you still need to write one other small piece of
code. Each statement has a simple header consisting of the name of the statement, the brokerage
address, and the customer assistance phone number. To add that information to the top of each
statement, you use Spring Batch's FlatFileHeaderCallback (see Listing 10-46).
 
Search WWH ::




Custom Search