I'm creating a java project named Bank. SavingsAccount and CurrentAccount classes are subclasses of Account class. In a class Bank, which stores an information about accounts I have to create a method which prints the sum of balance amount for all SAVINGS accounts. How should I write the method to sum exactly a balance of savings accounts, not of both savings and current accounts?
import java.util.ArrayList;
public class Bank
{
private ArrayList<Account> accounts;
public Bank()
{
super();
accounts = new ArrayList<Account>();
}
public void addAccount(Account theAccount)
{
accounts.add(theAccount);
}
public void getDescription()
{
for(Account account: accounts)
{
System.out.println("Name: " + account.getOwnerName() + "\nCode: " + account.getCode() + "\nBalance: " + account.getBalance());
}
}
public double accountsSum()
{
double total = 0;
for(Account acc: accounts)
{
total += acc.getBalance();
}
return total;
}
public void printSavingsBalance()
{
//?????
}
}
public class Account
{
protected String code;
protected String ownerName;
protected double balance;
public Account(String code,String ownerName, double balance)
{
this.code = code;
this.ownerName = ownerName;
this.balance = balance;
}
public String getCode(){return code;}
public String getOwnerName(){return ownerName;}
public double getBalance(){return balance;}
public void setBalance(double newBalance)
{
balance = newBalance;
}
public void addMoney(double plusMoney)
{
balance += plusMoney;
}
public void withdrawMoney(double minusMoney)
{
balance -= minusMoney;
}
}
public class SavingsAccount extends Account
{
private int term;
private double interestRate;
public SavingsAccount(int term, double interestRate, String code, String ownerName, double balance)
{
super(code,ownerName,balance);
this.term = term;
this.interestRate = interestRate;
}
public int getTerm(){return term;}
public double getInterestRate(){return interestRate;}
public void setTerm(int newTerm)
{
term = newTerm;
}
public void setInterestRate(double newInterestRate)
{
interestRate = newInterestRate;
}
public double interestSize()
{
return balance*interestRate/365*term;
}
}