Los Ficheros de Aplicación
import java.io.*;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.marshal.*;
public class CheckbookApp {
public static Transactions marchTrans = new Transactions();
public static Transactions aprilTrans = new Transactions();
public static CheckbookBalance chBook = new CheckbookBalance();
public static void main(String[] args) throws Exception{
// Build the content trees
buildTrees();
// Access content of trees
accessContent();
// Validate the trees
validateTrees();
// Marshal the trees
marshalTrees();
// Append the april transactions to the march transactions
appendTrees();
// Unmarshal the checkbook subclass
unmarshalSubclass();
// Add the transactions to the checkbook and update the balance
chBook.balanceCheckbook(marchTrans);
// Validate the updated checkbook and marshal it
validateAndMarshalCheckbook();
}
// Building the content trees
public static void buildTrees() throws Exception{
// Unmarshall the march.xml file
File march = new File(“march.xml”);
FileInputStream fIn = new FileInputStream(march);
try {
marchTrans = marchTrans.unmarshal(fIn);
}
finally {
fIn.close();
}
// Instantiate a content tree for the April transactions
List aprilEntries = aprilTrans.getEntries();
Check aprilRentCheck = new Check();
CheckCategory aprilRent = CheckCategory.RENT;
aprilRentCheck.setCategory(aprilRent);
aprilRentCheck.setName(“Me”);
aprilRentCheck.setNumber(51);
aprilRentCheck.setDate(TransDate.parseDate(“04-12-2001”));
aprilRentCheck.setAmount(new java.math.BigDecimal(“1500.00”));
Pending pending = new Pending();
aprilRentCheck.setPendVoidClrd(pending);
aprilEntries.add(aprilRentCheck);
}
// Access content of trees
public static void accessContent() {
// Edit the name on the groceries check in the Trans contenttree
List entryList = marchTrans.getEntries();
Entry entry;
for (ListIterator i = entryList.listIterator(); i.hasNext(); ) {
entry = (Entry)i.next();
if( entry instanceof Check ) {
CheckCategory category = ((Check)entry).getCategory();
if (category.equals(CheckCategory.GROCERIES)) {
((Check)entry).setName(“Mom & Pop Foods”);
break;
}
}
}
// Edit the rent check in the aprilTrans content tree
List aprilEntries = aprilTrans.getEntries();
for (ListIterator i = aprilEntries.listIterator(); i.hasNext(); ) {
entry = (Entry)i.next();
if ( entry instanceof Check ) {
CheckCategory category = ((Check)entry).getCategory();
if (category.equals(CheckCategory.RENT)) {
entry.setAmount(new java.math.BigDecimal(“2000.00”));
break;
}
}
}
}
// Validate the trees
public static void validateTrees() throws Exception{
// Validate the two content trees
marchTrans.validate();
aprilTrans.validate();
}
// Marshal the trees
public static void marshalTrees() throws Exception {
// Create output files for the two content trees
File march_new = new File(“march_new.xml”);
File april_new = new File(“april_new.xml”);
FileOutputStream fMOut = new FileOutputStream(march_new);
FileOutputStream fAOut = new FileOutputStream(april_new);
// Marshal the two content trees to new XML documents
try {
marchTrans.marshal(fMOut);
aprilTrans.marshal(fAOut);
}
finally {
fAOut.close();
}
}
// Append the april transactions to the march transactions
public static void appendTrees() {
// Append the aprilTrans content tree to the Trans content tree
List mEntries = marchTrans.getEntries();
List aEntries = aprilTrans.getEntries();
mEntries.addAll(aEntries);
}
// Unmarshal the checkbook subclass
public static void unmarshalSubclass() throws Exception{
// Register the subclass of Checkbook with a dispatcher
Dispatcher d = Checkbook.newDispatcher();
d.register(Checkbook.class, CheckbookBalance.class);
// Unmarshal the checkbook.xml file
File checkbookNew = new File(“checkbook.xml”);
FileInputStream fNewIn = new FileInputStream(checkbookNew);
// Unmarshal the checkbook file to a CheckbookBalance
try {
chBook = (CheckbookBalance) (d.unmarshal(fNewIn));
}
finally {
fNewIn.close();
}
}
// Validate the updated checkbook and marshal it
public static void validateAndMarshalCheckbook() throws Exception{
chBook.validate();
// Create an output file for the updated checkbook
File checkbook_new = new File(“checkbook_new.xml”);
FileOutputStream fCOut = new FileOutputStream(checkbook_new);
// Marshal the updated checkbook
try {
chBook.marshal(fCOut);
}
finally {
fCOut.close();
}
}
}
import java.util.*;
import java.io.*;
import java.math.*;
public class CheckbookBalance extends Checkbook {
void balanceCheckbook(Transactions trans) throws Exception {
// Get the current balance of the checkbook
BigDecimal balance = this.getBalance();
// Get the list of transactions from the Trans object
List tEntries = trans.getEntries();
// Initialize a BigDecimal to track the amount of each transaction BigDecimal amt;
// Iterate through the transaction list, recalculate the balance,
// and add the transaction to the checkbook
for (ListIterator i = tEntries.listIterator(); i.hasNext(); ) {
Entry entry = (Entry)i.next();
amt = entry.getAmount();
if (entry instanceof Deposit){
balance = balance.add(amt);
}
else {
balance = balance.subtract(amt);
}
this.getTransactions().getEntries().add(entry);
}
// Check if the balance is negative.
if(balance.compareTo(new BigDecimal(0.00)) == -1){
System.out.println(“You are overdrawn.”);
}
// Output the new balance
System.out.println(“Your balance is: “+balance);
// Update the balance in the checkbook.
this.setBalance(balance);
}
}