Assignment 2 In this assignment we will focus on inheritance

Assignment 2:

In this assignment we will focus on inheritance and testing.

Continuing from the ATM application in Assignment 1 we will build an inheritance structure for different kinds of accounts available at the bank. The class descriptions are as follows.

Account

The supertype. It will hold information common to all the subtypes. Fields include:

- double balance – the dollar amount in a subtype account
- String accountNumber – same as assignment 1 but no longer in the BankCustomer class.
- boolean active – true is the account is active, false when it is closed.
- ArrayList for transaction information – every time there is a transaction a record of it will be stored here. This will apply to both deposits and withdrawals.
- void addTransactionInfo(String) is a method that will add a String that details the information when a deposit or withdrawal is mode in any of the subtype accounts.

SavingsAccount

A subtype of Account. It will hold information that is unique to a savings account. Fields include:

- final double MIN_AMOUNT – the minimum amount an account must have in order to remain active.

ChequingAccount

A subtype of Account. It will hold information for a chequing account. Fields include:

- final double FEE – a fee that will be charged for each cheque that is processed.
- Int numberOfCheques – the number of cheques processed during a given time period (one month for example).

GoldAccount

This is an account that is provided to senior citizens, people 65 years and older. Fields include:

- double interestRate – the monthly interest earned by the account
- boolean inOverdraft - used to determine if the account has a negative balance
- final double FEE – a monthly fee that is charge but only if the account is in overdraft
- final double OVERDRAFT_AMT - will be used to set the overdraft limit that account holders are allowed to withdraw.

All classes in the hierarchy will implement the default and overloaded constructors, getters, setters, toString methods. All validation will done in the setters which will be called from the overloaded constructor.

Changes to existing classes

BankCustomer

- Will no longer hold the accountNumber. That will be in the Account class now. Also to be removed will be the balance, which will now be held in the Account super class.
- Account myAccount - A customer will have a reference to an Account. This will be one of either the above mentioned subtypes. Be clear that the reference in this class is of the supertype.
- in age field and getter and setter will added to hold a customer\'s age. This will be used when setting up a GoldAccount.
- All the rest remains unchanged, including the passcode.

Bank

The removeAccount method will be renamed “deactivate” it will now change the active setting on an account to false. It will not remove the account from the collection.

ATM

This class will have to be modified to incorporate all the new classes and functions added with Assignment 2.

- ATM will seed the Bank with different types of accounts.
- The subtype accounts will use a prefix code in the number. Examples are:
- SA-123 for a SavingsAccount
- CH-123 for a ChequingAccount
   - GL-123 for a GoldAccount
- prefix codes will identify the type of account when the account info is being displayed.
- As in Assignment 1 when the program is exited a summary of the current account will be displayed including all fees and interest amounts.
- The transaction record for the current account will also be displayed on exit.

Testing

A second part of Assignment 2 is to include JUnit tests.

- For this assignment only the Account hierarchy requires unit tests to be done.
- Testing will be thorough, using positive and negative tests.
- All the classes within the Account hierarchy need to be tested.
- Do not perform more than one test per unit.
- The test classes will be included in your submission.

Sample of summary after the user has exited the program.

Thank you for banking at Bullwinkle\'s Bank
ACCOUNT SUMMARY:
BankCustomer [firstName=Darby, lastName=Dog, passcode=123, account=SavingsAccount
[toString()=Account [balance=225.0, accountNumber=SA-123, active=true]], age=35]
Acount Activity:
Wed May 25 12:04:36 PDT 2016 - deposit: $200.00
Wed May 25 12:04:46 PDT 2016 - withdrawal: $75.00

DEBUG: Displaying all the accounts in the bank.
BankCustomer [firstName=Freckle, lastName=Cat, passcode=789, account=GoldAccount
[interestRate=0.025, overdraft=false, toString()=Account [balance=200.0,
accountNumber=GL-123, active=true]], age=65]
BankCustomer [firstName=Darby, lastName=Dog, passcode=123, account=SavingsAccount
[toString()=Account [balance=225.0, accountNumber=SA-123, active=true]], age=35]
BankCustomer [firstName=Myia, lastName=Dog, passcode=456, account=ChequingAccount
[numberOfCheques=0, toString()=Account [balance=50.0, accountNumber=CH-123,
active=true]], age=12]

From Previous Assignment

Assignment 1:

ATM class:


/**
* ATM class, The Assignment application driver
*
* @author BKR
*/
public class ATM {

