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); } }