Shang Silicon Valley Java foundation practice - Bank Project - experiment topic 5_ Continued 2

Posted by tomms on Wed, 10 Nov 2021 11:48:31 +0100

Experimental topic

Realize more complex overdraft protection mechanism

Note: This is a selection exercise for exercise 1. It includes a more complex overdraft protection mechanism model. It uses customer savings. It uses the customer's savings account for overdraft protection. This exercise must be carried out after completing the above two exercises.

Experimental purpose

Inheritance, polymorphism, method rewriting.

explain

Modify SavingAccount class

  1. Implement the SavingsAccount class as in exercise 1, two subclasses of the Account class.
  2. The SavingAccount class must extend the Account class.
  3. The class must contain an interestRate attribute of type double.
  4. The class must include a public constructor with two parameters (balance and interest_rate). The constructor must pass the balance parameter to the parent constructor by calling super (balance)

Modify the CheckingAccount class

  1. Implement the CheckingAccount class as in exercise 1, two subclasses of the Account class.

  2. The CheckingAccount class must extend the Account class

  3. This class must include an associated attribute called protectedBy, which represents overdraft protection. The type of this property is SavingAccount, and the default value must be null. In addition, this class has no other data properties.

  4. The class must include a public constructor with a parameter (balance), which must pass the balance parameter to the parent constructor by calling super(balance).

  5. Modify the constructor to CheckingAccount(double balance,SavingAccount protect) constructor. The constructor must pass the balance parameter to the parent constructor by calling super (balance).

  6. The CheckingAccount class must override the withraw method. This class must perform the following checks: if the current balance is enough to make up the withdraw al amount, it will proceed normally. If it is not enough, but there is overdraft protection, try to make up the balance amount with a savings account. If the latter transaction fails, the whole transaction must fail, but the balance is not affected.

Modify the Customer class to have two accounts: a savings account and a checking account: both are optional.

  1. In exercise 5_ Continue to modify in 1. The original Customer class contains an associated attribute called account, which can be used to control an account object. Rewrite this class to include two associated properties: savingAccount and checkingAccount, whose default values are null.
  2. It contains two access methods: getSaving and getChecking, which return the total amount of savings and checks respectively.
  3. It contains two opposite methods: SetSaving and setChecking, which assign values to the two associated attributes savingAccount and checkingAccount respectively.

Complete the TestBanking program. The results are as follows:

Customer [Simms, Jane] has a checking balance of 200.0 and a savings balance of 500.0
Checking Acct [Jane Simms] : withdraw 150.00 succeeds? true
Checking Acct [Jane Simms] : deposit 22.50 succeeds? true
Checking Acct [Jane Simms] : withdraw 147.62 succeeds? true
Customer [Simms, Jane] has a checking balance of 0.0 and a savings balance of 424.88

Customer [Bryant, Owen] has a checking balance of 200.0
Checking Acct [Owen Bryant] : withdraw 100.00 succeeds? true
Checking Acct [Owen Bryant] : deposit 25.00 succeeds? true
Checking Acct [Owen Bryant] : withdraw 175.00 succeeds? false
Customer [Bryant, Owen] has a checking balance of 125.0

code

[Account.java] class

package banking;

public class Account {

    protected double balance;    //Current (or immediate) balance of bank account

    //Public constructor, which assigns a value to the balance attribute
    public Account(double init_balance) {
        this.balance = init_balance;
    }

    //Used to obtain current balance
    public double getBalance() {
        return balance;
    }

    /**
     * Increase amount to current balance
     * @param amt   Increase amount
     * @return  Return true (meaning all deposits are successful)
     */
    public boolean deposit(double amt){
        balance+=amt;
        return true;
    }

    /**
     * Subtract amount from current balance
     * @param amt   Number of withdrawals
     * @return  If amt is less than balance, the withdrawal amount is deducted from the balance and returns true. Otherwise, the balance remains unchanged and returns false.
     */
    public boolean withdraw(double amt){
        if (amt < balance){
            balance-=amt;
            return true;
        }else{
            return false;
        }

    }

}

[Customer.java] class

package banking;

public class Customer {

    private String  firstName;
    private String  lastName;
    private SavingsAccount savingsAccount = null;   //savings account
    private CheckingAccount checkingAccount = null; //checking account
    private int numOfAccounts;

    public Customer(String f, String l) {
        this.firstName = f;
        this.lastName = l;
    }

    public void setSavings(SavingsAccount savingsAccount) {
        this.savingsAccount = savingsAccount;
    }

    public void setChecking(CheckingAccount checkingAccount) {
        this.checkingAccount = checkingAccount;
    }

    public SavingsAccount getSavings() {
        return savingsAccount;
    }

    public CheckingAccount getChecking() {
        return checkingAccount;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    /**
     * Get numOfAccounts
     * @return  numOfAccounts
     */
    public int getNumOfAccounts() {
        return numOfAccounts;
    }


}

[Bank.java] class

package banking;

/**
 * Banking
 */
public class Bank {

