A More OO Bank Account (Java) In many ways, the result of this exercise will look very similar to the static version, bu

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

A More OO Bank Account (Java) In many ways, the result of this exercise will look very similar to the static version, bu

Post by answerhappygod »

A More OO Bank Account (Java)
In many ways, the result of this exercise will look very similar
to the static version, but now we can have several BankAccounts,
with differing data.
A constructor that accepts the account number and the name of
the account. Calling it should result in a new instance of Bank
Account that is initialised to have the given account number and
name, and a balance of 0.
A constructor that accepts the account number, the name of the
account, and a starting balance. Calling it should result in a new
instance of Bank Account that is initialised to have the given
account number, name, and balance.
widthdraw should allow funds to be withdrawn from the account if
they are available. Note that the body of this method has been
written for you, you just need to define the correct return
type.
setInterestRate should allow the interest rate to be updated. It
should accept the new interest rate as a parameter, and should not
return anything.
getInterestRate should allow the interest rate to be retrieved.
It should not accept any parameters, and should return the value of
the interest rate.
toString should provide a simple string representation of the
account. Note that the body of this method has been written for
you, you just need to define the correct parameters and return
type.
Provided Code Skeleton:
/**
* This Object-Oriented" version of the "BankAccount"
class
* is a simple introduction to Constructors /
* private data members / static vs. not static / and
the
* "toString" method.
*
* SKELETON FOR LAB TEST.
*
* @author Raymond Lister
* @version April 2015
*
* @author Luke Mathieson
* @version April 2021
*/
public class BankAccountOO {
public static double interestRate = 0.0;
// Note: the interest rate is "static". That is,
all
// bank accounts have the same
interest rate.
/*
* The instance variables follow (i.e. not
static). These
* are also called "fields" or "private data
members".
*/
public final int accountNumber;
public String accountName;
public double balance; // e.g. 1.27 means $1.27
public static void main (String[] args) {

BankAccountOO.setInterestRate(7.50);

System.out.println("(Interest rate
should be 7.50) Interest rate is " +
BankAccountOO.getInterestRate() + ".");

BankAccountOO bankaccountoo = new
BankAccountOO(99998888,"Jack");
System.out.println("So we created an
account for Jack with zero dollars in his bank account under the
account number 99998888.");
System.out.println("");
System.out.println("");
System.out.println("For the object
created, the account number is: " +
bankaccountoo.accountNumber);
System.out.println("For the object
created, the account name is: " + bankaccountoo.accountName);
System.out.println("For the object
created, the balance is: " + bankaccountoo.balance);
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("Now we use the
constructor which takes three argumants including name, number and
initial balance:");
BankAccountOO bankaccountoo2 = new
BankAccountOO(12345678,"Jane",1000.99);
System.out.println("So we created an
account for Jane with 1000.99 dollars in her bank account under the
account number 12345678");
System.out.println("");
System.out.println("");
System.out.println("For the second
object created, the account number is: " +
bankaccountoo2.accountNumber);
System.out.println("For the second
object created, the account name is: " +
bankaccountoo2.accountName);
System.out.println("For the second
object created, the balance is: " + bankaccountoo2.balance);
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("Now we use the
method toString to print it all in the same line");
System.out.println("First object: " +
bankaccountoo.toString());
System.out.println("Second object: " +
bankaccountoo2.toString());
System.out.println("");
System.out.println("");
System.out.println("Testing withdraw
method on Jane's account: Jane's new balance after we withdraw
100.00 is " + bankaccountoo2.withdraw(100.00));
}
/**
* The constructors now follow
*/
/**
* @param num number for the
account
* @param name name of the
account
*/
public returnTypeIfAny
nameOfConstructor(parametersIfAny) {
add whatever code is required in the
constructor body
}
/**
* @param num number
for the account
* @param name name of the
account
* @param bal opening
balance
*/
public returnTypeIfAny
nameOfConstructor(parametersIfAny) {
add whatever code is required in the
constructor body
}
/*
* A number of the methods from the "static"
version
* have been left out of this object-oriented
version
* because you've already done that once.
*
* Most of those methods would appear in a
complete
* version of this object-oriented version of
the
* class, with the *ONLY* change being that the
reserved
* word "static" would be deleted from the
method
* header.
*
* The method setAccountNumber() would
definitely NOT
* be copied across from the static version to
this
* object-oriented version. That's because each
object
* (i.e. instance of BankAccount) represents a
unique
* bank account. The account number set up by
the
* constructor should never change again.
*
* The method setAccountName() MIGHT be copied
across.
* That's a business decision. Some banks
might allow
* a customer to change their name. Other
banks might
* only allow a name change by creating a new
account.
* If the account name cannot be changed, then
the
* declaration of private data member
"accountName"
* should have the reserved word "final"
added.
*
* The method withdraw() has been copied
across
* because:
* (1) it was provided in its entirety in
the
* "static" version of the
class,
* (2) to provide an example that "static" is
dropped
* from the header,
* (3) to provide a hint as to what changes
might be
* needed for the other,
incomplete methods.
*/
/**
* The withdrawal should be refused if the
withdrawal
* would result in a negative balance.
*
* @param amount The amount to be
withdrawn
*
* @return new balance or -1.0 if
withdrawal refused
*/
public returntype withdraw(double amount) {

if ( balance >= amount ) {
balance = balance -
amount;
return balance;
}
else
return -1.0;
}
/**
* Note that this is a static method. So a
change to the
* interest rate changes the interest rate for
all objects
* of this class.
*
* @param newInterest The
new interest rate
*/
public static returntype
setInterestRate(parametersIfAny) {
add whatever code is required here in
the method body
}
/**
* Note that this is a static
method.
*
* @return The interest rate
*/
public static returntype
getInterestRate(parametersIfAny) {
add whatever code is required here in
the method body
}
/**
* It is common practise to supply a "toString"
method
* in an object-oriented class. In fact,
if you don't
* explicitly supply such a method, Java
produces an
* implicit, simplistic "toString" method which
produces
* a String like "BankAccountOO@1edd1f0". The
word before
* the "@" is the name of the class. The
hexadecimal
* number after the "@" is called the objects
"hash code".
*
* Note: Method "toString" method is NOT
"static". It
* can't be static, since the values in the data
members
* may vary between objects of this class.
*
*@return The state of this "class
instance" or "object"
*/
public returntype toString(parametersIfAny) {
return "accountNumber = " +
accountNumber +
"
accountName = " + accountName +
" balance =
" + balance;
}
} // class BankAccountOO
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply