2017-11-27 1 views
-1

이것은 은행 고객 클래스이며 여러 return 문을 포함합니다. 내 질문은 어떻게 여러 return 문을 없애고 각 메서드 끝에 하나의 여러 반환 할 싶습니다.메소드에서 복수 return 문 제거 방법

public class BankCustomer { 
    //define the attribute 
    private String name; 
    private int chequeAcctNum; 
    private double chequeBal; 
    private int savingAcctNum; 
    private double savingBal; 

    //define constractor 
    public BankCustomer(String n, int chqAcctNum, double chqBal 
    , int savAcctNum, double savBal) 
    { 
     name = n; 
     chequeAcctNum = chqAcctNum; 
     chequeBal = chqBal; 
     savingAcctNum = savAcctNum; 
     savingBal = savBal;   
    } 

    //define the methods 
    // Call withdraw from chequing method 
    public boolean withdrawChequing(double amount) { 
      if(chequeBal >= amount) { 
      chequeBal-=amount; 
      return true;  
      } else {    
      return false; 
      }  
    } 
+1

A를 조건을 지정 :

if(chequeBal >= amount) { chequeBal-=amount; retVal = true; } 

는 그 다음 retVal 반환을 'boolean' 변수를 반환하고이를 반환합니다. –

답변

0

뭔가이 : 제 생각에는

public boolean withdrawChequing(double amount) { 
    boolean bRetVal = false; 
    if(chequeBal >= amount) { 
     chequeBal-= amount; 
     bRetVal = true; 
    } 
    return bRetVal; 
} 
1

여러 반환 진술과 방법이 완벽하게 괜찮습니다. 올바르게 사용하면 코드를보다 쉽게 ​​읽을 수 있습니다. 메소드를 변경할 필요가 없습니다.

귀하가 주장하는 경우, 여기에 하나의 return 문으로 줄이는 방법이 있습니다.

는 반환 값을 저장하는 부울 변수 만들기 :

boolean retVal = false; 

를 그리고 상태 확인 :

return retVal;