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

Project A(to be modified) Account Class import java.text.DateFormat; import java

ID: 671449 • Letter: P

Question

Project A(to be modified)

Account Class

import java.text.DateFormat;
import java.util.Date;

public class Account {
private int accountNumber;
Date dateOpened;
private Customer owner;
private double currentBalance;

protected Account(int accountNumber, Customer owner, double currentBalance) {
setAccountNumber(accountNumber);
setDateOpened(new Date());
setOwner(owner);
setCurrentBalance(currentBalance);
}

public final int getAccountNumber() { return accountNumber; }
public final Date getDateOpened() { return dateOpened; }
public final Customer getOwner() { return owner; }
public final double getCurrentBalance() { return currentBalance; }
  
private void setAccountNumber(int accountNumber) { this.accountNumber = accountNumber; }
private void setDateOpened(Date dateOpened) { this.dateOpened = dateOpened; }
private void setOwner(Customer owner) { this.owner = owner; }
private void setCurrentBalance(double currentBalance) { this.currentBalance = currentBalance; }
  
public double withdraw(double amount) {
currentBalance -= amount;
return currentBalance;
}
  
public double deposit(double amount) {
currentBalance += amount;
return currentBalance;
}
  
@Override
public String toString() {
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);

return "[" + getAccountNumber() + "]" +
", Opened: " + df.format(getDateOpened()) +
", " + owner.toString() +
" Balance: " + String.format("$%.2f", getCurrentBalance());
}
}

Bank

import java.util.ArrayList;

public class Bank {
private final ArrayList customers;
private final ArrayList accounts;
  
public Bank() {
customers = new ArrayList();
accounts = new ArrayList();
}
  
public Customer getCustomerAtIndex(int index) { return customers.get(index); }
public Account getAccountAtIndex(int index) { return accounts.get(index); }
  
public void addCustomer(Customer o) {
customers.add(o);
}
  
public void openAccount(Account o) {
accounts.add(o);
customers.add(o.getOwner());
}
  
public String customerList() {
String list = "";
for (int i = 0; i <= customers.size() - 1; i++) {
list += customers.get(i) + " ";
}
return list;
}
  
public String accountList() {
String list = "";
for (int i = 0; i <= accounts.size() - 1; i++) {
list += accounts.get(i) + " ";
}
return list;
}
  
@Override
public String toString() {
return "Customers(" + customers.size() + ") " +
customerList() + " " +
"Accounts(" + accounts.size() + ") " +
accountList();
}
}

Checking Account Class

public class CheckingAccount extends Account {
private boolean freeChecks;
  
public CheckingAccount(boolean freeChecks, int accountNumber,
Customer owner, double currentBalance) {
  
super(accountNumber, owner, currentBalance);
setFreeChecks(freeChecks);
}
  
public boolean getFreeChecks() { return freeChecks; }
public final void setFreeChecks(boolean freeChecks) { this.freeChecks = freeChecks; }
  
@Override
public String toString() {
return super.toString() + ", " +
"Free checks: " + (getFreeChecks()? "Yes" : "No");
}
}

Customer Class

public class Customer {
private String first;
private String last;

public Customer(String first, String last) {
setFirst(first);
setLast(last);
}

public final String getFirst() { return first; }
public final String getLast() { return last; }
public final String getName() { return getLast() + ", " + getFirst(); }
  
public final void setFirst(String first) { this.first = first; }
public final void setLast(String last) { this.last = last; }

@Override
public String toString() {
return "Owner: " + getName();
}
}

Main Class

