In: Computer Science
This is in JAVA
Bank Accounts 01: Child Classes
Copy the following SimpleBankAccount class and use it as a base class:
/** * Simple representation of a bank account * * @author Jo Belle * @version 0.5 (10/12/2020) */ import java.text.NumberFormat; public class SimpleBankAccount{ // fields (instance variables) private double balance; private String accountId; /** * Constructor for objects of class SimpleBankAccount */ public SimpleBankAccount(){ balance = 0.0; accountId = ""; } /** * Constructor for objects of class SimpleBankAccount */ public SimpleBankAccount( double bal, String id ){ balance = bal; accountId = id; } /** * Add money to the balance * * @param amount the amount to deposit * @return void */ public void deposit( double amount ){ balance += amount; } /** * Remove money from the balance * * @param amount the amount to withdraw * @return true (success) or false (failure) */ public boolean withdraw( double amount ){ if( balance - amount >= 0 ){ balance -= amount; return true; }else{ return false; } } /** * Get the balance * * @return the balance */ public double getBalance(){ return balance; } /** * Set account ID * * @param the account ID */ public void setAccountId(String id){ accountId = id; } /** * Get the account ID * * @return the account ID */ public String getAccountId(){ return accountId; } /** * Produces a string represenation of the balance * @return The balance (with a label) */ public String toString( ){ // display balance as currency String balanceStr = NumberFormat.getCurrencyInstance().format( balance ); return "Balance for account " + accountId + ": " + balanceStr + "\n"; } }
Include at least two classes: CheckingAccount and SavingsAccount. Save your CheckingAccount class in a file named CheckingAccount.java and your SavingsAccount class in a file named SavingsAccount.java. Your CheckingAccount class needs to add a field to track the last processed check number. Also include both a no-argument constructor and a parameterized constructor (that takes a double and a String). Furthermore, include the following method:
public boolean processCheck( int checkNum, double amount );
which returns false if checkNum has the same check number as the
last check processed, otherwise it reduces the balance by amount
and returns true.
Your SavingsAccount class needs to have a field for the interest
rate. Also include both a constructor that just takes the interest
rate (as a double) and a parameterized constructor (that takes a
double, String and a double). Furthermore, include an
applyInterest() method that multiples the current balance by the
interest rate, and adds that to the balance.
The following code should work and produce the output below:
/** * Exercises the basic functionality of a Checking and SavingsAccount * * @author Jo Belle * @version 0.3 (10/12/2020) */ public class AccountsDriver{ final public static double INTEREST_RATE = 0.01; // 1% public static void main( String[] args ){ CheckingAccount checking = new CheckingAccount( 100.0, "checking123" ); SavingsAccount savings = new SavingsAccount( 1000.0, "savings124", INTEREST_RATE ); double monthlyExpenses = 756.34; int electricBillCheckNum = 2123; double electricBill = 60.34; int registationCheckNum = 2124; double registration = 50.00; double dinnerMoney = 55.32; double futureCar = 200.0; double textbook = 90.0; // checking account transactions checking.deposit( monthlyExpenses ); checking.processCheck( electricBillCheckNum, electricBill ); checking.withdraw( dinnerMoney ); checking.processCheck( registationCheckNum, registration ); System.out.print( checking.toString() ); System.out.println( ); // savings account transactions savings.deposit( futureCar ); savings.applyInterest( ); savings.withdraw( textbook ); System.out.print( savings.toString() ); System.out.println( ); } }
Output:
Checking Account: Balance for account checking123: $690.68 Last processed check number: 2124 Savings Account: Balance for account savings124: $1,122.00 APR: 1.0%
Make just the necessary changes to the code in
SimpleBankAccount to complete the instructions.
Submit the following files:
// SimpleBankAccount.java
/**
* Simple representation of a bank account
*
* @author Jo Belle
* @version 0.5 (10/12/2020)
*/
import java.text.NumberFormat;
public class SimpleBankAccount{
// make balance protected, so that it is directly
accessible to the child classes
protected double balance;
private String accountId;
/**
* Constructor for objects of class SimpleBankAccount
*/
public SimpleBankAccount(){
balance = 0.0;
accountId = "";
}
/**
* Constructor for objects of class SimpleBankAccount
*/
public SimpleBankAccount( double bal, String id ){
balance = bal;
accountId = id;
}
/**
* Add money to the balance
*
* @param amount the amount to deposit
* @return void
*/
public void deposit( double amount ){
balance += amount;
}
/**
* Remove money from the balance
*
* @param amount the amount to withdraw
* @return true (success) or false (failure)
*/
public boolean withdraw( double amount ){
if( balance - amount >= 0 ){
balance -= amount;
return true;
}else{
return false;
}
}
/**
* Get the balance
*
* @return the balance
*/
public double getBalance(){
return balance;
}
/**
* Set account ID
*
* @param the account ID
*/
public void setAccountId(String id){
accountId = id;
}
/**
* Get the account ID
*
* @return the account ID
*/
public String getAccountId(){
return accountId;
}
/**
* Produces a string represenation of the balance
* @return The balance (with a label)
*/
public String toString( ){
// display balance as currency
String balanceStr = NumberFormat.getCurrencyInstance().format(
balance );
return "Balance for account " + accountId + ": " + balanceStr +
"\n";
}
}
//end of SimpleBankAccount.java
// CheckingAccount.java
public class CheckingAccount extends SimpleBankAccount
{
// field to track the last processed check
number
private int last_processed_check;
// default constructor
public CheckingAccount()
{
super();
last_processed_check = -1; // set
it to -1, since all check number >= 0
}
// parameterized constructor
public CheckingAccount(double bal, String id)
{
super(bal, id);
last_processed_check = -1; // set
it to -1, since all check number >= 0
}
/*
* returns false if checkNum has the same check number
as the last check processed,
* otherwise it reduces the balance by amount and
returns true.
*/
public boolean processCheck( int checkNum, double
amount )
{
// checkNum is not same as last
processed check
if(checkNum !=
last_processed_check)
{
balance -=
amount; // reduce balance by amount
last_processed_check = checkNum; // set last_processed_check to
checkNum
return
true;
}
return false; // checkNum and
last_processed_check are equal
}
// override toString method to return string
representation of CheckingAccount
public String toString()
{
return "Checking
Account:\n"+super.toString() + "Last processed check number:
"+last_processed_check+"\n";
}
}
//end of CheckingAccount.java
// SavingsAccount.java
public class SavingsAccount extends SimpleBankAccount
{
// field for the interest rate
private double interest_rate;
// 1-argument constructor
public SavingsAccount(double rate)
{
super();
interest_rate = rate;
}
// 3-argument constructor
public SavingsAccount(double bal, String id, double
rate )
{
super(bal, id);
interest_rate = rate;
}
// method that multiples the current balance by the
interest rate, and adds that to the balance.
public void applyInterest()
{
balance +=
(balance*interest_rate);
}
// return String representation of
SavingsAccount
public String toString()
{
return "Savings
Account:\n"+super.toString()+"APR: "+(interest_rate*100)+"%";
}
}
//end of SavingsAccount.java
// AccountsDriver.java
/**
* Exercises the basic functionality of a Checking and
SavingsAccount
*
* @author Jo Belle
* @version 0.3 (10/12/2020)
*/
public class AccountsDriver {
final public static double INTEREST_RATE = 0.01; // 1%
public static void main(String[] args) {
CheckingAccount checking = new
CheckingAccount( 100.0, "checking123" );
SavingsAccount savings = new
SavingsAccount( 1000.0, "savings124", INTEREST_RATE );
double monthlyExpenses =
756.34;
int electricBillCheckNum =
2123;
double electricBill = 60.34;
int registationCheckNum =
2124;
double registration = 50.00;
double dinnerMoney = 55.32;
double futureCar = 200.0;
double textbook = 90.0;
// checking account
transactions
checking.deposit( monthlyExpenses
);
checking.processCheck(
electricBillCheckNum, electricBill );
checking.withdraw( dinnerMoney
);
checking.processCheck(
registationCheckNum, registration );
System.out.print(
checking.toString() );
System.out.println( );
// savings account
transactions
savings.deposit( futureCar );
savings.applyInterest( );
savings.withdraw( textbook );
System.out.print(
savings.toString() );
System.out.println( );
}
}
//end of AccountsDriver.java
Output: