Finish the CustomerAccountTransactions program that reads
customer accounts receivable data from a file and then applies a
series of transactions to the accounts. Its main() method uses the
CustomerAccounts class to manage a collection of CustomerAccount
objects: reading customer accounts data & transactions, and
obtaining a String showing the customer accounts data after the
operations are complete.
You will need to complete the readCustomerAccounts () and
applyTransactions() methods in the CustomerAccounts class.
First, please fill in your name in the @author section of
the comment in the CustomerAccounts.java file.
Next, complete the readCustomerAccounts () method in the CustomerAccounts class to read customer account data (customer name, account number, and account balance) values from a text file, create a CustomerAccount object from the customer name, account number, and account balance, and uses the addCustomerAccount() method to add the new customer account object into its customerAccounts list. A sample customer accounts file contains lines like:
Apple County Grocery
1001
1565.99
Uptown Grill
1002
875.20
Skyway Shop
1003
443.20
Finally, complete the applyTransactions() method in the CustomerAccounts class to read transactions and apply them to the customer accounts. Each line will have a word specifying the action (“purchase” or “payment”), followed by the account number and amount for the transaction. The purchase and payment words are followed by one customer account number and the value of the purchase or payment. A sample transactions file contains lines like:
payment 1002 875.20
purchase 1002 400.00
purchase 1003 600.00
payment 1003 443.20
purchase 1001 99.99
payment 1001 1465.98
After the program has read the customer accounts and applied the transactions, it prints the list of all the customer accounts and balances (this code is already completed for you). Sample output looks like this:
1001 Apple County Grocery 200.00
1002 Uptown Grill 400.00
1003 Skyway Shop 600.00
Total balance of accounts receivable: 1200.00
Test your program using the provided customerAccounts.txt and transactions.txt files, but be aware that we will test your program using different input files with different amounts of data.
We will not use invalid data (such as incorrect customer account numbers in the transactions file) when we test your program.
Files that go with this:
CustomerAccount.java:
/**
* 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);
}
}
CustomerAccounts.java:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/**
* This class manages a collection of customer accounts
* for an accounts receivable system.
* @author
*/
public class CustomerAccounts
{
private ArrayList<CustomerAccount>
customerAccounts;
/**
* Initialize the list of customerAccounts.
*/
public CustomerAccounts()
{
customerAccounts = new
ArrayList<CustomerAccount>();
}
/**
* Adds a customer account to our list.
* @param c the customer account object to add
*/
public void addCustomerAccount(CustomerAccount
c)
{
customerAccounts.add(c);
}
/**
* Load the customerAccounts from the given file.
* @param in - Scanner from which to read customer
account information
*/
public void readCustomerAccounts(Scanner in)
{
/*
* Read the customerAccounts (name,
account number,
* and initial account balance) and
construct a new
* CustomerAccount object from the
information read
* from the file.
* Use the addCustomerAccount()
method to store the
* new customer account in our
accounts list.
*/
// Please DO NOT open
customeraccounts.txt here.
// Use the Scanner in, which
already has opened the
// customer accounts file specified
by the user.
// TODO: Fill in missing
code.
}
/**
* Read and apply the accounts receivable
transactions
* from the given file.
* Transactions may be:
* "purchase <account-number>
<amount>"
* "payment <account-number>
<amount>"
*
* @param in - Scanner from which to read
transactions
*/
public void applyTransactions(Scanner in)
{
/*
* Read the word determining the
kind of transaction. Based on
* the type of transaction, read the
account number
* amount.
* Find the matching account in the
customer accounts
* list (hint: use the find() method
in this class),
* Then use the appropriate method
on the found
* account to apply the
transaction.
*/
// Please DO NOT open
transactions.txt here.
// Use the Scanner in, which
already has opened the
// transactions file specified by
the user.
// TODO: Fill in missing
code.
}
/**
* 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('\n');
totalBalance +=
ca.getAccountBalance();
}
sb.append(String.format("Total
balance of accounts receivable: %.2f\n", totalBalance));
return sb.toString();
}
}
customerAccounts:
Apple County Grocery
1001
1565.99
Uptown Grill
1002
875.20
Skyway Shop
1003
443.20
CustomerAccountTransactions:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* Read the customer accounts and transactions files, and
* show the results after the operations are completed.
* @author
*/
public class CustomerAccountTransactions
{
/**
* Main program.
* @param args
*/
public static void main(String[] args) throws
FileNotFoundException
{
Scanner console = new
Scanner(System.in);
System.out.print("Enter customer
accounts file name: ");
String customerAccountsFilename =
console.nextLine();
System.out.print("Enter
transactions file name: ");
String transactionsFilename =
console.nextLine();
CustomerAccounts accountsReceivable
= new CustomerAccounts();
try (Scanner
customerAccountsScanner = new Scanner(new
File(customerAccountsFilename))) {
accountsReceivable.readCustomerAccounts(customerAccountsScanner);
}
try (Scanner transactionsScanner
= new Scanner(new File(transactionsFilename))) {
accountsReceivable.applyTransactions(transactionsScanner);
}
System.out.println(accountsReceivable.toString());
}
}
transactions:
payment 1002 875.20
purchase 1002 400.00
purchase 1003 600.00
payment 1003 443.20
purchase 1001 99.99
payment 1001 1465.
/** * @fileName :CustomerAccounts.java * @since 15-02-2017 **/ package customeraccount; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * This class manages a collection of customer accounts * for an accounts receivable system. * @author */ public class CustomerAccounts { private ArrayList<CustomerAccount> customerAccounts; /** * Initialize the list of customerAccounts. */ public CustomerAccounts() { customerAccounts = new ArrayList<CustomerAccount>(); } /** * Adds a customer account to our list. * @param c the customer account object to add */ public void addCustomerAccount(CustomerAccount c) { customerAccounts.add(c); } /** * Load the customerAccounts from the given file. * @param in - Scanner from which to read customer account information */ public void readCustomerAccounts(Scanner in) { /* * Read the customerAccounts (name, account number, * and initial account balance) and construct a new * CustomerAccount object from the information read * from the file. * Use the addCustomerAccount() method to store the * new customer account in our accounts list. */ // Please DO NOT open customeraccounts.txt here. // Use the Scanner in, which already has opened the // customer accounts file specified by the user. // TODO: Fill in missing code. String name; int accountNumber; double balance; while (in.hasNext()){ name = in.nextLine(); accountNumber =in.nextInt(); balance = in.nextDouble(); in.nextLine(); CustomerAccount ca = new CustomerAccount(name,accountNumber,balance); addCustomerAccount(ca); } } /** * Read and apply the accounts receivable transactions * from the given file. * Transactions may be: * "purchase <account-number> <amount>" * "payment <account-number> <amount>" * * @param in - Scanner from which to read transactions */ public void applyTransactions(Scanner in) { /* * Read the word determining the kind of transaction. Based on * the type of transaction, read the account number * amount. * Find the matching account in the customer accounts * list (hint: use the find() method in this class), * Then use the appropriate method on the found * account to apply the transaction. */ // Please DO NOT open transactions.txt here. // Use the Scanner in, which already has opened the // transactions file specified by the user. // TODO: Fill in missing code. String type; int accountNumber; double balance; while (in.hasNext()){ type=in.next(); accountNumber=in.nextInt(); balance = in.nextDouble(); CustomerAccount ca = find(accountNumber); if(type.equalsIgnoreCase("payment")){ ca.payment(balance); } else if(type.equalsIgnoreCase("purchase")){ ca.purchase(balance); } } } /** * 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('\n'); totalBalance += ca.getAccountBalance(); } sb.append(String.format("Total balance of accounts receivable: %.2f\n", totalBalance)); return sb.toString(); } }
output:
Get Answers For Free
Most questions answered within 1 hours.