public class Main {

public static void main(String[] args) {
/* Create a Bank object to hold some customers and accounts. */
Bank bank = new Bank();
  
bank.openAccount(new CheckingAccount(false, 10100, new Customer("Adam", "Apple"), 500.0));
bank.openAccount(new CheckingAccount(true, 10101, new Customer("Beatrice", "Bagel"), 2000.0));
bank.openAccount(new SavingsAccount(0.02, 20100, new Customer("Chris", "Cucumber"), 5000.0));
  
System.out.printf("%s %s %s", "Customer list:","--------------", bank.customerList());
System.out.println("");
System.out.printf("%s %s %s", "Account list:","-------------", bank.accountList());
System.out.printf(" %s %s %s", "Bank record:","------------", bank);
System.out.println("");
}

}

Savings Account class

public class SavingsAccount extends Account {
private double interestRate;
  
public SavingsAccount(double interestRate, int accountNumber,
Customer owner, double currentBalance) {
super(accountNumber, owner, currentBalance);
setInterestRate(interestRate);
}

public double getInterestRate() { return interestRate; }

public final void setInterestRate(double interestRate) { this.interestRate = interestRate; }

@Override
public String toString() {
return super.toString() +
", Interest rate: " + String.format("%.2f%s", getInterestRate(), "%");
}
}

Files:

accountinfo.txt

transactions.txt

Problem description:

Two files are provided, one with the account information you are to read in, the second a file with transactions.

Modifcations required:

(1) Add the .equals() method to the Customer class. Use the include code shortcut which will also include a hash function. Include both, and select both fields (first, last).

(2) Modify the Bank class as follows:

• Add the method getAccountWithNumber(int accountNumber).

• Modify the openAccount(Account o) method to check if the account owner is a current customer in which case you should not add them to the customers list.

(3) Modify the Main class as follows:

• Add the method void loadAccountInformationFromFile(). Open the input file, accountInfo.txt, and read all the account information into the bank object.Use the .split() method and read one line at a time.

• Add the method void processTransactionsInFile(). This method will open the input file, transactions.txt, and process each transaction.

Each transaction consists of the account number and the transaction amount (positive means deposit, negative means withdrawal). Use the getAccountWithNumber() method - and then make a deposit or a withdraw accordingly.

• Use constants for each index

• Add the method void displayBankRecords() that displays out the bank record.

• Modify the main() method

Explanation / Answer

//Account.java
import java.text.DateFormat;
import java.util.Date;
public class Account
{
   private int accountNumber;
   Date dateOpened;
   private Customer owner;
   private double currentBalance;

   protected Account(int accountNumber, Customer owner, double currentBalance)
   {
       setAccountNumber(accountNumber);
       setDateOpened(new Date());
       setOwner(owner);
       setCurrentBalance(currentBalance);
   }

   public int getAccountNumber() { return accountNumber; }
   public Date getDateOpened() { return dateOpened; }
   public Customer getOwner() { return owner; }
   public double getCurrentBalance() { return currentBalance; }

   private void setAccountNumber(int accountNumber) { this.accountNumber = accountNumber; }
   private void setDateOpened(Date dateOpened) { this.dateOpened = dateOpened; }
   private void setOwner(Customer owner) { this.owner = owner; }
   private void setCurrentBalance(double currentBalance) { this.currentBalance = currentBalance; }

   public double withdraw(double amount) {
       currentBalance -= amount;
       return currentBalance;
   }

   public double deposit(double amount) {
       currentBalance += amount;
       return currentBalance;
   }

   @Override
   public String toString() {
       DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);

       return "[" + getAccountNumber() + "]" +
       ", Opened: " + df.format(getDateOpened()) +
       ", " + owner.toString() +
       " Balance: " + String.format("$%.2f", getCurrentBalance());
   }
}
---------------------------------------------------------------------------------------------------------------------------------


//Customer.java
public class Customer
{
   private String first;
   private String last;

   public Customer(String first, String last)
   {
       setFirst(first);
       setLast(last);
   }

   public final String getFirst() { return first; }
   public final String getLast() { return last; }
   public final String getName() { return getLast() + ", " + getFirst(); }

   public final void setFirst(String first) { this.first = first; }
   public final void setLast(String last) { this.last = last; }

  
  