    private Customer[] customers;   //Array of Customer objects
    private int numberOfCustomer;   //Integer that tracks the index of the next customers array

    /**
     * Public constructor to initialize the customers array with the appropriate maximum size (at least greater than 5).
     */
    public Bank() {
        customers = new Customer[10];
    }

    /**
     *  The method must construct a new customer object according to the parameters (last name, first name) and put it into the customer array. You must also add 1 to the value of the numberOfCustomers property.
     * @param f lastname
     * @param l name
     */
    public void addCustomer(String f,String l){
        customers[numberOfCustomer++]=new Customer(f,l);
    }

    /**
     * Get customer by subscript index
     * @param index Subscript index
     * @return  customer
     */
    public Customer getCustomer(int index) {
        return customers[index];
    }

    public int getNumOfCustomers() {
        return numberOfCustomer;
    }
}

[CheckingAccount.java] class

package banking;

public class CheckingAccount extends Account{

    private SavingsAccount protectedBy=null;

    public CheckingAccount(double balance) {
        super(balance);
    }

    public CheckingAccount(double balance, SavingsAccount protect) {
        super(balance);
        this.protectedBy = protect;
    }

    /**
     * This method must perform the following checks. If the current balance is enough to make up the withdrawal amount, it will proceed normally.
     * If overdraft protection is not sufficient, try to use the value of overdraftProtection to make up for the balance amount
     * If the amount required to cover the overdraft is greater than the current level of protection. The whole transaction fails, but the balance is not affected.
     * @param amt   Number of withdrawals
     * @return  Returning true means that the transaction is successful, otherwise the transaction fails
     */
    @Override
    public boolean withdraw(double amt){
        if (balance < amt){
            if(protectedBy==null){
                return false;
            }
            if (amt-balance> protectedBy.getBalance()) {
                return false;
            }else {
                protectedBy.balance-=amt-balance;
                balance=0;
                return true;
            }
        }else {
            balance-=amt;
            return true;
        }
    }
}

[SavingsAccount.java] class

package banking;

public class SavingsAccount extends Account{

    private double interestRate;

    public SavingsAccount(double balance, double interest_Rate) {
        super(balance);
        this.interestRate = interest_Rate;
    }
}

[TestBanking.java] class

package banking;/*
 * This class creates the program to test the banking classes.
 * It creates a new Bank, sets the Customer (with an initial balance),
 * and performs a series of transactions with the Account object.
 */

import banking.*;

public class TestBanking {

  public static void main(String[] args) {
    Bank bank = new Bank();
    Customer customer;
    Account account;

    // Create two customers and their accounts
    bank.addCustomer("Jane", "Simms");
    customer = bank.getCustomer(0);
	account = customer.getChecking();
    customer.setSavings(new SavingsAccount(500.00, 0.05));
    customer.setChecking(new CheckingAccount(200.00, customer.getSavings()));
    
	bank.addCustomer("Owen", "Bryant");
    customer = bank.getCustomer(1);
    customer.setChecking(new CheckingAccount(200.00));

    // Test the checking account of Jane Simms (with overdraft protection)
    customer = bank.getCustomer(0);
    System.out.println("Customer [" + customer.getLastName()
		       + ", " + customer.getFirstName() + "]"
		       + " has a checking balance of "
		       + customer.getChecking().getBalance()
		       + " and a savings balance of "
		       + customer.getSavings().getBalance());
    account=customer.getChecking();
    System.out.println("Checking Acct [Jane Simms] : withdraw 150.00 succeeds? "
		       + account.withdraw(150.00));
    System.out.println("Checking Acct [Jane Simms] : deposit 22.50 succeeds? "
		       + account.deposit(22.50));
    System.out.println("Checking Acct [Jane Simms] : withdraw 147.62 succeeds? "
		       + account.withdraw(147.62));
    System.out.println("Customer [" + customer.getLastName()
		       + ", " + customer.getFirstName() + "]"
		       + " has a checking balance of "
		       + account.getBalance()
		       + " and a savings balance of "
		       + customer.getSavings().getBalance());
    System.out.println();

    // Test the checking account of Owen Bryant (without overdraft protection)
    customer = bank.getCustomer(1);

    account = customer.getChecking();
    System.out.println("Customer [" + customer.getLastName()
		       + ", " + customer.getFirstName() + "]"
		       + " has a checking balance of "
		       + account.getBalance());

    System.out.println("Checking Acct [Owen Bryant] : withdraw 100.00 succeeds? "
		       + account.withdraw(100.00));
    System.out.println("Checking Acct [Owen Bryant] : deposit 25.00 succeeds? "
		       + account.deposit(25.00));

    System.out.println("Checking Acct [Owen Bryant] : withdraw 175.00 succeeds? "
		       + account.withdraw(175.00));
    System.out.println("Customer [" + customer.getLastName()
		       + ", " + customer.getFirstName() + "]"
		       + " has a checking balance of "
		       + account.getBalance());
    System.out.println();
  }
}

Topics: Java Back-end