Java 查找银行帐户的总额
我是一名刚学完java第一年的学生,在一次考试中遇到了一个问题“创建一个返回银行存款总额的方法?”。我已经包括了下面的课程。我得到了大多数正确的方法,包括addAccount()、getBalance()、draw()等,但不确定这个方法的答案是什么。这可能很简单,而且比我想象的要简单(我必须使用一个for循环或两个类似的循环),但只是为了澄清合计的正确方法。这也出现在一家杂货店的c#分配中,在那里,顾客从不同的产品(如水果、蔬菜等)购买商品,并且必须计算总金额 提前谢谢你 保罗 代码: 超类:Java 查找银行帐户的总额,java,methods,arraylist,subclass,superclass,Java,Methods,Arraylist,Subclass,Superclass,我是一名刚学完java第一年的学生,在一次考试中遇到了一个问题“创建一个返回银行存款总额的方法?”。我已经包括了下面的课程。我得到了大多数正确的方法,包括addAccount()、getBalance()、draw()等,但不确定这个方法的答案是什么。这可能很简单,而且比我想象的要简单(我必须使用一个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;
}
}
你在正确的轨道上思考一个循环。把余额加起来就行了
public static double totalBalance(Collection<BankAccount> accounts){
double sum = 0.0;
for(BankAccount b : accounts){
sum += b.getBalance();
}
return sum;
}
公共静态双总余额(收款账户){
双和=0.0;
用于(银行账户b:账户){
sum+=b.getBalance();
}
回报金额;
}
你说得对
如果您有一组银行账户或其子类,您可以在for循环中对它们进行简单求和
假设您有这样的东西(我假设有一个Bank对象,其中包含一个函数,该函数以某种方式为我提供所有帐户):
Collection accounts=Bank.getAccounts();
双和=0.0;
用于(银行账户:账户){
sum+=account.getBalance();
}
Hmm,我希望getBalance()方法总是能为帐户生成正确的balnce,而addInterest()和deccutfees()方法则会被BankController等频繁调用。如果您每次检查余额时都使用它们,它们将更改余额,余额将取决于您检查余额的次数。我觉得这不对……你可能是对的。调用debutctfees()通常不会有什么坏处,因为如果没有更多的交易,它不会改变余额。但是,我现在看到AddInTest总是添加相同数量的利息,而不管上次调用它后经过了多少“时间”。我会去掉那个部分。谢谢你们,谢谢你们给了我一些示例代码,我很高兴知道我的思路是正确的,但有第二个意见也很好。
Collection<BankAccount> accounts = Bank.getAccounts();
Double sum = 0.0;
for (BankAccount account : accounts) {
sum += account.getBalance();
}