2014-08-31 53 views
0

我是一个学生刚刚完成我的第一年学习Java,并有一个问题“创建一个方法,返回在银行总资金?”在考试中。我已经包括下面的类。我得到了大部分正确的方法,包括addAccount(),getBalance(),withdraw()等,但不确定这个答案是什么。这可能比我想象的简单并且更简单(我必须使用for循环或某种类型的两个循环),但只是为了澄清合计总和的正确方法。这也出现在一家杂货店的C#作业中,客户从不同的产品(例如水果蔬菜等)购买了货物,并且必须计算总计。找到一个银行账户的总数

预先感谢您...

保罗

代码: 超类:

/** 
    A bank account has a balance that can be changed by 
    deposits and withdrawals. 
*/ 
public class BankAccount 
{ 
    //Declare balance field 
    private double balance; 

    /** 
     Constructs a bank account with a zero balance. 
    */ 
    public BankAccount() 
    { 
     balance = 0; 
    } 

    /** 
     Constructs a bank account with a given balance. 
     @param initialBalance the initial balance 
    */ 
    public BankAccount(double initialBalance) 
    { 
     balance = initialBalance; 
    } 

    /** 
     Deposits money into the bank account. 
     @param amount the amount to deposit 
    */ 
    public void deposit(double amount) 
    { 
     balance = balance + amount; 
    } 

    /** 
     Withdraws money from the bank account. 
     @param amount the amount to withdraw 
    */ 
    public void withdraw(double amount) 
    { 
     if (balance >= amount) 
     { 
     balance = balance - amount; 
     } 
     else 
     { 
      System.out.println("Withdrawal error: insufficent funds"); 
     } 
    } 

    /** 
     Gets the current balance of the bank account. 
     @return the current balance 
    */ 
    public double getBalance() 
    { 
     return balance; 
    } 

    /** 
     Transfers money from the bank account to another account 
     @param amount the amount to transfer 
     @param other the other account 
    */ 
    public void transfer(double amount, BankAccount other) 
    { 
     withdraw(amount); 
     other.deposit(amount); 
    } 

    public String toString() 
    { 
     return "Your Balance: "+ balance; 
    } 
} 

子类的支票帐户:

/** 
    A checking account that charges transaction fees. 
*/ 
public class CheckingAccount extends BankAccount 
{ 
    private int transactionCount; 
    private int transaction; 

    private static final int FREE_TRANSACTIONS = 0; 
    private static final double TRANSACTION_FEE = 2.0; 

    /** 
     Constructs a checking account with a given balance. 
     @param initialBalance the initial balance 
    */ 
    public CheckingAccount(double initialBalance) 
    { 
     // Construct superclass 
     super(initialBalance); 

     // Initialize transaction count 
     transactionCount = 0; 
    } 

    public void deposit(double amount) 
    { 
     transactionCount++; 
     // Now add amount to balance 
     super.deposit(amount); 
    } 

    public void withdraw(double amount) 
    { 
     transactionCount++; 
     // Now subtract amount from balance 
     super.withdraw(amount); 
    } 

    /** 
     Deducts the accumulated fees and resets the 
     transaction count. 
    */ 
    public void deductFees() 
    { 
     if (transactionCount > FREE_TRANSACTIONS) 
     { 
     double fees = TRANSACTION_FEE * 
       (transactionCount - FREE_TRANSACTIONS); 
     super.withdraw(fees); 
     } 
     transaction = transactionCount; 
    } 

    public String toString() 
    { 

     return super.toString() + "\t Your Transactions: "+ transaction; 
    } 

} 

子类储蓄帐户:

/** 
    An account that earns interest at a fixed rate. 
*/ 
public class SavingsAccount extends BankAccount 
{ 
    private double interestRate; 

    /** 
     Constructs a bank account with a given interest rate. 
     @param rate the interest rate 
    */ 
    public SavingsAccount(double rate) 
    { 
     interestRate = rate; 
    } 

    /** 
     Constructs a bank account with a given interest rate. 
     @param rate the interest rate 
    */ 
    public SavingsAccount(double rate, double initBalance) 
    { 
     super(initBalance); 
     interestRate = rate; 
    } 

    /** 
     Adds the earned interest to the account balance. 
    */ 
    public void addInterest() 
    { 
     double interest = getBalance() * interestRate + 100; 
     deposit(interest); 
    } 

     public String toString() 
    { 

     return super.toString() + "\t Your Interest rate: "+ interestRate; 
    } 

} 

回答

0

你说得对。

如果您拥有BankAccounts或其子类的集合,那么可以在for循环中将它们简单地相加。

可以说,你有这样的事情(我假设有包含一个函数,给了我所有账户以某种方式银行对象):

Collection<BankAccount> accounts = Bank.getAccounts(); 
Double sum = 0.0; 
for (BankAccount account : accounts) { 
    sum += account.getBalance(); 
} 
+0

感谢那些人,并给我一些示例代码混乱,我很高兴知道我在正确的轨道上,但很高兴有第二个意见。 – Paul66 2014-08-31 18:45:49

+0

然后请接受答案;-) – mcdikki 2014-08-31 19:53:30

0

您正在思考一个循环的正确轨道。只需总结余额。

public static double totalBalance(Collection<BankAccount> accounts){ 
    double sum = 0.0; 
    for(BankAccount b : accounts){ 
     sum += b.getBalance(); 
    } 
    return sum; 
} 
+0

嗯,我期望为getBalance()方法总是会导致帐户的正确balnce,以及由Bankcontroler等经常调用的addInterest()和dectutFees()方法。如果您每次检查余额时都使用它们,则会改变余额,余额将取决于您检查余额的次数。这对我来说似乎是错误的... – mcdikki 2014-08-31 15:30:07

+0

你可能是对的。调用deductFees()通常不会受到伤害,因为如果没有更多事务,它不会更改余额。但是,我现在看到addInterest始终增加了相同的兴趣量,而不考虑自上次调用后已经过去的“时间”。我会摆脱那部分。 – Mshnik 2014-08-31 16:03:16

相关问题