need help in enum in java [closed]

-2

I'm migrating a desktop application in delphi to java, and in delphi has a class that persists in the bench the values 00, 10, 20 and 30. I'm doing an enum in java to persist values. I know that by annotation, I can define the string and ordinal strategies, but none of them will answer me.

What I have so far.

public enum Tipo {
    tipo("00"),
    tipo1("10"),
    tipo2("20"),
    tipo3("30");

    String valor;

    private Tipo(String valor) {
        this.valor=valor;
    }

    public String getValor() {
        return valor;
    }   
}

I would like to know if I'm on the way or if there is a better way to create this enum?

    
asked by anonymous 08.04.2017 / 21:18

1 answer

0

I got it with this code.

The enum looks like this.

@Column(name="MOD_RECEPCAO", length=2)
        private String modeloRecepcao;
        public enum ModRecepcao{

            conferencia_recepicao("00"),
            conferencia_recepicao_armagenagem_Ulma("10"),
            conferencia_recepicao_puxada_armagenagem_Ulma("20"),
            conferencia_recepicao_armagenagem_total("30");

            private final String valor;

            private ModRecepcao(String valor){
                this.valor=valor;
            }

            //converão e verificação se nome da classe para valor
            public static String parseSet(String s) {
                for (ModRecepcao modelo: ModRecepcao.values()) {
                    if (s.equals(modelo.toString())) return modelo.getValor();
                }
                throw new IllegalArgumentException("Modelo invalido");
            }

            //convesão e verificação de valor para o nome da classe
            public static String parseGet(String s) {
                for (ModRecepcao modelo: ModRecepcao.values()) {
                    if(s == null){
                        return "";
                    }
                    if (s.equals(modelo.getValor())){
                        return modelo.toString();
                    }
                }
                throw new IllegalArgumentException("Modelo invalido");
            }

             public String getValor() {
                    return this.valor;
            }

        }

 get e set
public String getModeloRecepcao() {
            return ModRecepcao.parseGet(modeloRecepcao);
        }


        public void setModeloRecepcao(String modeloRecepcao) {
            this.modeloRecepcao = ModRecepcao.parseSet(modeloRecepcao);
        }

But I'd like something more elegant. if anyone has an idea please help me.

    
10.04.2017 / 18:44