   private InputReader reader;
   private String accountNumber;
   private String passcode;
   private boolean customerVerified;

   private Bank theBank;
   private BankCustomer currentCustomer;

   /**
   * Default constructor. Calls the initialize() method to seed the Bank with
   * some BankCustomers. Calls the run() method to perform the primary program
   * functions.
   */
   public ATM() {
       super();
       initialize();
       run();
   }

   /**
   * Main method calls the class default constructor.
   * @param args for program arguments (not used)
   */
   public static void main(String[] args) {

       new ATM();

   }

   /**
   * The primary application processor. All application functions are called
   * from here. Uses a loop to prompt users to perform banking transactions.
   * Must use switch/case selection to determine uses choices.
   */
   public void run() {

       reader = new InputReader();
       boolean exit = false;

       System.out.println(\"Welcome to Bullwinkle\'s Bank.\");

       while (!exit) {
           System.out.println(\"Choose one of the following options:\");
           System.out.println(\"1 - Sign In\");
           System.out.println(\"2 - Deposit\");
           System.out.println(\"3 - Withdraw\");
           System.out.println(\"4 - Display Account Info\");
           System.out.println(\"5 - Exit\");
           System.out.print(\"> \");
           int choice = reader.getIntInput();

           switch (choice) {

           case 1:
               choice = 1;
               verifyCustomer();
               break;
           case 2:
               choice = 2;
               transactDeposit();
               break;
           case 3:
               choice = 3;
               transactWithdraw();
               break;
           case 4:
               choice = 4;
               displayAccountInformation();
               break;
           case 5:
               choice = 5;
               System.out
                       .println(\"Thank you for banking at Bullwinkle\'s Bank\");
               System.out
                       .println(\"DEBUG: Displaying all the accounts in the bank.\");
               Bank.displayAllCustomers();
               System.exit(0);

           }

       }

   }

   /**
   * Adds Customer references to the Back HashMap as seed data for testing.
   */
   public void initialize() {

       theBank = new Bank();
       theBank.createAccount(new BankCustomer(\"Darby\", \"Dog\", \"ST-123\", \"123\"));
       theBank.createAccount(new BankCustomer(\"Myia\", \"Dog\", \"ST-456\", \"456\"));
       theBank.createAccount(new BankCustomer(\"Freckle\", \"Cat\", \"ST-789\",
               \"789\"));

   }

   /**
   * Performs a deposit into a BankCustomer\'s account. Checks to see if the
   * user has signed in. If not, then verifyCustomer() is called and the menu
   * is displayed again.
   */
   public void transactDeposit() {

       if (customerVerified) {
           System.out.println(\"Enter the amount to deposit: \");
           currentCustomer.addToBalance(reader.getDoubleInput());

       } else {

           System.out
                   .println(\"ERROR: You must LOGIN before you can perform a transaction.\");
           verifyCustomer();
       }
   }

   /**
   * Performs a withdrawal from a BankCustomer\'s account. Checks to see if the
   * user has signed in. If not, then verifyCustomer() is called and the menu
   * is displayed again.
   */
   public void transactWithdraw() {

       if (customerVerified) {
           System.out.println(\"Enter the amount to withdraw: \");
           double amount = reader.getDoubleInput();
           if (amount <= currentCustomer.getBalance()) {
               currentCustomer.subtractFromBalance(amount);
           } else {
               System.out
                       .println(\"ERROR: You have insufficinet funds to withdraw that amount.\");
           }
       } else {

           System.out
                   .println(\"ERROR: You must LOGIN before you can perform a transaction.\");
           verifyCustomer();
       }

   }

   /**
   * Displays a BankCustomer\'s account information if the customer has been
   * previously verified.
   */
   public void displayAccountInformation() {

       if (customerVerified) {
           System.out.println(\"Here is your information.\");
           Bank.displayCustomerInformation(currentCustomer);
       } else {

           System.out
                   .println(\"ERROR: You must LOGIN before you can perform a transaction.\");
           verifyCustomer();
       }

   }

   /**
   * To confirm a BankCustomer\'s account number and passcode. Called when the
   * user is required to sign in to the application. Will set a boolean so the
   * user does not have to sign in again during the session.
   */
   public void verifyCustomer() {

       System.out.println(\"Enter Account Number: \");
       accountNumber = reader.getStringInput();
       System.out.println(\"Enter Passcode: \");
       passcode = reader.getStringInput();

       currentCustomer = Bank.theBank.get(accountNumber);

       if (currentCustomer != null) {
           if (passcode.equals(currentCustomer.getPasscode())) {

               customerVerified = true;
           } else{
               System.out.println(\"ERROR: Either account number or passcode is not correct.\");
               run();
           }

       } else {
           System.out.println(\"ERROR: Either account number or passcode is not correct.\");
           run();
       }

   }
}

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


Bank class:


/**
* Supports a HashMap of customer accounts.
*
* @author BKR
*/
public class Bank {

   /**
   * The bank collection to hold all BankCustomer data.
   * Uses a customer\'s account number as key and the BankCustomer reference as the value.
   */
   public static HashMap<String, BankCustomer> theBank;

   /**
   * Default constructor for Bank class.
   * Initializes the HashMap
   */
   public Bank() {
       super();
       theBank = new HashMap<String, BankCustomer>();
   }

   /**
   * Add a new BankCustomer element to the HashMap.
   * @param newCustomer The new element to add to the HashpMap using
   * the account number as the key and the new BankCustomer as the value.
   */
   public void createAccount(BankCustomer newCustomer) {

       if (newCustomer != null) {
           theBank.put(newCustomer.getAccountNumber(), newCustomer);
       }
   }

   /**
   * Removes an BankCustomer from the HashMap.
   * @param accountNumber The key of the element to remove from the HashMap.
   */
   public void closeAccount(String accountNumber) {

       if (accountNumber != null) {

           theBank.remove(accountNumber);
       }
   }

   /**
   * Gets the BankCustomer from the HashMap and adds a double amount
   * to a BankCustomer\'s balance.
   * @param accountNumber The account number of the BankCustomer.
   * @param amount The amount to deposit.
   */
   public void deposit(String accountNumber, double amount) {

       if (accountNumber != null) {
           BankCustomer customer = theBank.get(accountNumber);
           customer.addToBalance(amount);
       }

   }
  
   /**
   * Gets the BankCustomer from the HashMap and subtracts an amount
   * from a BankCustomer\'s balance as long as it does not leave a negative balance.
   * @param accountNumber The account number of the BankCustomer.
   * @param amount The amount to subtract from a BankCustomer\'s balance.
   */
   public void withdraw(String accountNumber, double amount) {
      
       if (accountNumber != null) {
           BankCustomer customer = theBank.get(accountNumber);
           customer.subtractFromBalance(amount);
       }
      
   }
  
   /**
   * Displays the details of a BankCustomer element in the HshMap.
   * Uses BankCustomer.toString() implementation.
   * @param customer the BankCustomer chosen to display.
   */
   public static void displayCustomerInformation(BankCustomer customer){
      
       if(customer != null){
          
           System.out.println(customer);
       }
   }
  
   /**
   * Displays all elements of the HashMap by using BankCustomer.toString()
   * implementation of each.
   */
   public static void displayAllCustomers(){
      
       for(BankCustomer customer : theBank.values()){
          
           System.out.println(customer);
          
       }
   }

}

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


BankCustomer class:


/**
* BanckCustomer data class to hold customer information
*
* @author BKR
*/
public class BankCustomer {

   private String firstName;
   private String lastName;
   private String accountNumber;
   private String passcode;
   private double balance;

   /**
   * Default constructor for a BankCustomer. Sets the fields to the default
   * values for each type.
   */
   public BankCustomer() {
       super();
       // TODO Auto-generated constructor stub
   }

   /**
   * @param firstName
   * String to initialize the firstName field
   * @param lastName
   * String to initialize the lastName field
   * @param accountNumber
   * String to initialize the accountNumber field
   * @param passcode
   * String to initialize the passcode field
   */
   public BankCustomer(String firstName, String lastName, String accountNumber, String passcode) {
       super();

       setFirstName(firstName);
       setLastName(lastName);
       setAccountNumber(accountNumber);
       setPasscode(passcode);

   }

   /**
   * Accessor method for the firstName field
   *
   * @return the firstName as a String
   */
   public String getFirstName() {
       return firstName;
   }

   /**
   * Mutator for the firstName field
   *
   * @param firstName
   * the firstName to set
   */
   public void setFirstName(String firstName) {

       if (firstName != null && !firstName.trim().isEmpty()) {
           this.firstName = firstName;
       }
   }

   /**
   * Accessor method for the lastName
   *
   * @return the lastName as a String
   */
   public String getLastName() {
       return lastName;
   }

   /**
   * Mutator for the lastName field
   *
   * @param lastName
   * the lastName to set
   */
   public void setLastName(String lastName) {

       if (lastName != null && !lastName.trim().isEmpty()) {
           this.lastName = lastName;
       }
   }

   /**
   * Accessor method for the accountNumber field
   *
   * @return the accountNumber as a String
   */
   public String getAccountNumber() {
       return accountNumber;
   }

   /**
   * Mutator for the accountNumber field
   *
   * @param accountNumber
   * the accountNumber to set
   */
   public void setAccountNumber(String accountNumber) {

       if (accountNumber != null && !accountNumber.trim().isEmpty()) {
           this.accountNumber = accountNumber;
       }
   }

   /**
   * Accessor method for the passcode field
   *
   * @return the passcode as a String
   */
   public String getPasscode() {
       return passcode;
   }

   /**
   * Mutator for the passcode field
   *
   * @param passcode
   * the passcode to set
   */
   public void setPasscode(String passcode) {

       if (passcode != null && !passcode.trim().isEmpty()) {
           this.passcode = passcode;
       }
   }

   /**
   * Accessor method for the the balance field
   *
   * @return the balance as a double
   */
   public double getBalance() {
       return balance;
   }

   /**
   * Mutator for the the balance field
   *
   * @param balance
   * the balance to set
   */
   public void setBalance(double balance) {
       this.balance = balance;
   }

   /**
   * Adds to a BankCustomer\'s balance
   *
   * @param amount
   * a double to add to the existing balance field
   */
   public void addToBalance(double amount) {

       if (amount > 0) {
           balance += amount;
       }
   }

   /**
   * Subtracts from a BankCustomer\'s balance
   *
   * @param amount
   * a double to subtract from the balance field
   */
   public void subtractFromBalance(double amount) {
      
       if (amount > 0) {
           balance -= amount;
       }
   }

   /**
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return \"BankCustomer [firstName=\" + firstName + \", lastName=\" + lastName + \", accountNumber=\" + accountNumber
               + \", passcode=\" + passcode + \", balance=\" + balance + \"]\";
   }

}

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


InputReader class:


import java.util.Scanner;

/**
* Class InputReader reads user input from the keyboard.
*
* @author BKR
*/
public class InputReader {
   private Scanner scanner;

   /**
   * Default constructor for InputReader class
   * Create a new InputReader to read user input.
   */
   public InputReader() {
       scanner = new Scanner(System.in);
   }

   /**
   * Retrieves a user\'s int input
   * @return the user\'s input as an int
   */
   public int getIntInput() {
       try {
           return scanner.nextInt();
       } catch (java.util.InputMismatchException e) {
           System.out.println(\"Not a number - treating as zero\");
           scanner.nextLine(); // clear the buffer
           return 0;
       }
   }
  
   /**
   * Retrieves a user\'s double input
   * @return the user\'s input as an double
   */
   public double getDoubleInput() {
       try {
           return scanner.nextDouble();
       } catch (java.util.InputMismatchException e) {
           System.out.println(\"Not a number - treating as zero\");
           scanner.nextLine(); // clear the buffer
           return 0.0;
       }
   }

   /**
   * Retrieves a user\'s String input
   * @return the user\'s input as an String
   */
   public String getStringInput() {

       String input = scanner.next();
       if (input.length() > 0) {
           return input;
       } else {
           System.out.println(\"ERROR:Invalid input provided\");
       }
       return null;

   }
}

Solution

Answer:

Note: I have created Account and its subtype classes.

import java.io.*;
import java.lang.*;
import java.util.*;
class Account
{
   double balance;
   String accountNumber;
   boolean active;
   ArrayList<String> tInfo;
   public Account()
   {
       balance=0;
       accountNumber=\"\";
       active=false;
       tInfo=new ArrayList<String>();
   }
   public Account(double b, String aa, boolean act)
   {
       setBal(b);
       setAN(aa);
       setSt(act);
       tInfo=new ArrayList<String>();
   }
   public void setBal(double b)
   {
       if(b>0)
           balance=b;
       else balance=0;
   }
   public void setAN(String aa)
   {
       accountNumber=aa;
   }
   public void setSt(boolean act)
   {
       active=act;
   }
   public void addTransactionInfo(String tt)
   {
       tInfo.add(tt);
   }
   public double getbala()
   {
       return balance;
   }
   public String getAN()
   {
       return accountNumber;
   }
   public boolean getSt()
   {
       return active;
   }
   public String printTransInfo()
   {
       String tt=\"\";
       for(int kk=0;kk<tInfo.size();kk++)
       {
           tt=tt+tInfo.get(kk);
       }
       return tt;
   }
   public String toString()
   {
       return \"toString()=Account [balance=\"+balance+\", accountNumber=\"+accountNumber+\", active=\"+active+\"]\";
   }
}

class SavingsAccount extends Account
{
   final double MIN_AMOUNT;
  
   public SavingsAccount()
   {
       super();
       MIN_AMOUNT=0;
   }
   public SavingsAccount(double b, String aa, boolean act,double min)
   {
       super(b,aa,act);
       MIN_AMOUNT=min;
       if(b<MIN_AMOUNT)
           super.setSt(false);
   }
   public void setBal(double b)
   {
       super.setBal(b);
   }
   public void setAN(String aa)
   {
       super.setAN(aa);
   }
   public void setSt(boolean act)
   {
       super.setSt(act);
   }
   public void addTransactionInfo(String tt)
   {
       super.addTransactionInfo(tt);
   }
   public double getbala()
   {
       return super.getbala();
   }
   public String getAN()
   {
       return super.getAN();
   }
   public boolean getSt()
   {
       return super.getSt();
   }
   public String printTransInfo()
   {
       return super.printTransInfo();
   }
   public String toString()
   {
       return \"account=SavingsAccount [\"+super.toString()+\"]\";
   }
}


class ChequingAccount extends Account
{
   final double FEE;
   int numberOfCheques;
  
   public ChequingAccount()
   {
       super();
       FEE=0;
      
   }
   public ChequingAccount(double b, String aa, boolean act,double fee,int nCheq)
   {
       super(b,aa,act);
       FEE=fee;
       numberOfCheques=nCheq;
   }
   public void setBal(double b)
   {
       super.setBal(b);
   }
   public void setAN(String aa)
   {
       super.setAN(aa);
   }
   public void setSt(boolean act)
   {
       super.setSt(act);
   }
   public void addTransactionInfo(String tt)
   {
       super.addTransactionInfo(tt);
   }
   public double getbala()
   {
       return super.getbala();
   }
   public String getAN()
   {
       return super.getAN();
   }
   public boolean getSt()
   {
       return super.getSt();
   }
   public int getNCheq()
   {
       return numberOfCheques;
   }

   public String printTransInfo()
   {
       return super.printTransInfo();
   }
   public String toString()
   {
       return \"account=ChequingAccount [numberOfCheques=\"+numberOfCheques+\",\"+super.toString()+\"]\";
   }
}

class GoldAccount extends Account
{
   double interestRate;
   boolean inOverdraft;
   final double FEE;
   final double OVERDRAFT_AMT;
  
   public GoldAccount()
   {
       super();
       interestRate=0.01;
       inOverdraft=false;
       FEE=0;
       OVERDRAFT_AMT=0;
   }
   public GoldAccount(double b, String aa, boolean act,double ir, boolean od,double fee,double ovamt)
   {
       super(b,aa,act);
       interestRate=ir;
       inOverdraft=od;
       FEE=fee;
       OVERDRAFT_AMT=ovamt;
      
   }
   public void setBal(double b)
   {
       super.setBal(b);
   }
   public void setAN(String aa)
   {
       super.setAN(aa);
   }
   public void setSt(boolean act)
   {
       super.setSt(act);
   }
   public void setIntRate(double ir)
   {
       interestRate=ir;
   }
   public void setOvBol(boolean od)
   {
       inOverdraft=od;
   }  
   public void addTransactionInfo(String tt)
   {
       super.addTransactionInfo(tt);
   }
   public double getbala()
   {
       return super.getbala();
   }
   public String getAN()
   {
       return super.getAN();
   }
   public boolean getSt()
   {
       return super.getSt();
   }
  
   public boolean getOvBol()
   {
       return inOverdraft;
   }
   public double getIntRate()
   {
       return interestRate;
   }
   public String printTransInfo()
   {
       return super.printTransInfo();
   }
   public String toString()
   {
       return \"account=Gold Account [intrestRate=\"+interestRate+\", overdraft=\"+inOverdraft+\",\"+ super.toString()+\"]\";
   }
}

Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru
Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru
Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru
Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru
Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru
Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru
Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru
Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru
Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru
Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru
Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru
Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru
Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru
Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru
Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru
Assignment 2: In this assignment we will focus on inheritance and testing. Continuing from the ATM application in Assignment 1 we will build an inheritance stru

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site