   /*Override the equals method that checks for the customer equality of
   * first name and last name*/
   @Override
   public boolean equals(Object obj)
   {
       Customer customer=(Customer)obj;
       return first.equals(customer.getFirst()) &&
               last.equals(customer.getLast());
   }
  
   @Override
   public String toString() {
       return "Owner: " + getName();
   }
}
----------------------------------------------------------------------------------------------------------------
//CheckingAccount.java
public class CheckingAccount extends Account
{
   private boolean freeChecks;

   public CheckingAccount(boolean freeChecks, int accountNumber,
           Customer owner, double currentBalance) {

       super(accountNumber, owner, currentBalance);
       setFreeChecks(freeChecks);
   }

   public boolean getFreeChecks() { return freeChecks; }
   public final void setFreeChecks(boolean freeChecks) { this.freeChecks = freeChecks; }

   @Override
   public String toString() {
       return super.toString() + ", " +
               "Free checks: " + (getFreeChecks()? "Yes" : "No");
   }
}
----------------------------------------------------------------------------------------------------------------
//SavingsAccount.java
public class SavingsAccount extends Account
{
   private double interestRate;
  
   public SavingsAccount(double interestRate, int accountNumber,
           Customer owner, double currentBalance) {
       super(accountNumber, owner, currentBalance);
       setInterestRate(interestRate);
   }

   public double getInterestRate() { return interestRate; }

   public final void setInterestRate(double interestRate) { this.interestRate = interestRate; }

   @Override
   public String toString() {
       return super.toString() +
               ", Interest rate: " + String.format("%.2f%s", getInterestRate(), "%");
   }
}
----------------------------------------------------------------------------------------------------------------

//Bank.java
import java.util.ArrayList;
public class Bank
{
   private ArrayList<Customer> customers;
   private ArrayList<Account> accounts;

   public Bank()
   {
       customers = new ArrayList<Customer>();
       accounts = new ArrayList<Account>();
   }

   public Customer getCustomerAtIndex(int index)
   {
       return customers.get(index);
   }
  
   public Account getAccountAtIndex(int index)
   {
       return accounts.get(index);
   }
  
   /**Returns the account of the input account number*/
   public Account getAccountWithNumber(int accountNumber)
   {
      
       for (int i = 0; i <= accounts.size() - 1; i++)
       {          
           if(accounts.get(i).getAccountNumber()==accountNumber)
               return accounts.get(i);
       }
      
       return null;
   }
  
   /**The method openAccount takes the input account and adds the
   * account if account is not already existed*/
   public boolean openAccount(Account account)
   {
      
       boolean exists=false;
       for (int i = 0; i < customers.size() &&!exists; i++)
       {
          
           if(customers.get(i).getFirst().equals(account.getOwner().getFirst())
                   &&customers.get(i).getLast().equals(account.getOwner().getLast()))
                   {
                   exists=true;
                   }
       }      
       if(!exists)
           accounts.add(account);      
       return exists;
   }
  

   public void addCustomer(Customer o) {
       customers.add(o);
   }

   public String customerList() {
       String list = "";
       for (int i = 0; i <= customers.size() - 1; i++) {
           list += customers.get(i) + " ";
       }
       return list;
   }

   public String accountList() {
       String list = "";
       for (int i = 0; i <= accounts.size() - 1; i++) {
           list += accounts.get(i) + " ";
       }
       return list;
   }

   @Override
   public String toString() {
       return "Customers(" + customers.size() + ") " +
               customerList() + " " +
               "Accounts(" + accounts.size() + ") " +
               accountList();
   }
}
---------------------------------------------------------------------------------------------------------

