银行账户交易的Java Math.max

时间:2012-10-03 20:10:39

标签: java

所以,我有这个问题,我一直在努力的目标是为银行账户中的每笔交易扣除超过分配数量的免费交易的费用。到目前为止,我已经掌握了计算交易的所有内容,但我们应该与Math.max合作,以便当您查看免费交易金额时,费用开始从余额中减去帐户。我在我的deductMonthlyCharge方法中使用Math.max。我想我知道如何用if和else语句来做这个,但是我们不允许在这里使用它们而且我对Math.max不是很熟悉。所以,如果有人能指出我正确的方向来解决这个问题,那就太好了。感谢。

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

/**
   Constructs a bank account with a zero balance
*/
public BankAccount()
{   
  balance = 0;
    fee = 5;
    freeTransactions = 5;
    transactionCount = 0;
}

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

public static void main(String [ ] args)
{
    BankAccount newTransaction = new BankAccount();
    newTransaction.deposit(30);
    newTransaction.withdraw(5);
    newTransaction.deposit(20);
    newTransaction.deposit(5);
    newTransaction.withdraw(5);
    newTransaction.deposit(10);
    System.out.println(newTransaction.getBalance());
    System.out.println(newTransaction.deductMonthlyCharge());

}
public void setTransFee(double amount)
{
    balance = amount+(balance-fee);
    balance = balance;

}

public void setNumFreeTrans(double amount)
{
    amount = freeTransactions;
}

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

/**
  Withdraws money from the bank account.
  @param amount the amount to withdraw
*/
public void withdraw(double amount)
{   
  double newBalance = balance - amount;
  balance = newBalance;
    transactionCount++;
}

public double deductMonthlyCharge()
{
    Math.max(transactionCount, freeTransactions);
    return transactionCount;
}

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

2 个答案:

答案 0 :(得分:2)

max(double, double)返回灌浆者双值。只需改变

Math.max(transactionCount, freeTransactions);
    return transactionCount;

return Math.max(transactionCount, freeTransactions);

如果您想要返回更大的值。

双打,就像所有原始类型都没有像对象一样的引用。您需要像double foo = functionThatReturnPrimitiveDouble()一样保存返回的值,或者像我上面的示例中那样再次返回它。

答案 1 :(得分:0)

我认为您想要这样的事情(假设每笔交易超过允许金额的费用为1.00美元):

public double deductMonthlyCharge()
{
    int transCount = Math.max(transactionCount, freeTransactions);
    double fee = 1.00 * (transCount - freeTransactions);
    return fee;
}

如果客户未超过允许的免费交易次数,则(transCount - freeTransactions)将为0,因此不会收取任何费用。

这段代码对自己的好处有点过于聪明,但我认为这就是古怪的要求(不要使用if语句,而不是使用max)要求。

更清楚(但相当)将是:

public double deductMonthlyCharge()
{
    if (transactionCount > freeTransactions) {
        return 1.00 * (transactionCount - freeTransactions);
    }
    return 0.0;
}