Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that can read XML files for customer accounts receivable, such as Apple County Grocery 1001 1565.99 Uptown Grill 1002 875.20 1002 875.20

Write a program that can read XML files for customer accounts receivable, such as

Apple County Grocery

1001

1565.99

Uptown Grill

1002

875.20

1002

875.20

1002

400.00

1001

99.99

1001

1465.98

Your program should construct an CustomerAccountsParser object, parse the XML data & create new CustomerAccount objects in the CustomerAccountsParser for each of the products the XML data, execute all of the transactions from the file, and print the list of customers & total receivables balance. Note that there can be any number of customer accounts, and any number of purchase and payment transactions. Do not hard-code a specific number of customer accounts or transactions.

Download and use the provided ARParser.java class as the main program for your solution, and download the provided CustomerAccountsParser.java and CustomerAccount.java classes to use with your ARParser solution. Complete the parse() method in the CustomerAccountsParser to finish the program. Download and use the provided customerAccounts.xml to test your program after the transactions, the customer accounts from the provided customerAccounts.xml file should have the results: Customer Accounts:

1001 Apple County Grocery 200.00

1002 Uptown Grill 400.00

1003 Skyway Shop 600.00

Total balance of accounts receivable: 1200.00

(Different numbers of customer accounts and transactions will be used to test your program, so dont assume there are only three customers in the XML data.)

To run the ARParser Java program in Eclipse, create a new project, copy the ARParser.java, CustomerAccountsParser.java, and CustomerAccount.java files into the src folder of your project, and copy the customerAccounts.xml file into the project's folder.

Files:

ARParser.java:

package Homework9;

import java.util.Scanner;

/** This program parses an XML file containing inventory information. It prints out the inventory inforamation after processing the products, purchases and sales that are described in the XML file. */ public class ARParser { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); CustomerAccountsParser inventory = new CustomerAccountsParser(); System.out.print("Enter name of the customer accounts XML file: "); String filename = in.nextLine(); inventory.parse(filename); System.out.println("Customer Accounts: " + inventory.toString()); } }

CustomerAccount.java:

package Homework9;

/** * A simple class for Accounts Receivable: * A CustomerAccount has a customer name, account number, * and account balance. * The account balance can be changed by customer purchases * payments received. */ public class CustomerAccount { private String name; private int accountNumber; private double accountBalance;

/** * Constructs a customer account with a specified * name, account number, and initial balance. * @param newName - customer name * @param newAccountNumber - assigned identifier * @param newAccountBalance - balance on account */ public CustomerAccount(String newName, int newAccountNumber, double newAccountBalance) { name = newName; accountNumber = newAccountNumber; accountBalance = newAccountBalance; } /** * Increases the customer's balance when the * customer makes a purchase. * @param amount - value of purchase to be added * to customer's account */ public void purchase(double amount) { double newAccountBalance = accountBalance + amount; accountBalance = newAccountBalance; } /** * Reduce the customer's balance when a payment * is received from the customer. * @param amount - amount paid on account */ public void payment(double amount) { double newAccountBalance = accountBalance - amount; accountBalance = newAccountBalance; } /** * Gets the name for this customer's account. * @return the customer name */ public String getName() { return name; }

/** * Gets the account number for this customer's account. * @return account number */ public int getAccountNumber() { return accountNumber; }

/** * Gets the balance for this customer's account. * @return the balance */ public double getAccountBalance() { return accountBalance; }

/** * Get a String that describes this customer account * @return info about this account */ public String toString() { return String.format("%d %s %.2f", accountNumber, name, accountBalance); } }

CustomerAccountsParser.java:

package Homework9;

import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner;

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;

/** * This class manages a collection of customer accounts * for an accounts receivable system. * @author */ public class CustomerAccountsParser { private ArrayList customerAccounts; private DocumentBuilder builder; private XPath path;

/** * Initialize the list of customerAccounts. * @throws ParserConfigurationException */ public CustomerAccountsParser() throws ParserConfigurationException { customerAccounts = new ArrayList(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //factory.setValidating(true); factory.setIgnoringElementContentWhitespace(true); builder = factory.newDocumentBuilder(); XPathFactory xpfactory = XPathFactory.newInstance(); path = xpfactory.newXPath(); }

/** * Adds a customer account to our list. * @param c the customer account object to add */ public void addCustomerAccount(CustomerAccount c) { customerAccounts.add(c); }

/** * Gets the sum of the balances of all customerAccounts. * @return the sum of the balances */ public double getTotalBalance() { double total = 0; for (CustomerAccount ca : customerAccounts) { total = total + ca.getAccountBalance(); } return total; }

/** * Finds a customer account with the matching account number. * @param number the account number to find * @return the customer account with the matching number * @throws IllegalArgumentException if there is no match */ public CustomerAccount find(int number) { for (CustomerAccount ca : customerAccounts) { if (ca.getAccountNumber() == number) // Found a match return ca; } // No match in the entire array list throw new IllegalArgumentException("CustomerAccount " + number + " was not found"); } /** * Return a string that describes all the customerAccounts * in the accounts list. */ public String toString() { double totalBalance = 0.0; StringBuffer sb = new StringBuffer(); for (CustomerAccount ca : customerAccounts) { sb.append(ca.toString()); sb.append(' '); totalBalance += ca.getAccountBalance(); } sb.append(String.format("Total balance of accounts receivable: %.2f ", totalBalance)); return sb.toString(); } /** * Parses an XML file containing the inventory of products. * @param fileName the name of the file */ public void parse(String fileName) throws SAXException, IOException, XPathExpressionException { File f = new File(fileName); Document doc = builder.parse(f);

// Count the number of customer accounts, and then get // each account's name, accountNumber, and balance // from the document, create a new CustomerAccount object, // and add the account object to the customer account list using addCustomerAccount(). // Refer back to Homework 4's readCustomerAccounts() method where we created // a CustomerAccount object and added it to the list.

...; // Get and apply the purchased and sold transactions from the // XML document: // Count the account receivable purchase transactions. Then, // for each purchase transaction, get the account number and amount, // find the customer account by its accountNumber, and call the "purchase" method // with the amount. ...;

// Count the account receivable payment transactions. Then, // for each payment transaction, get the account number and amount, // find the customer account by its accountNumber, and call the "payment" method // with the amount. ...; } }

XML Document:

Apple County Grocery 1001 1565.99 Uptown Grill 1002 875.20 Skyway Shop 1003 443.20 1002 875.20 1002 400.00 1003 600.00 1003 443.20 1001 99.99 1001 1465.98

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions