JAVA - Best fit data structure

I'm a little new to Java, but now I'm facing a dilemma. I have a list of errors that looks like this:

"ERROR CODE" "POSITION" "Error description"
"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"

and a lot more mistakes.

Now I'm working on the JAVA library, what I really need to do is implement these errors (errors never change, they are always the same), so developers who use the library can easily identify the error description using this ERROR_CODE.

for example: String getError (ERROR_CODE); and return the error description string associated with ERROR_CODE.

I was thinking of declaring an ENUM data structure, but I cannot get it to work correctly.

Many thanks.

+5
source share
8 answers

:

enum Error {

    ERROR_000("000", 1, "No error"),
    ERROR_001("001", 1, "Connection error"),
    ERROR_002("002", 1, "Error sending reversal or batch capture process"),
    ERROR_003("003", 1, "Error after authorization – message sent" +
                        "to host and answer received"),
    ERROR_004("004", 1, "Error sending message for authorization"),
    ERROR_005("005", 1, "Error receiving message from host");

    private final String code;
    private final int position;
    private final String description;
    private static final Map<String, Error> errorMap =
        new HashMap<String, Error>();

    static {
        for (Error error : Error.values()) {
            errorMap.put(error.code, error);
        }
    }

    Error(final String code, final int position, final String description) {
        this.code = code;
        this.position = position;
        this.description = description;
    }

    public static Error getError(String code) {
        return errorMap.get(code);
    }
    // add getters and setters here:
    public String getCode() { return this.code; }
    public int getPosition() { return this.position; }
    public String getDescription() { return this.description; }
}
+3

, :

public enum Error {

   public final int code;
   public final String message;

   e0 (000, "No Error"),
   e1 (001, "Connection error");

   public Error(int code, String message) {
      this.code = code;
      this.message = message;
   }

   public static Error byCode(int code) {
        return Error.valueOf("e"+code); // you may add try/catch on IllegalArgumentException, etc.
   }
}

( , , , HashMap ), .

switch java 1.5.

+2

java.util.Map (HashMap). .

+1
enum ErrorCode{
 001,002
}

class ErrorWrapper{
 private ErrorCode errorCode;
 private String description;
 //setters + getters    
}

Map<ErrorCode, List<ErrorWrapper>>

0

HashMap<String, String> 

, , .

0

, " " :)

( " " )

Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("errors.properties"));
    } catch (IOException e) {}

getError :

public String getError(String errorCode){
     return properties.getProperty(errorCode);
}

errors.properties :

001=No error
002=Connection error

,

0

- ; . Main , . getError (String code) Errors, :

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;
        }
    }
}
0

Java enum . , .

  • , .
    • 000-1 =
    • 001-1 =
  • , .
  • , jar, ( HashMap) .
  • , HashMap, .
  • getError(ERROR_CODE) -1 .
  • . .
0

All Articles