Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA 1. Constructors There will be 2 different ways to create a bank account object: By providing the account holders name only. By providing the

JAVA

1. Constructors

There will be 2 different ways to create a bank account object:

By providing the account holders name only.

By providing the name and an initial account balance.

In both cases the account should also be initialized with a 6-digit random account number (a random integer). If the account object is created without an initial balance, the balance field should be set to zero, if a balance is provided, the balance field must be set to the balance provided.

You will need to utilize your knowledge on chained constructors to accomplish this efficiently. Try to figure out which task must be done by both constructors and perform that task in your first constructor; any additional tasks can be done in the additional separate constructors that can then call your first constructor via the this() method.

NOTE: The account number must be a 6 digit random integer.

2. deposit(double money)

This method deposits money into the bank account. This method acts as a mutator to the balance field. The method should return the amount deposited if the deposit was successful and -1 if the amount to be deposited is less than 0.

3. withdrawMoney(double money)

This method removes and returns the amount of money from the bank account; it also decrements the balance field by the amount withdrawn. If the account doesn't have sufficient funds for withdrawal or the amount to be withdrawn is less than 0, it returns -1.

4. getAccountNumber()

This method returns the 6 digit account number.

5. getInterestRate()

This method returns the current interest rate at the bank. To get the interest rate you need to know what the last interest rate was, and how many bank accounts have been created (how many objects have been instantiated) since the interest rate was last updated. The interest rate depends on the amount of accounts that have been opened. So the Interest rate at the bank starts at 30% (0.3) but each time 5 new people decide to join the bank, 2% (0.02) is taken of the (global) interest rate.

For example if the interest rate is 24% we know that at least 15 accounts have been created (it drop 2% every 5 objects created); however the interest rate should not drop to 22% until 20 accounts have been created.

NOTE: There is a naive and tedious way to do this by checking manually multiples of 5, try to avoid that method, even a loop will not be the most efficient way, try to come up with a clever solution using static variables and methods.

6. transfer(BankAccount destinationBankAccount, double amount)

This method subtracts money from one account (source) and adds it to the other(destination). Be sure to use the this keyword when trying to access variables and methods of the account object on which the transfer() method was called. You should also make use of the deposit method on the destination object. The method returns the amount of money transferred or -1 in the following 3 cases:

There aren't sufficient funds in the source account to make the transfer.

The destination account is null.

The amount to be transferred is less than 0.

7. transfer(BankAccount[] destinationBankAccount, double amount)

This method subtracts money from one account (source) and adds it to each destination accounts, the amount specified will be transferred to each account in the array of bank accounts. Be sure to use the this keyword when trying to access variables and methods of the account object on which the transfer() method was called. You should also make use of the deposit method on the destination object. The method returns the amount of money transferred or -1 in the following 4 cases:

There aren't sufficient funds in the source account to make the transfer.

The destination account is null.

The amount to be transferred is less than 0.

The number of accounts to be transferred is equal to zero.

Your task in this project is to complete the above mentioned methods, test that all the code works well by making use of a main method.

Example uses of BankAccount class

The following statement is used to create an account for a person named Bob with initial deposit of $1000.

 BankAccount bankAccountOne = new BankAccount(Bob, 1000); 

The following statement is used to create an account for a person named Sara with with no initial deposit.

 BankAccount bankAccountTwo = new BankAccount(Sara); 

The following statement is used to transfer money from Bob to Sara.

 double transfer = bankAccountOne.transfer(bankAccountTwo, 500); 

My code:

package com.company; import java.util.ArrayList; import java.util.Random; public class BankAccount { //instance variables  private int accountNumber; private String name; private double balance; //static properties  private static double interest = 0.3; private static ArrayList accounts = new ArrayList(); //The first constructor receives a name and creates bank account with balance = 0  public BankAccount (String name) { this.name = name; balance = 0; Random rn = new Random(); accountNumber = rn.nextInt(899999)+100000; } //TODO 1.1   //The second constructor receives name and balance, creates a bank amount and updates balance  public BankAccount (String name, double balance) { this.name = name; Random rn = new Random(); accountNumber = rn.nextInt(899999)+100000; //TODO 1.2  } //This method performs deposit operation  public double deposit(double money) { if(money< 0){return -1;} else{ balance+=money; return money;} //TODO 2  } //This method performs withdraw operation  public double withdrawMoney(double money) { if(money<0){return -1;} else if (this.balancereturn -1;} else{ this.balance-=money; return money;} //TODO 3  } //This method returns account number  public int getAccountNumber() { return accountNumber; //TODO 4  } //This method returns interest rate  public static double getInterestRate() { int s =accounts.size(); double rate =interest- s/5 *0.02; return rate; //TODO 5  } //This method performs a transfer operation to a single bank account  public double transfer(BankAccount destinationBankAccount, double amount) { if(destinationBankAccount == null){return -1;} else if(amount >this.balance){return -1;} else if(amount<0){return -1;}else  { destinationBankAccount.deposit(amount); return amount; } //TODO 6  } //This method performs a transfer operation to multiple accounts  public double transfer(BankAccount[] destinationBankAccount, double amount) { if(amount * (destinationBankAccount.length+1) < this.balance){return -1;} else if(destinationBankAccount==null){return -1;} else if(amount<0){return -1;} else if(destinationBankAccount.length==0){return -1;} else for(int i=0;ilength;i++){ destinationBankAccount [i].deposit(amount); } return amount*(destinationBankAccount.length+1); //TODO 7  } public static void main(String args[]) { //Feel free to add code that will help you test your methods  }} 

//

java.lang.AssertionError: Error with interestRate

java.lang.AssertionError: Error with interestRate 0.3

java.lang.AssertionError: Error with interestRate -1.0

java.lang.AssertionError: Error with withdraw

java.lang.AssertionError: Error with transfer

java.lang.NullPointerException: null

java.lang.AssertionError: Error with transfer to accounts

java.lang.AssertionError: Error with transfer to accounts120.0

END OF TEST FAILURES

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Algorithmic Trading Navigating The Digital Frontier

Authors: Alex Thompson

1st Edition

B0CHXR6CXX, 979-8223284987

More Books

Students also viewed these Databases questions