Java lab Design and write 3 classes – Account, CheckingAccount, and SavingsAccou
ID: 3887534 • Letter: J
Question
Java lab
Design and write 3 classes – Account, CheckingAccount, and SavingsAccount. Have CheckingAccount and SavingsAccount inherit from Account. In Account, you should include an account number, an account balance, a deposit and toString method, and an abstract withdraw method. Since deposits work the same way in both child classes, make sure they cannot override it. The constructor should only take an initial deposit amount to start along with generating an account number. The toString should display the account number and the current balance.
In the CheckingAccount class add a minimum balance requirement and overdraft fee. Implement the withdraw method so that overdrafts are allowed, but the overdraft fee is incurred if the balance drops below the minimum balance. Override the toString method to display everything the Account toString displays plus the minimum balance. Use the parent class toString to do most of the work.
In the SavingsAccount class add an annual interest rate and a method to recalculate the balance when the interest rate method is called. Since the interest rate is annual, make sure to calculate the interest accordingly. Override the toString method to display everything the Account toString displays plus the interest rate separately. Like the CheckingAccount toString, you should use the parent class to do most of the work. Also in the SavingsAccount class, if the current balance is less than $100.00, display an “account balance low” warning message along with the balance. Additionally, subtract $5.00 from the account balance as a fine for the low balance.
Create a driver class to instantiate and exercise each of the account types. Be sure that your driver uses descriptive output and tests each of the features of your account types. You can create a menu if you wish.
Explanation / Answer
Below is your code: -
Account.java
public abstract class Account {
private static int accountNO = 10000;
protected double accountBal;
Account(double initAmount) {
accountNO++;
this.accountBal = initAmount;
}
public void deposit(double depAmount) {
this.accountBal = this.accountBal + depAmount;
}
public String toString() {
return "Account NO = " + accountNO + " , Current Balance = " + this.accountBal;
}
public abstract void withdraw(double amount);
}
SavingsAccount.java
public class SavingsAccount extends Account {
SavingsAccount(double initAmount) {
super(initAmount);
}
double interestRate = 4.5;
public void addInterest() {
this.accountBal = this.accountBal + this.accountBal * interestRate / 100.0;
}
public void withdraw(double amount) {
this.accountBal -= amount;
if (this.accountBal < 100) {
System.out.println("account balance low");
this.accountBal -= 5;
}
}
public String toString() {
return super.toString() + " , interest rate = " + this.interestRate;
}
}
CheckingAccount.java
public class CheckingAccount extends Account {
CheckingAccount(double initAmount) {
super(initAmount);
}
private double minBal = 1000;
private double overdraftFee = 200;
public void withdraw(double amount) {
this.accountBal -= amount;
if (this.accountBal < minBal) {
this.accountBal -= overdraftFee;
}
}
public String toString() {
return super.toString() + " , minimum balance = " + this.minBal;
}
}
Driver.java
public class Driver {
public static void main(String[] args) {
System.out.println("Welcome to bank account");
System.out.println("Your Savings Account Details : ");
SavingsAccount savA = new SavingsAccount(100);
System.out.println(savA);
System.out.println("Withdrawing $5");
savA.withdraw(5);
savA.addInterest();
System.out.println("Account details after withdrawing and running annual interest");
System.out.println(savA);
System.out.println("Depositing $10");
savA.deposit(10);
System.out.println("Account details after depositing");
System.out.println(savA);
CheckingAccount checkA = new CheckingAccount(1005);
System.out.println("Your Checking Account Details : ");
System.out.println(checkA);
System.out.println("Withdrawing $10");
checkA.withdraw(10);
System.out.println("Account details after withdrawing");
System.out.println(checkA);
System.out.println("Depositing $10");
checkA.deposit(10);
System.out.println("Account details after depositing");
System.out.println(checkA);
}
}
Sample Run
Welcome to bank account
Your Savings Account Details :
Account NO = 10001 , Current Balance = 100.0 , interest rate = 4.5
Withdrawing $5
account balance low
Account details after withdrawing and running annual interest
Account NO = 10001 , Current Balance = 94.05 , interest rate = 4.5
Depositing $10
Account details after depositing
Account NO = 10001 , Current Balance = 104.05 , interest rate = 4.5
Your Checking Account Details :
Account NO = 10002 , Current Balance = 1005.0 , minimum balance = 1000.0
Withdrawing $10
Account details after withdrawing
Account NO = 10002 , Current Balance = 795.0 , minimum balance = 1000.0
Depositing $10
Account details after depositing
Account NO = 10002 , Current Balance = 805.0 , minimum balance = 1000.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.