import java.awt.HeadlessException;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class Main {
/**
* Design an abstract class named BankAccount to hold the following data for a bank account:
• Balance
• Number of deposits this month
• Number of withdrawals
• Annual interest rate
• Monthly service charges
The class should have the following methods:
// See Book
*
* Next, design a SavingsAccount class that extends the BankAccount class. The
SavingsAccount class should have a status field to represent an active or inactive account. If the
balance of a savings account falls below $25, it becomes inactive. (The status field could be a
boolean variable.) No more withdrawals may be made until the balance is raised above $25, at
which time the account becomes active again. The savings account class should have the following
methods:
// See Book
*
*
*/
public static class SavingsAccount extends BankAccount {
protected boolean isActive = (super.balance > 25);
// Constructors
public SavingsAccount() {
super();
}
public SavingsAccount(double balance, double annualInterest) {
super(balance, annualInterest);
}
public double getMonthlyCharge() {
this.monthlyProcess();
return monthlyCharge;
}
// Monthly charge process.
public void monthlyProcess() {
if (super.numberOfWithdrawals > 4) {
super.monthlyCharge += super.numberOfWithdrawals - 4;
if (super.balance < 25) {
this.isActive = false;
}
}
}
// Withdraw.
public boolean withdraw(double amount) {
if (isActive) {
return super.withdraw(amount);
} else {
return false;
}
}
// Deposit
public boolean deposit(double amount) {
super.deposit(amount);
if ((!this.isActive) && super.balance > 25) {
this.isActive = true;
return true;
} else {
return false;
}
}
public boolean getIsActive() {
return isActive;
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
}
public static abstract class BankAccount {
protected double balance;
protected int numberOfDeposits;
protected int numberOfWithdrawals;
protected double annualInterest;
protected double monthlyCharge;
// Constructor accepts annual interest and
// the balance.
public BankAccount(double balance, double annualInterest) {
this.setAnnualInterest(annualInterest);
this.setBalance(balance);
}
// Default constructor.
public BankAccount() {}
// Monthly charge process.
public void monthlyProcess() {
this.balance -= this.monthlyCharge;
this.calcInterest();
this.monthlyCharge = 0;
this.numberOfDeposits = 0;
this.numberOfWithdrawals = 0;
}
// Calculate the interest.
protected void calcInterest() {
double monthlyInterestRate;
double monthlyInterest;
monthlyInterestRate = this.annualInterest / 12;
monthlyInterest = this.balance * monthlyInterestRate;
this.balance = this.balance + monthlyInterest;
}
// Withdraw.
public boolean withdraw(double amount) {
if ((this.balance > amount) && (amount > 0)) {
this.balance -= amount;
this.numberOfWithdrawals++;
return true;
} else {
return false;
}
}
// Deposit
public boolean deposit(double amount) {
if (amount > 0) {
this.balance += amount;
this.numberOfDeposits++;
return true;
} else {
return false;
}
}
// Accessors and mutators.
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public int getNumDepositsThisMonth() {
return numberOfDeposits;
}
public void setNumDepositsThisMonth(int numDepositsThisMonth) {
this.numberOfDeposits = numDepositsThisMonth;
}
public double getAnnualInterest() {
return annualInterest;
}
public void setAnnualInterest(double annualInterest) {
this.annualInterest = annualInterest;
}
public double getMonthlyCharge() {
return monthlyCharge;
}
public void setMonthlyCharge(double monthlyCharge) {
this.monthlyCharge = monthlyCharge;
}
public boolean getIsActive() {
// TODO Auto-generated method stub
}
}
public static void main
(String[] args
) { double balance = 0;
double annualInterest = 0;
do {
try {
output = "Enter your account balance $:";
balance
= Double.
parseDouble(input
); if (balance < 0) {
}
break;
} catch (HeadlessException e) {
errorMsg();
errorMsg();
}
} while (true);
do {
try {
output = "Please enter your annual interest rate: \n"
+ "EXAMPLE: A 4% interst rate is 0.04";
annualInterest
= Double.
parseDouble(input
); if (annualInterest < 0) {
}
break;
} catch (HeadlessException e) {
errorMsg();
errorMsg();
}
} while (true);
// Instantiate your object.
BankAccount savings = new SavingsAccount(balance, annualInterest);
do {
try {
output = "Enter the number of withdrawals:";
savings.
numberOfWithdrawals = Integer.
parseInt(input
); if (savings.numberOfWithdrawals < 0) {
}
break;
} catch (HeadlessException e) {
errorMsg();
errorMsg();
}
} while (true);
do {
try {
output = "Enter the number of deposits:";
savings.
numberOfDeposits = Integer.
parseInt(input
); if (savings.numberOfDeposits < 0) {
}
break;
} catch (HeadlessException e) {
errorMsg();
errorMsg();
}
} while (true);
// Add the interest to the balance:
savings.calcInterest();
output = "Account Balance with Interest: $" + df.format(savings.getBalance());
output += "\nMonthly Charge that will be deducted: $" + df.format(savings.getMonthlyCharge());
output += "\nAccount Status: ";
output += savings.getIsActive() ? "Is Active" : "Not Active";
}
private static void errorMsg() {
output = "Error: There was an error with your entry";
}
}