Correct way to update a property of an Object

0

I created an update method, however I'm having to create a variable to be able to update my property, which although working, I think this would not be the correct way to treat an object.

I have this property in the FunctionalModel object below the code below.

public void atualizar(FuncionarioModel funcionarioModel) throws NegocioException {
    String cargo = funcionarioModel.getCargo();
    funcionarioModel = this.funcionarioRepository.porId(funcionarioModel.getCodigo());
    funcionarioModel.setCargo(cargo);
    if (cargo.isEmpty()) {
        throw new NegocioException("Não é possível fazer a Alteração campo cargo está vazio !");

    }
}

If someone knows how I can fix this.

    
asked by anonymous 06.04.2017 / 18:01

1 answer

1

If you want to delete the line "String cargo = funcionarioModel.getCargo();" you can do this:

public void atualizar(FuncionarioModel funcionarioModel) throws NegocioException {  
    if (funcionarioModel.getCargo().isEmpty()) {
        throw new NegocioException("Não é possível fazer a Alteração campo cargo está vazio !");
    }  
    this.funcionarioRepository.porId(funcionarioModel.getCodigo()).setCargo(funcionarioModel.getCargo());
}

Note that you should check if you are going to throw an Exception before making the change, because if you post it after making the change your "this.funcionarioRepository" will have an Invalid Position (empty) even after the Exception has been posted.

    
07.04.2017 / 20:26