Question

Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies...

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.

Homework Answers

Answer #1
/**
 * @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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
IN JAVA PLEASE Design and implement an ADT CreditCard that represents a credit card. The data...
IN JAVA PLEASE Design and implement an ADT CreditCard that represents a credit card. The data of the ADT should include Java variables for the customer name, the account number, the next due date, the reward points, and the account balance. The initialization operation should set the data to client-supplied values. Include operations for a credit card charge, a cash advance, a payment, the addition of interest to the balance, and the display of the statistics of the account. Be...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list code and can't figure out how to complete it: Below is the code Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are already implemented in the ShoppingListArray class. /////////////////////////////////////////////////////////////////////////////////////////////////////////// Grocery Class (If this helps) package Shopping; public class Grocery implements Comparable<Grocery> { private String name; private String category; private int aisle; private float price; private int quantity;...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT