2016-11-14 55 views
0

我有ENUM類,我正在使用它來構建響應對象。將枚舉描述與可變參數相結合

if(statusCode == 0){ 
      userModel.setResponseCode(BOFAStatusCodes.ACTION_TAKEN.getErrorCode()); 
      userModel.setResponseMessage(BOFAStatusCodes.ACTION_TAKEN.getDescription()); 
     } 

這裏是ENUM。

public enum BOFAStatusCodes { 
ACTION_TAKEN(0,"An action has been taken that successfully completes the request."), 
NO_ACTION(800,"No action was necessary to complete the request successfully."), 
AUTH_FAILED(403,"Authentication failed."), 
FAILURE_CLIENT(905,"The request cannot be completed due to an error with the client's request."), 
FAILURE_SERVICE(500,"The request cannot be completed due to an error with the service's processing of the request."); 

private String description; 
private int errorCode; 

private BOFAStatusCodes(int errorCode, String description){ 
    this.errorCode=errorCode; 
    this.description = description; 
} 
public int getErrorCode() { 
    return errorCode; 
} 

public String getDescription() { 
    return description; 
} 

}

但現在的要求是這樣的,他們想與例如郵件的用戶ID一起:

An action has been taken that successfully completes the request for the User XXX. 

任何建議。

回答

3

您可以使用字符串格式,在您的枚舉

ACTION_TAKEN(0,"An action has been taken that successfully completes the request for the user %s.") 

和類:

if(statusCode == 0){ 
     userModel.setResponseCode(BOFAStatusCodes.ACTION_TAKEN.getErrorCode()); 
     userModel.setResponseMessage(String.format(BOFAStatusCodes.ACTION_TAKEN.getDescription(), username)); 
    }