2012-10-03 131 views
0

因此,我遇到了这个问题,我一直在努力实现的目标是扣除银行账户中每笔交易的费用,这笔交易超过了分配数量的免费交易。到目前为止,我已经掌握了一切以计算交易,但我们应该与Math.max合作,以便在您结束免费交易时,费用开始从余额中扣除帐户。我在我的deductMonthlyCharge方法中使用Math.max。我想我知道如何用if和else语句来做到这一点,但我们不允许在这里使用它们,而且我对Math.max也不是很熟悉。所以,如果有人能指出我正确的方向来解决这个问题,那就太好了。谢谢。Java 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; 
} 
} 
+1

你会意识到,'Math.max()'返回一个值,对不对?这不会改变你传递给它的任何东西。 – NullUserException

+0

钱不应该创建或丢弃。钱只能转移。 –

回答

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

谢谢,我得到了一切工作,谢谢你。 虽然我确实有最后一个问题。我的问题是,如何将新的月份重置为0?我想我可以设置transactionCount = 0或transCount = 0,但我遇到了麻烦。如果真的那么简单,我会在哪里实现这一行代码? – user1696162

+0

@ user1696162最好的方法是向类中添加一个'setTransactionCount(int count)'方法。 –

2

max(double, double)返回grouter double值。如果你想返回的更大的价值只是改变

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

return Math.max(transactionCount, freeTransactions); 

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

+0

对,我明白了,并且感谢您的解释,但是如果返回值大于每月免费交易的金额,那么我将如何执行返回值并说: ,然后从交易中扣除费用 不使用if语句?这就是我感到困惑的地方。 – user1696162

+0

比尔刚刚给出了一个应该满足您的兴趣的答案。 我个人也会用if语句来处理这个案例,因为你的意图对读者来说更清楚。 所以事实证明'返回1.00 *(Math.max(transactionCount,freeTransactions) - freeTransactions)'应该工作;) – Zhedar