简体   繁体   中英

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. I'm using the Math.max in my deductMonthlyCharge method. 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. 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. 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. You need to save the returned value like double foo = functionThatReturnPrimitiveDouble() or just return it again like I did in my example above.

I think you want something like this (which assumes a fee of $1.00 for each transaction over the allowed amount):

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.

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.

Much clearer (but equivalent) would be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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