2012-10-03 2 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의 수수료를 가정) 같은 것을 원한다고 생각 (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 값을 반환합니다. 당신이 더 큰 값을 반환하려면 그냥

return Math.max(transactionCount, freeTransactions); 

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

을 변경합니다.

두 배가됩니다. 모든 기본 유형에는 객체와 같은 참조가 없습니다. double foo = functionThatReturnPrimitiveDouble()과 같은 반환 값을 저장하거나 위 예제에서했던 것처럼 다시 반환해야합니다. 다음, 고객이 무료 거래 허용 된 걸쳐 사라하지 않은 경우

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

:

+0

맞습니다. 설명을 듣고 답해 주겠지 만 반환 값을 받아서 다음과 같이 말하면 어떻게됩니까? 반환 값이 매월 무료 트랜잭션 양보다 많으면 트랜잭션에서 수수료를 뺍니다 if 문을 사용하지 않습니까? 그것이 내가 혼란스러워하는 곳입니다. – user1696162

+0

Bill이 방금 당신의 관심사에 대해 만족해야하는 대답을했습니다. 개인적으로는 if 문을 사용하여이 사례를 처리 할 수도 있습니다. 의도가 독자에게 명확하기 때문입니다. 그래서 'return 1.00 * (Math.max (transactionCount, freeTransactions) - freeTransactions)'가 작동해야합니다.) – Zhedar

관련 문제