我寫的東西了你;包括測試代碼。只需在默認包中創建一個Main類,複製粘貼我的代碼並運行它。您可以使用該方法getError(字符串代碼)從類錯誤的代碼來獲取錯誤消息:
import java.util.*;
public class Main {
public static void main(String[] args) {
for(String code : new String[]{"000", "001", "002", "003", "004", "005"}) {
System.out.println(Errors.getError(code));
}
}
}
class Errors {
static {
Errors.errors = new HashMap<String, Error>();
for(String[] error : new String[][]{
{"000", "1", "No error"},
{"001", "1", "Connection error"},
{"002", "1", "Error sending reversal or batch capture process"},
{"003", "1", "Error after authorization – message sent to host and answer received"},
{"004", "1", "Error sending message for authorization"},
{"005", "1", "Error receiving message from host"}
}) {
Errors.errors.put(error[0], new Error(error[0], error[1], error[2]));
}
}
private static Map<String, Error> errors;
public static String getError(String code) {
return Errors.errors.get(code).message;
}
private static class Error {
private String code;
private String position;
private String message;
public Error(String code, String position, String message) {
super();
this.code = code;
this.position = position;
this.message = message;
}
@Override
public String toString() {
return this.getClass().getSimpleName() + " | code: " + this.code + " | position: " + this.position + " | message: " + this.message;
}
}
}
來源
2012-07-04 11:41:01
Tom
向我們展示您的枚舉的嘗試 –
正確的結構確實是一個枚舉。 –
我不會說適當的結構是一個枚舉,id說它完全取決於情況。特別是考慮到他非常清楚地說錯誤代碼是一個字符串,這可能是因爲某些錯誤代碼包含非數字字符。 –