vendredi 23 novembre 2018

improvement setters and methods that i need to fill fields

i have a model with multiple fields, therefore, i have setters and getters methods, then,i need to fill this fields with zeros o blank spaces to the left, i already have a method for this but i will need to call it on every setter, how can i improve this and does not have a lot of duplicate code?

here is the example:

public String rellenarStringConEspaciosEnBlanco(String cadenaARellenar, int tamanoACumplir,
        int tamanoCadenaARellenar) {
    int size = tamanoACumplir - tamanoCadenaARellenar;
    if(size>0) {
        String formato = "%-" + size + "s";
        String rellenar = String.format(formato, " ");
        cadenaARellenar = cadenaARellenar + rellenar;
        return cadenaARellenar;
    }
    return cadenaARellenar;

Aucun commentaire:

Enregistrer un commentaire