I need help in java

Asked

Viewed 125 times

-2

I am migrating a desktop application in Delphi to java, and in Delphi there is a class that persists in the database the values 00, 10, 20 and 30. I’m making a java Enum to persist the values. I know that, by annotation, you have to define the strategies of string and ordinal , but none of them suits 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 wonder if I’m in the way or if there’s a better way to create this Enum?

1 answer

0


I got it with this code.

The Enum was 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);
        }

more I would like something more elegant. if anyone has an idea please help me.

  • This is the solution? If it is another doubt, you must create a new question or edit this, this field is only for answers.

  • And the one I found more not the one I want, so I would very much like to know if there is another solution I don’t ,because I urgently need this help;so I didn’t create a new question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.