Question
Write a program that can read XML files, such as 3 1295.32 4 25.00 3 100.00 3 25.00 3 4 250.00 Your program should construct
Write a program that can read XML files, such as
Your program should construct a Bank object, parse the XML data & create new BankAccount objects in the Bank for each of the accounts the XML data, execute all of the transactions from the file, and print the list of accounts & total value of the balances. Note that there can be any number of accounts, and any number of deposit, withdraw, and transfer transactions. Do not hard-code a specific number of accounts or transactions.
Download and use the provided BankParserDemo.java class as the main program for your solution, and download the providedBank.java and BankAccount.java classes to use with your BankParser solution. Download the provided BankParser.javaclass and complete the parse() method to finish the program. Download and use the provided bank.xml to test your program after the transactions, the accounts from the provided bank.xml files should have the balances: 1001: 0.00
1002: 1000.00
1003: 2000.00
1004: 3000.00
1005: 4000.00
and the total balance should be 10000.00. (Different XML data will be used to test your program, so dont assume there are only five accounts in the XML data.)
Bank
import java.util.ArrayList;
/** This bank contains a collection of bank accounts. */ public class Bank { private ArrayList
/** Constructs a bank with no bank accounts. */ public Bank() { accounts = new ArrayList
/** Adds an account to this bank. @param a the account to add */ public void addAccount(BankAccount a) { accounts.add(a); } /** Gets the sum of the balances of all accounts in this bank. @return the sum of the balances */ public double getTotalBalance() { double total = 0; for (BankAccount a : accounts) { total = total + a.getBalance(); } return total; }
/** Counts the number of bank accounts whose balance is at least a given value. @param atLeast the balance required to count an account @return the number of accounts having least the given balance */ public int count(double atLeast) { int matches = 0; for (BankAccount a : accounts) { if (a.getBalance() >= atLeast) matches++; // Found a match } return matches; }
/** Finds a bank account with a given number. @param accountNumber the number to find @return the account with the given number, or null if there is no such account */ public BankAccount find(int accountNumber) { for (BankAccount a : accounts) { if (a.getAccountNumber() == accountNumber) // Found a match return a; } return null; // No match in the entire array list }
/** Gets the bank account with the largest balance. @return the account with the largest balance, or null if the bank has no accounts */ public BankAccount getMaximum() { if (accounts.size() == 0) return null; BankAccount largestYet = accounts.get(0); for (int i = 1; i < accounts.size(); i++) { BankAccount a = accounts.get(i); if (a.getBalance() > largestYet.getBalance()) largestYet = a; } return largestYet; } /** * Return a string representation of the accounts in the bank. * @return String describing the accounts. */ public String toString() { String result = ""; for (BankAccount a : accounts) { result += String.format("%d: %8.2f ", a.getAccountNumber(), a.getBalance()); } return result; } }
Bank Account
/** A bank account has a balance that can be changed by deposits and withdrawals. */ public class BankAccount { private int accountNumber; private double balance;
/** Constructs a bank account with a zero balance @param anAccountNumber the account number for this account */ public BankAccount(int anAccountNumber) { accountNumber = anAccountNumber; balance = 0; }
/** Constructs a bank account with a given balance @param anAccountNumber the account number for this account @param initialBalance the initial balance */ public BankAccount(int anAccountNumber, double initialBalance) { accountNumber = anAccountNumber; balance = initialBalance; }
/** Gets the account number of this bank account. @return the account number */ public int getAccountNumber() { return accountNumber; }
/** Deposits money into the bank account. @param amount the amount to deposit */ public void deposit(double amount) { double newBalance = balance + amount; balance = newBalance; }
/** Withdraws money from the bank account. @param amount the amount to withdraw */ public void withdraw(double amount) { double newBalance = balance - amount; balance = newBalance; }
/** Transfer money from this bank account to the other bank account. @param amount - the amount to transfer @param other - the account to transfer to */ public void transfer(double amount, BankAccount other) { this.balance = this.balance - amount; other.balance = other.balance + amount; }
/** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { return balance; } }
Bank Parser
import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.xml.sax.SAXException;
/** An XML parser for a bank. */ public class BankParser { private DocumentBuilder builder; private XPath path;
/** Constructs a parser that can parse a bank. */ public BankParser() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //factory.setValidating(true); factory.setIgnoringElementContentWhitespace(true); builder = factory.newDocumentBuilder(); XPathFactory xpfactory = XPathFactory.newInstance(); path = xpfactory.newXPath(); }
/** Parses an XML file containing a bank. @param fileName the name of the file @return a Bank object containing all coins in the XML file */ public Bank parse(String fileName) throws SAXException, IOException, XPathExpressionException { File f = new File(fileName); Document doc = builder.parse(f);
// get the bank Bank b = new Bank(); // Get all of the accounts from the document and add each of them // to the Bank b.
...
// Get and apply each type of transactions from the document: // deposit, withdraw, and transfer
...
return b;
} }
Parser Demo
/** This program parses an XML file containing an account list. It prints out the total balance of the bank accounts that are described in the XML file. */ public class BankParserDemo { public static void main(String[] args) throws Exception { BankParser parser = new BankParser(); Bank b = parser.parse(args[0]); System.out.println("Accounts: " + b.toString()); System.out.println("Total balance: " + b.getTotalBalance()); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started