0

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;
    }
}
Derek
  • 952
  • 3
  • 13
  • 34
user3096253
  • 61
  • 1
  • 1
  • 5

4 Answers4

2

You can use the instanceof to determine the type of the object and sum only when the type is Savings.

int totalBalance = 0;
for(Account account: accounts){
   if (account instanceof Saving) {
      totalBalance = totalBalance + account.getBalance();
   }
}
Alex Theedom
  • 1,604
  • 15
  • 16
1
private List<SavingsAccount> savingsAccounts;
private List<CurrentAccount> currentAccounts;

public addAccount(SavingsAccount account){

}

public addAccount(CurrentAccount account){

}

This would be much better to do. Then just iterate over the savings accounts and add the balance of all accounts. Introducing accountType and instanceof checks would be against polymorphism IMHO.


A much cleaner approach with Visitor pattern:

public class Bank{

    private List<Account> accounts = new ArrayList<Account>();

    public void add(Account account) {
        accounts.add(account);
    }

    public double getBalance(){
        AdditionAccountVisitor visitor = new AdditionAccountVisitor();
        for(Account account : accounts){
            account.accept(visitor);
        }

        return visitor.getSum();
    }
}

abstract class Account{
    private double balance;

    public Account(double balance) {
        this.balance = balance;
    }

    public double getBalance(){
        return balance;
    }

    public abstract void accept(AccountVisitor visitor);
}

class SavingsAccount extends Account{

    public SavingsAccount(double balance) {
        super(balance);
    }

    @Override
    public void accept(AccountVisitor visitor) {
        visitor.visit(this);
    }

}

class CurrentAccount extends Account{

    public CurrentAccount(double balance) {
        super(balance);
    }

    @Override
    public void accept(AccountVisitor visitor) {
        visitor.visit(this);
    }
}

interface AccountVisitor{
    public void visit(SavingsAccount savingsAccount);
    public void visit(CurrentAccount savingsAccount);
}

class AdditionAccountVisitor implements AccountVisitor{
    private double sum = 0.0;

    @Override
    public void visit(SavingsAccount savingsAccount) {
        sum += savingsAccount.getBalance();
    }

    @Override
    public void visit(CurrentAccount savingsAccount) {
        //do nothing
    }

    public double getSum(){
        return sum;
    }
}
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
  • 1
    @HotLicks Who said this is simpler? This is a way to do with polymorphism. http://www.avajava.com/tutorials/lessons/visitor-pattern.html#! will be helpful. – Narendra Pathai Dec 12 '13 at 18:16
  • Polymorphism for polymorphism's sake is simply stupid. You use a technique because it produces better code faster and more cheaply, not because it suits some theory. – Hot Licks Dec 12 '13 at 18:19
  • Above you've introduced two new classes and about 50 lines of code, all to avoid one instanceof. – Hot Licks Dec 12 '13 at 18:21
  • 1
    @HotLicks Well lets agree to disagree :) But can I ask if you downvoted me? I think 50 lines of code today will pay tomorrow. Well if thats the case then why not just write whole code in a procedural way. Why bother creating classes? But that's just your way of doing things. – Narendra Pathai Dec 12 '13 at 18:25
  • Well, I've been doing this for 40 years and I'm kind of set in my ways. Of course at least 30 of those years I've been a fan of *appropriate* polymorphism, but then, over 30 years I've been exposed to an awful lot of *inappropriate* polymorphism, and all the other buzzwords like MVC and ERM and ECS and too many others to count. – Hot Licks Dec 12 '13 at 20:22
  • Very nice answer @NarendraPathai I didn't think of doing it this way. – Alex Theedom Dec 12 '13 at 20:28
0

add an accountType field to the Account class, possibly of type enumeration (but also int etc). Then in the loop check if the account is of the desired type before adding the balance.

public void printSavingsBalance()
{
    double total = 0;
    for(Account acc: accounts)
    {
        if (acc.getType() == SAVINGS)
            total += acc.getBalance();
    }
    return total; 
}

or use 2 lists as suggested by @Narendra Pathai - depends on what else you want to do with the accounts and list

msam
  • 4,259
  • 3
  • 19
  • 32
0

If you're emotionally distraught at the thought of using instanceof, add a virtual method to your superclass and define it thusly in each subclass:

Savings:

String accountType() {
    return "Savings";
}

Checking:

String accountType() {
    return "Checking";
}
Hot Licks
  • 47,103
  • 17
  • 93
  • 151