【问题标题】:how to write a method to sum exactly abalance of savings account, not of both savings and current accounts in bank?如何编写一种方法来准确计算储蓄账户的余额,而不是银行储蓄账户和活期账户的余额?
【发布时间】:2013-12-12 16:42:55
【问题描述】:

我正在创建一个名为 Bank 的 java 项目。 SavingsAccount 和 CurrentAccount 类是 Account 类的子类。在存储有关帐户信息的 Bank 类中,我必须创建一个方法来打印所有 SAVINGS 帐户的余额总和。我应该如何编写方法来准确总结储蓄账户的余额,而不是储蓄账户和活期账户?

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;
    }
}

【问题讨论】:

  • 也许您的 Bank 类中只有两个数组列表,每个帐户类型一个?
  • 我已经编辑了我的答案以提供另一种方法。

标签: java


【解决方案1】:

可以使用instanceof判断对象的类型,只有类型为Savings时才求和。

int totalBalance = 0;
for(Account account: accounts){
   if (account instanceof Saving) {
      totalBalance = totalBalance + account.getBalance();
   }
}

【讨论】:

  • 这完全反对多态性!!
  • @NarendraPathai - 但 ole Poly 是一个相当自负的角色。
  • instanceof 确实被认为是反多态的。另一种方法是使用覆盖,但在这种情况下它将不起作用。下面的海报给出了另一个选项。
  • 请查看替代方法的更新答案。欢迎任何cmets。
【解决方案2】:
private List<SavingsAccount> savingsAccounts;
private List<CurrentAccount> currentAccounts;

public addAccount(SavingsAccount account){

}

public addAccount(CurrentAccount account){

}

这样做会更好。然后只需遍历储蓄账户并添加所有账户的余额。引入 accountTypeinstanceof 检查将违反多态性恕我直言。


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;
    }
}

【讨论】:

  • @HotLicks 谁说这更简单?这是一种处理多态性的方法。 avajava.com/tutorials/lessons/visitor-pattern.html#!会有帮助的。
  • 为多态而多态简直是愚蠢的。您使用一种技术是因为它可以更快、更便宜地生成更好的代码,而不是因为它适合某些理论。
  • 您在上面引入了两个新类和大约 50 行代码,所有这些都是为了避免使用一个 instanceof。
  • @HotLicks 好吧,让我们同意不同意:) 但是我能问一下你是否对我投了反对票吗?我认为今天的 50 行代码明天就会有所回报。好吧,如果是这样,那么为什么不以程序方式编写整个代码。为什么要费心创建类?但这只是你做事的方式。
  • 嗯,我已经这样做了 40 年,而且我已经确定了自己的方式。当然,至少有 30 年我一直是适当的多态性的粉丝,但是,30 多年来,我已经接触到了非常多的不适当的多态性,以及所有其他流行语,例如 MVC、ERM 和 ECS,还有太多其他的数不胜数。
【解决方案3】:

accountType 字段添加到Account 类,可能是枚举类型(也可能是int 等)。然后在循环中检查帐户是否属于所需类型,然后再添加余额。

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

或使用@Narendra Pathai 建议的 2 个列表 - 取决于您还想对帐户和列表做什么

【讨论】:

  • 请查看替代方法的更新答案。欢迎任何cmets。
【解决方案4】:

如果您对使用 instanceof 的想法感到心烦意乱,请向您的超类添加一个虚拟方法,并在每个子类中定义它:

Savings:

String accountType() {
    return "Savings";
}

Checking:

String accountType() {
    return "Checking";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 2015-08-31
    • 1970-01-01
    • 2015-02-18
    • 2014-12-15
    • 2022-08-02
    • 1970-01-01
    相关资源
    最近更新 更多