我应该将BankAccount类中的monthlyProcess做为摘要。
我有一个父类BankAccount及其子类SavingsAccount。我把BankAccount类和它的monthlyProcess方法也做了抽象。我想我的问题是我不知道如何从SavingsAccount类中获取monthlyProcess方法来执行BankAccount类中的操作,因为我无法访问Savings类中所需的变量。
我尝试创建一个新的BankAccount构造函数,并将monthlyProcess方法内部的内容复制到新的构造函数中。
public abstract class BankAccount
{
// Will hold the account balance
private double accountBalance;
// will hold the number of deposits made
private int numberOfDeposits;
// Will hold number of withdrawals made
private int numberOfWithdrawals;
// will hold the annual interest rate
private double annualInterestRate;
// will hold the amount of the monthly service charge
private double monthlyServiceCharge;
// will hold the monthly interest rate
private double monthlyInterestRate;
// Will hold the amount of monthly interest
private double monthlyInterest;
// Constructor accepts arguments for the balance and interest rate
public BankAccount(double initialBalance, double interest)
{
accountBalance = initialBalance;
annualInterestRate = interest;
}
public BankAccount()
{
accountBalance = accountBalance - monthlyServiceCharge;
calcInterest();
numberOfWithdrawals = 0;
numberOfDeposits = 0;
monthlyServiceCharge = 0;
}
// Method will add the argument it take to the accountBalance variable
// The number of deposits is incremented
public void deposit(double deposit)
{
accountBalance += deposit;
numberOfDeposits++;
}
// The argument taken will be substracted form the account balance
// The number of withdrawals is also incremented
public void withdraw(double withdraw)
{
accountBalance -= withdraw;
numberOfWithdrawals++;
}
// Method which determines the interest calculations
public void calcInterest()
{
// Calculation for monthly interest rate
monthlyInterestRate = (annualInterestRate / 12);
//Calculation for monthly interst
monthlyInterest = accountBalance * monthlyInterestRate;
// Will hold the balance of the account added to the monthly interest
accountBalance = accountBalance + monthlyInterest;
}
public abstract void monthlyProcess();
/*// Method which determines amount how much interest is owed on account
public void monthlyProcess()
{
accountBalance = accountBalance - monthlyServiceCharge;
calcInterest();
numberOfWithdrawals = 0;
numberOfDeposits = 0;
monthlyServiceCharge = 0;
}*/
//Returns number of withdrawals made
public int getNumberOfWithdrawals()
{
return numberOfWithdrawals;
}
// Returns number of deposits made
public int getNumberOfDeposits()
{
return numberOfDeposits;
}
public void setMonthlyServiceCharge(double charge)
{
monthlyServiceCharge = charge;
}
//returns monthly service charge
public double getMonthlyServiceCharge()
{
return monthlyServiceCharge;
}
// Returns account balance
public double getBalance()
{
return accountBalance;
}
}
````
// SavingsAccount will inhert from BankAccount
public class SavingsAccount extends BankAccount
{
// boolean variable will be used to determine if account is active
private boolean status = true;
// Constrcutor will take an argument for the account balance and interest rate
public SavingsAccount(double initialBalance, double interest)
{
// Will inherit contructor from BankAccount class
super(initialBalance, interest);
// If statement used to determine if account is active
if(initialBalance < 25)
{
status = false;
}
}
// Withdraw methof will take away from the bank account balance
// if the balance falls below 25, the account is inactive
public void withdraw(double amount)
{
// If statement will determine if the active in order to withdraw
if(getBalance() < 25)
{
status = false;
System.out.println("Account Inactive: No Withdrawals can be made. ");
}
else
{
super.withdraw(amount);
}
}
// The deposit method will be used to add funds into the account.
// Once the account balance is above 25, it becomes active
public void deposit(double deposit)
{
// Determines if account is inactive or active after a deposit has been made
if(getBalance() < 25)
{
status = false;
}
else if(getBalance() > 25)
{
status = true;
}
// Calling the superclass version of the deposit method
super.deposit(deposit);
}
// Method will charge the service fee for having more than four withdrawals
public void monthlyProcess()
{
// If statement will be used to charge the account for having more than 4 withdrawals
if(getNumberOfWithdrawals() > 4)
{
setMonthlyServiceCharge((getMonthlyServiceCharge() + getNumberOfWithdrawals()) -4);
}
// Calling the superclass version of the monthlyProcess method
//super.monthlyProcess();
// will check to see if account is inactive
if(getBalance() < 25)
{
status = false;
}
//new SavingsAccount(new(BankAccount));
}
}