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:
Bank Accounts 02: Overriding 1
Building off of the Bank Accounts 01 practice assignment above,
in your CheckingAccount and SavingsAccount classes, override the
toString() method. Additionally include a call to
SimpleBankAccount's toString() method. Use the appropriate
annotation to designate that you're expecting this method to
override another method.
Submit the following files:
Bank Accounts 03: Overriding 2
Building off of the Bank Accounts 01 practice assignment above, add an equals() method that returns true if all of the fields match and false otherwise. The Object class has the following method:
public boolean equals( Object obj )
To override this method, you must have the same method header.
Additionally, to use the fields of the class that overrides the
method, you need to cast the parameter to the current class.
Your equals methods should work so that the following code will
execute, but not display anything:
/** * Exercises equals() * * @author Jo Belle * @version 0.1 (10/12/2020) */ public class BankAccounts03{ 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 ); CheckingAccount checkingCopy = new CheckingAccount( 100.0, "checking123" ); SavingsAccount savingsCopy = new SavingsAccount( 1000.0, "savings124", INTEREST_RATE ); if( checking.equals( checkingCopy ) == false ){ System.err.println("ERROR: The following objects are equal:"); System.err.println( checking ); System.err.println( checkingCopy ); } if( savings.equals( savingsCopy ) == false ){ System.err.println("ERROR: The following objects are equal:"); System.err.println( savings ); System.err.println( savingsCopy ); } int electricBillCheckNum = 2123; double electricBill = 60.34; double futureCar = 200.0; checking.processCheck( electricBillCheckNum, electricBill ); savings.deposit( futureCar ); savings.applyInterest( ); if( checking.equals( checkingCopy ) == true ){ System.err.println("ERROR: The following objects are NOT equal:"); System.err.println( checking ); System.err.println( checkingCopy ); } if( savings.equals( savingsCopy ) == true ){ System.err.println("ERROR: The following objects are NOT equal:"); System.err.println( savings ); System.err.println( savingsCopy ); } } }
Submit the following files:
Solution to the above problem is given below.
SimpleBankAccount.java
/**
* 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";
}
}
CheckingAccount.java
public class CheckingAccount extends SimpleBankAccount{
private int lastProcessedCheckNumber;
/**
*
*/
public CheckingAccount() {
super();
// TODO Auto-generated constructor stub
}
/**
* @param bal
* @param id
*/
public CheckingAccount(double bal, String id) {
super(bal, id);
// TODO Auto-generated constructor stub
}
/**
* @return the lastProcessedCheckNumber
*/
public int getLastProcessedCheckNumber() {
return lastProcessedCheckNumber;
}
/**
* @param lastProcessedCheckNumber the lastProcessedCheckNumber to set
*/
public void setLastProcessedCheckNumber(int lastProcessedCheckNumber) {
this.lastProcessedCheckNumber = lastProcessedCheckNumber;
}
public boolean processCheck( int checkNum, double amount ) {
if(checkNum == this.lastProcessedCheckNumber) {
return false;
}
this.lastProcessedCheckNumber = checkNum;
return true;
}
@Override
public String toString() {
return "CheckingAccount [lastProcessedCheckNumber=" + lastProcessedCheckNumber + "]";
}
}
SavingsAccount.java
public class SavingsAccount extends SimpleBankAccount{
private double interestRate;
/**
*
*/
public SavingsAccount() {
super();
// TODO Auto-generated constructor stub
}
/**
* @return the interestRate
*/
public double getInterestRate() {
return interestRate;
}
/**
* @param interestRate the interestRate to set
*/
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
/**
* @param bal
* @param id
*/
public SavingsAccount(double bal, String id, double interestRate) {
super(bal, id);
this.interestRate = interestRate;
}
// method to apply interest rate
public double applyInterest() {
return super.getBalance() + super.getBalance()*interestRate;
}
@Override
public String toString() {
return "SavingsAccount [interestRate=" + interestRate + "]";
}
}
After making BankAccount02 modifications:
SimpleBankAccount.java
/**
* 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";
}
}
CheckingAccount.java
public class CheckingAccount extends SimpleBankAccount{
private int lastProcessedCheckNumber;
/**
*
*/
public CheckingAccount() {
super();
// TODO Auto-generated constructor stub
}
/**
* @param bal
* @param id
*/
public CheckingAccount(double bal, String id) {
super(bal, id);
// TODO Auto-generated constructor stub
}
/**
* @return the lastProcessedCheckNumber
*/
public int getLastProcessedCheckNumber() {
return lastProcessedCheckNumber;
}
/**
* @param lastProcessedCheckNumber the lastProcessedCheckNumber to set
*/
public void setLastProcessedCheckNumber(int lastProcessedCheckNumber) {
this.lastProcessedCheckNumber = lastProcessedCheckNumber;
}
public boolean processCheck( int checkNum, double amount ) {
if(checkNum == this.lastProcessedCheckNumber) {
return false;
}
this.lastProcessedCheckNumber = checkNum;
return true;
}
@Override
public String toString() {
return super.toString()+"CheckingAccount [lastProcessedCheckNumber=" + lastProcessedCheckNumber + "]";
}
}
SavingsAccount.java
public class SavingsAccount extends SimpleBankAccount{
private double interestRate;
/**
*
*/
public SavingsAccount() {
super();
// TODO Auto-generated constructor stub
}
/**
* @return the interestRate
*/
public double getInterestRate() {
return interestRate;
}
/**
* @param interestRate the interestRate to set
*/
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
/**
* @param bal
* @param id
*/
public SavingsAccount(double bal, String id, double interestRate) {
super(bal, id);
this.interestRate = interestRate;
}
// method to apply interest rate
public double applyInterest() {
return super.getBalance() + super.getBalance()*interestRate;
}
@Override
public String toString() {
return super.toString()+ "SavingsAccount [interestRate=" + interestRate + "]";
}
}
Output BankAccount02:
Output for AccountsDriver class:
Output for BankAccount03:
End of Solution