//Main.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main
{
   /* Create static a Bank object to hold some customers and accounts. */
   private static Bank bank = new Bank();
  
   public static void main(String[] args)
   {
      
      
       /*Call the method loadAccounts information from file
          accountinfo.txt */
       loadAccountInformationFromFile();
       /*Call the method load transactions from file
          transactions.txt */
       processTransactionsInFile();
       /*Prints accounts and customers */
       displayBankRecords();
      

      
   }
  
   /**The method processTransactionsInFile that reads the transactions file
      * and process the customers */
   public static void processTransactionsInFile()
   {
       String inputFile="transactions.txt";
       Scanner scanner=null;              
       try
       {
           scanner=new Scanner(new File(inputFile));          
           while(scanner.hasNextLine())
           {
               int accountNumber=scanner.nextInt();              
               double amount=scanner.nextDouble();              
               if(amount>0)      
                   //call deposit method to add amount to customer
                   bank.getAccountWithNumber(accountNumber).deposit(amount);                              
               else
                   //call deposit method to with draw from the customer
                   bank.getAccountWithNumber(accountNumber).withdraw((-1)*amount);                          
           }          
       }
       catch (FileNotFoundException e)
       {
           System.out.println(e);
       }
   } //end of the method
  
   /**Print the customer list and account list and bank records list*/
   public static void displayBankRecords()
   {
       System.out.printf("%s %s %s", "Customer list:","--------------",
               bank.customerList());
       System.out.println("");
       System.out.printf("%s %s %s", "Account list:","-------------",
               bank.accountList());
       System.out.printf(" %s %s %s", "Bank record:","------------",
               bank);
       System.out.println("");
   }
  
   /**Read accounts information file and add to the bank*/
   public static void loadAccountInformationFromFile()
   {
       String inputFile="accountInfo.txt";
       Scanner scanner=null;
      
      
       try
       {
           scanner=new Scanner(new File(inputFile));
          
           while(scanner.hasNextLine())
           {
               int accountNumber=scanner.nextInt();
               String firstName=scanner.next();
               String lastName=scanner.next();              
               double balance=scanner.nextDouble();
               //create an account
               Account account=new Account(accountNumber, new Customer(firstName, lastName),
                       balance);
               //open account with account information
               bank.openAccount(account);          
           }      
       }
       catch (FileNotFoundException e)
       {
           System.out.println(e);
       }              
   }//end of the method loadAccountInformationFromFile

}
----------------------------------------------------------------------------------------------------------------------------
Sample Input files

accountinfo.txt
10100 Adam Apple 500.0
10101 Beatrice Bagel 2000.0
20100 Chris Cucumber 5000.0

transactions.txt
10100 200.0
10101 -200.0
20100 -500.0

Sample Output:

Customers(0)

Accounts(3)
[10100], Opened: Oct 3, 2015 10:46 AM, Owner: Apple, Adam
Balance: $500.00

[10101], Opened: Oct 3, 2015 10:46 AM, Owner: Bagel, Beatrice
Balance: $2000.00

[20100], Opened: Oct 3, 2015 10:46 AM, Owner: Cucumber, Chris
Balance: $5000.00


Customer list:
--------------

Account list:
-------------
[10100], Opened: Oct 3, 2015 10:46 AM, Owner: Apple, Adam
Balance: $700.00

[10101], Opened: Oct 3, 2015 10:46 AM, Owner: Bagel, Beatrice
Balance: $1800.00

[20100], Opened: Oct 3, 2015 10:46 AM, Owner: Cucumber, Chris
Balance: $4500.00


Bank record:
------------
Customers(0)

Accounts(3)
[10100], Opened: Oct 3, 2015 10:46 AM, Owner: Apple, Adam
Balance: $700.00

[10101], Opened: Oct 3, 2015 10:46 AM, Owner: Bagel, Beatrice
Balance: $1800.00

[20100], Opened: Oct 3, 2015 10:46 AM, Owner: Cucumber, Chris
Balance: $4500.00


Note : Some of the files are are not modified and given as supportive files from the post.
Only the required files are modified.

Hope this helps you