Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that can read XML files, such as <bank> <account> <number>3</num

ID: 3569014 • Letter: W

Question

Write a program that can read XML files, such as

<bank>

   <account>

      <number>3</number>

      <balance>1295.32</balance>

   </account>

   <account>

      <number>4</number>

      <balance>25.00</balance>

   </account>

   <deposit>

      <accountNumber>3</accountNumber>

      <amount>100.00</amount>

   </deposit>

   <withdraw>

      <accountNumber>3</accountNumber>

      <amount>25.00</amount>

   </withdraw>

   <transfer>

      <fromAccountNumber>3</fromAccountNumber>

      <toAccountNumber>4</toAccountNumber>

      <amount>250.00</amount>

   </transfer>

</bank>

Your program should construct a Bank object, parse the XML data & create new BankAccount objects in the Bank from the XML data, execute the transactions from the file, and print the list of accounts & total value of the balances.

Download and use the provided BankParserDemo.java class as the main program for your solution, and download the provided Bank.java and BankAccount.java classes to use with your BankParsersolution. Download the provided BankParser.java class 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: 1400.00

1002: 1500.00

1003: 3100.00

and the total balance should be 6000.00. (Different XML data will be used to test your program, so dont assume there are only three accounts in the XML data.)

To run the BankParserDemo Java program in Eclipse, create a new project, copy the BankParserDemo.java, BankParser.java, Bank.java, and BankAccount.java files into the src folder of your project, and copy the Bank.xml file into the project's folder.  Open the Projects Properties dialog and select Run/Debug Settings, then click the Arguments tab and enter "bank.xml" in the Arguments field. Click OK to save these changes to the Projects properties

Bank.java:

bank.xml:

<!-- <!DOCTYPE bank SYSTEM "bank.dtd"> -->

<bank>

<account>

<number>1001</number>

<balance>1000.00</balance>

</account>

<account>

<number>1002</number>

<balance>2000.00</balance>

</account>

<account>

<number>1003</number>

<balance>3000.00</balance>

</account>

<deposit>

<accountNumber>1003</accountNumber>

<amount>100.00</amount>

</deposit>

<withdraw>

<accountNumber>1001</accountNumber>

<amount>100.00</amount>

</withdraw>

<transfer>

<fromAccountNumber>1002</fromAccountNumber>

<toAccountNumber>1001</toAccountNumber>

<amount>500.00</amount>

</transfer>

</bank>

BankAccount.java:

BankParser.java:

BankParserDemo.java:

Explanation / Answer

Here is the completed BankParser.java file with implementation for parse() method. Output is shown below. Please use the other files Bank.java, BankAccount.java and BankParserDemo.java as is from your question. You will need to pass in the filename as command line argument. Please post a comment in case of any issue/ doubt , I shall respond. If the answer helped, please don't forget to rate it. Thank you very much.

BankParser.java

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.XPathConstants;

import javax.xml.xpath.XPathExpressionException;

import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

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 the accounts and add them to the bank

final String accountExpression = "/bank/account";

  

final NodeList accountNodeList = (NodeList) path.compile(accountExpression).evaluate(doc, XPathConstants.NODESET);

if(accountNodeList != null) {

      for(int i = 0; i < accountNodeList.getLength(); i++) {

          

          final Element accountElement = (Element) accountNodeList.item(i);

          if(accountElement != null) {

              try {

                  int accountNumber = Integer.parseInt( path.evaluate("number", accountElement));

          double initialBalance = Double.parseDouble( path.evaluate("balance", accountElement));

          

          final BankAccount bankAccount = new BankAccount(accountNumber, initialBalance);        

          b.addAccount(bankAccount);

          

               } catch (NumberFormatException numFormatEx) {

                   // Ignore if the Account number or the balance are NOT in the right format

               }

              

          }

      }

}

// get and apply the transactions: deposit, withdraw, and transfer

  

//Deposit

final String depositExpression = "/bank/*[self::deposit or self::withdraw or self::transfer]";

  

final NodeList depositNodeList = (NodeList) path.compile(depositExpression).evaluate(doc, XPathConstants.NODESET);

if(depositNodeList != null) {

      for(int i = 0; i < depositNodeList.getLength(); i++) {

          

          final Element accountElement = (Element) depositNodeList.item(i);

          if(accountElement != null) {

              try {

                  if(accountElement.getTagName().equals("transfer")){

                      int fromAccountNumber = Integer.parseInt( path.evaluate("fromAccountNumber", accountElement));

                  int toAccountNumber = Integer.parseInt( path.evaluate("toAccountNumber", accountElement));

          double amount = Double.parseDouble( path.evaluate("amount", accountElement));

          

          final BankAccount fromAccount = b.find(fromAccountNumber);

          final BankAccount toAccount = b.find(toAccountNumber);

          

          if(fromAccount != null && toAccount != null) {

              fromAccount.withdraw(amount);

              toAccount.deposit(amount);

          }        

                  }

                  else

                  {

                        int accountNumber = Integer.parseInt( path.evaluate("accountNumber", accountElement));

                double amount = Double.parseDouble( path.evaluate("amount", accountElement));

          

                final BankAccount account = b.find(accountNumber);

                if(account != null) {

                    if(accountElement.getTagName().equals("deposit"))

                        account.deposit(amount);

                    else if(accountElement.getTagName().equals("withdraw"))

                        account.withdraw(amount);

                }

                  }

          

          

               } catch (NumberFormatException numFormatEx) {

                   // Ignore if the Account number or the amount are NOT in the right format

               }

              

          }

      }

}

return b;

   }

}

output

Accounts:

1001: 1400.00

1002: 1500.00

1003: 3100.00

Total balance: 6000.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote