In: Computer Science
JAVA LANGUAGE
Create a new project named BankAccount in Eclipse. Then,
implement the following requirements. You need to create two
classes, one for BankAccount (BankAccount.java) and the other for
the tester (BankAccountTest.java).
Make sure your program compile without errors before submitting.
Submit .java files to eCampus by the end of next Thursday,
10/15/2020.
Part 1: Create the BankAccount class (template is attached after the project description) in the project. You must add the following to the BankAccount class:
An instance variable for bank account number: accountNum
The long getAccountNum() method to get the account number
The void setAccountNum(long acctnum) method to set the account
number
An instance variable for bank account owner’s name: name
The String getAccountName() method to get the account owner’s
name
The void setAccountName(String acctname) method to set the account
owner’s name
Another instance variable for account overdrawn status:
overdrawn
The boolean isOverdrawn() method to get the account overdrawn
status
The void setOverdrawn(boolean status) method to set the account
overdrawn status
A constructor with four parameters: name, account number, the
initial balance, and the overdrawn status to set the initial values
for the instance variables when an object is created
Modify the withdraw() and deposit() methods
An account will be overdrawn if a withdrawal results in a negative
balance.
An account that was overdrawn will not be overdrawn anymore if a
deposit results in a positive balance or 0.
The deposit and withdraw methods must verify the amount to deposit
(or to withdraw). If the amount is negative, the methods must
printout "Negative amount!" and the balance of the account will not
be changed.
Add a method called transfer(double amount, BankAccount
otherAccount)
The transfer method is to transfer some money from the account to
another account specified by otherAccount.
Add a method addInterest ( ) to add interest to the account and
change the balance
use a constant to represent the interest rate 3.5%
Add the toString() method to represent an account in the following
format:
“The Account Number xxx is owned by xxx with a Balance xxx. It is
(not) overdrawn.”
Here xxx needs to replace by the values of the account. If it is
overdrawn, show
overdrawn. Otherwise, show not overdrawn.
Part 2: Create a BankAccountTest class with only the main method in the BankAccount project to test the BankAccount class. You must add the following to the BankAccountTest class:
Create a BankAccount object named harrysChecking with the
initial values: name as "Harry", accoutNum as 12345, balance as
0.00, and overdrawn as false
Print out the object harrysChecking by calling toString()
method
Call the deposit method to deposit $200 to harrysChecking and then
print out the current balance and overdrawn status of
harrysChecking
Call the withdraw method to withdraw $500 from harrysChecking and
then print out the current balance and overdrawn status of
harrysChecking
Again call the deposit method to deposit $400 to harrysChecking and
then print out the current balance and overdrawn status of
harrysChecking
Create a BankAccount object named harrysSaving with the initial
values: name as "Harry", accoutNum as 86754, balance as 500.00, and
overdrawn as false
Print out the object harrysSaving by calling toString()
method
Call the transfer method to transfer $50 from harrysChecking to
harrysSaving
Print out the current balances of harrysChecking and harrysSaving,
respectively
Call the addInterest method to add interest to harrysSaving and
Print out information for harrysSaving by calling toString()
method
/**
A bank account has a balance that can be changed by deposits,
withdrawals, transfers, and interests.
*/
public class BankAccount {
private double balance;
/**
Constructs a bank account with a zero balance
*/
public BankAccount() {
balance=0;
}
/**
Constructs a bank account with a given balance
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance) {
balance=initialBalance;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit (double amount) {
balance=balance+amount;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw.
*/
public void withdraw (double amount) {
balance=balance-amount;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance() {
return balance;
}
}
public class BankAccountTest {
public static void main(String[] args) {
}
}
Java program with comments
BankAccount.java
/**
A bank account has a balance that can be changed by deposits,
withdrawals, transfers, and interests.
*/
public class BankAccount {
private double balance;
//An instance variable for bank account number: accountNum
private long accountNum;
//An instance variable for bank account owner’s name: name
private String name;
//Another instance variable for account overdrawn status:
overdrawn
private boolean overdrawn;
//use a constant to represent the interest rate 3.5%
public static final double RATE = 3.5;
/**
Constructs a bank account with a zero balance
*/
public BankAccount() {
balance=0;
}
/**
Constructs a bank account with a given balance
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance) {
balance=initialBalance;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit (double amount) {
//The deposit and withdraw methods
must verify the amount to deposit (or to withdraw).
//If the amount is negative, the methods must printout "Negative
amount!" and the balance of the account will not be changed.
if(amount <= 0)
{
System.out.println("Negative
amount!");
return;
}
balance=balance+amount;
//An account that was overdrawn will not be overdrawn anymore if a
deposit results in a positive balance or 0.
if(isOverdrawn() && balance >= 0)
setOverdrawn(false);
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw.
*/
public void withdraw (double amount) {
//The deposit and withdraw methods must verify the
amount to deposit (or to withdraw).
//If the amount is negative, the methods must printout "Negative
amount!" and the balance of the account will not be changed.
if(amount <= 0)
{
System.out.println("Negative
amount!");
return;
}
balance=balance-amount;
//An account will be overdrawn if a withdrawal results in a
negative balance.
if(balance < 0)
setOverdrawn(true);
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance() {
return balance;
}
//The long getAccountNum() method to get the account number
long getAccountNum()
{
return accountNum;
}
//The void setAccountNum(long acctnum) method to set the account
number
void setAccountNum(long ac)
{
accountNum = ac;
}
//The String getAccountName() method to get the account owner’s
name
String getAccountName()
{
return name;
}
//The void setAccountName(String acctname) method to set the
account owner’s name
void setAccountName(String acctname)
{
name = acctname;
}
//The boolean isOverdrawn() method to get the account overdrawn
status
boolean isOverdrawn()
{
return overdrawn;
}
//The void setOverdrawn(boolean status) method to set the account
overdrawn status
void setOverdrawn(boolean status)
{
overdrawn = status;
}
//A constructor with four parameters: name, account number, the
initial balance, and the overdrawn status
//to set the initial values for the instance variables when an
object is created
BankAccount(String n, long acn, double bal, boolean status)
{
setAccountName(n);
setAccountNum(acn);
balance = bal;
setOverdrawn(status);
}
//Add a method called transfer(double amount, BankAccount
otherAccount)
void transfer(double amount, BankAccount otherAccount)
{
//The transfer method is to transfer some money from the account to
another account specified by otherAccount.
withdraw(amount);
otherAccount.deposit(amount);
}
//Add a method addInterest ( ) to add interest to the account and
change the balance
void addInterest()
{
double interest;
interest = RATE / 100 * getBalance();
deposit(interest);
}
//Add the toString() method to represent an account in the
following format:
// “The Account Number xxx is owned by xxx with a
Balance xxx. It is (not) overdrawn.”
// Here xxx needs to replace by the values of the
account. If it is overdrawn, show
// overdrawn. Otherwise, show not overdrawn.
public String toString()
{
String result="";
result += "The Account Number ";
result += getAccountNum();
result += " is owned by ";
result += getAccountName();
result += " with a Balance ";
result += getBalance();
result += ". It is ";
if(!isOverdrawn())
result += "(not) ";
result += "overdrawn.";
return result;
}
}
BankAccountTest.java
public class BankAccountTest {
public static void main(String[] args) {
//Create a BankAccount object named harrysChecking
with the initial values:
//name as "Harry", accoutNum as 12345, balance as
0.00, and overdrawn as false
BankAccount harrysChecking = new BankAccount("Harry",
12345, 0.00, false);
//Print out the object harrysChecking by calling
toString() method
System.out.println(harrysChecking.toString());
//Call the deposit method to deposit $200 to
harrysChecking and
//then print out the current balance and overdrawn
status of harrysChecking
harrysChecking.deposit(200);
System.out.println("Current balance: "+
harrysChecking.getBalance());
System.out.println("Overdrawn Status: "+
harrysChecking.isOverdrawn());
//Call the withdraw method to withdraw $500 from
harrysChecking and then
//print out the current balance and overdrawn status
of harrysChecking
harrysChecking.withdraw(500);
System.out.println("Current balance: "+
harrysChecking.getBalance());
System.out.println("Overdrawn Status: "+
harrysChecking.isOverdrawn());
//Again call the deposit method to deposit $400 to
harrysChecking and then
//print out the current balance and overdrawn status
of harrysChecking
harrysChecking.deposit(400);
System.out.println("Current balance: "+
harrysChecking.getBalance());
System.out.println("Overdrawn Status: "+
harrysChecking.isOverdrawn());
//Create a BankAccount object named harrysSaving with
the initial values:
//name as "Harry", accoutNum as 86754, balance as
500.00, and overdrawn as false
BankAccount harrysSaving = new BankAccount("Harry",
86754, 500.00, false);
//Print out the object harrysSaving by calling
toString() method
System.out.println(harrysSaving.toString());
//Call the transfer method to transfer $50 from
harrysChecking to harrysSaving
harrysChecking.transfer(50, harrysSaving);
//Print out the current balances of harrysChecking and
harrysSaving, respectively
System.out.println("Current balance harrysChecking: "+
harrysChecking.getBalance());
System.out.println("Current balance harrysSaving: "+
harrysSaving.getBalance());
//Call the addInterest method to add interest to
harrysSaving and Print out information for harrysSaving by calling
toString() method
harrysSaving.addInterest();
System.out.println(harrysSaving.toString());
}
}
Sample I/O and Output: