我为你写了一些东西;包括测试代码。只需在默认包中创建一个 Main 类,复制粘贴我的代码并运行它。您可以使用 Errors 类中的 getError(String code) 方法通过代码获取错误消息:
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;
}
}
}