Finish the CustomerAccountTransactions program that reads cu

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(\'\ \');
           totalBalance += ca.getAccountBalance();
       }
       sb.append(String.format(\"Total balance of accounts receivable: %.2f\ \", 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.

Solution

//Code added to class is in bold letters
//CustomerAccountTransactions.java
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());
   }
}

------------------------------------------------------------------------------------------------------------


//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.
       */

       /*
       * The while in scanner object that continues until
       * in has nothing left to read from file.
       *
       * */
       while(in.hasNextLine())
       {
           //read name of account holder
           String newName=in.nextLine();
           //read account as string and convert to integer type
           int newAccountNumber=Integer.parseInt(in.nextLine());
           //read account balance as string and convert to doube
           double newAccountBalance=Double.parseDouble(in.nextLine());
           /*create an instance of CustomerAccount class with name,account
           and balance */
           CustomerAccount caccount
           =new CustomerAccount(newName, newAccountNumber, newAccountBalance);
           //call method addCustomerAccount to add caccount object
           addCustomerAccount(caccount);
       }
   }

   /**
   * 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.
       */


       /*
       * The while continuly reads until the input
       * scanner in has left to read.
       * */
       while(in.hasNextLine())
       {
           //read type of transaction type as string
           String type=in.next();
           //read account number
           int acNumber=Integer.parseInt(in.next());
           //read amount of transaction
           double amount=Double.parseDouble(in.next());

           //check type is payment
           if(type.equalsIgnoreCase(\"payment\"))
           {
               /*calling find method that returns
               object of CustomerAccount*/
               CustomerAccount user=find(acNumber);
               //call payment method that takes amount
               user.payment(amount);
           }
           else if(type.equalsIgnoreCase(\"purchase\"))
           {
               /*calling find method that returns
               object of CustomerAccount*/
               CustomerAccount user=find(acNumber);
               //call purchase method that takes amount
               user.purchase(amount);
           }

       }
   }

   /**
   * 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();
   }
}

------------------------------------------------------------------------------------------------------------

/**
* 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.
*/
//CustomerAccount.java
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);
   }
}

------------------------------------------------------------------------------------------------------------

Sample output:

Enter customer accounts file name: CustomerAccountTransactions.txt
Enter transactions file name: transactions.txt
1001 Apple County Grocery 200.98
1002 Uptown Grill 400.00
1003 Skyway Shop 600.00
Total balance of accounts receivable: 1200.98


Note : Input files CustomerAccountTransactions.txt and transactions.txt must be placed before running the program.

Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the account
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the account
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the account
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the account
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the account
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the account
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the account
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the account
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the account
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the account
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the account

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site