我正在创建一个名为Bank的java项目。 SavingsAccount和CurrentAccount类是Account类的子类。在存储有关帐户信息的类银行中,我必须创建一个方法,用于打印所有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;
}
}
答案 0 :(得分:2)
您可以使用instanceof来确定对象的类型,并仅在类型为Savings时求和。
int totalBalance = 0;
for(Account account: accounts){
if (account instanceof Saving) {
totalBalance = totalBalance + account.getBalance();
}
}
答案 1 :(得分:1)
private List<SavingsAccount> savingsAccounts;
private List<CurrentAccount> currentAccounts;
public addAccount(SavingsAccount account){
}
public addAccount(CurrentAccount account){
}
这样做会好得多。然后只需迭代储蓄账户并添加所有账户的余额。引入accountType
和instanceof
检查将违反多态性恕我直言。
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;
}
}
答案 2 :(得分:0)
向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个列表 - 取决于你想对帐户和列表做什么
答案 3 :(得分:0)
如果您在考虑使用instanceof
时感到心烦意乱,请在您的超类中添加一个虚拟方法,并在每个子类中进行定义:
Savings:
String accountType() {
return "Savings";
}
Checking:
String accountType() {
return "Checking";
}