简体   繁体   English

银行账户交易的Java Math.max

[英]Java Math.max for Bank Account Transactions

So, I have this problem that I've been working on where the goal is to deduct a fee for each transaction in a bank account that goes over the allotted number of free transactions. 所以,我有这个问题,我一直在努力的目标是为银行账户中的每笔交易扣除超过分配数量的免费交易的费用。 So far I've got everything in place to count the transactions, but we're supposed to be working with Math.max to make it so that when you go over the free transaction amount, the fees start getting subtracted from the balance of the account. 到目前为止,我已经掌握了计算交易的所有内容,但我们应该与Math.max合作,以便当您查看免费交易金额时,费用开始从余额中减去帐户。 I'm using the Math.max in my deductMonthlyCharge method. 我在我的deductMonthlyCharge方法中使用Math.max。 I think I know how to do this with if and else statements, but we're not allowed to use them here and I'm not very familiar with Math.max. 我想我知道如何用if和else语句来做这个,但是我们不允许在这里使用它们而且我对Math.max不是很熟悉。 So, if anyone could point me in the right direction to figure this out, that would be great. 所以,如果有人能指出我正确的方向来解决这个问题,那就太好了。 Thanks. 谢谢。

/**
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;
}
 }

max(double, double) returns the grouter double value. max(double, double)返回灌浆者的双倍值。 Just change 只是改变

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

to

return Math.max(transactionCount, freeTransactions);

if you want the greater value to be returned. 如果你想要返回更大的值。

doubles, like all primitive types don't have references like objects. double,就像所有原始类型都没有像对象一样的引用。 You need to save the returned value like double foo = functionThatReturnPrimitiveDouble() or just return it again like I did in my example above. 你需要像double foo = functionThatReturnPrimitiveDouble()一样保存返回的值,或者像我在上面的例子中那样再次返回它。

I think you want something like this (which assumes a fee of $1.00 for each transaction over the allowed amount): 我想你想要这样的东西(假定每笔交易超过允许金额的费用为1.00美元):

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

If the customer hasn't gone over their allowed number of free transactions, then (transCount - freeTransactions) will be 0, so no fee will be charged. 如果客户没有超过他们允许的免费交易数量,那么(transCount - freeTransactions)将为0,因此不收取任何费用。

This code is a little too clever for its own good, but I think it's what the eccentric requirement (don't use an if statement, use max instead) calls for. 这段代码对于它自己的好处来说有点过于聪明,但我认为这就是古怪的要求(不要使用if语句,使用max代替)。

Much clearer (but equivalent) would be: 更清楚(但相当)将是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM