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;
java design-patterns setter getter
add a comment |
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;
java design-patterns setter getter
Have you heard of loops
– JavaScriptCoder
Nov 23 '18 at 21:18
Just include it in the setter itself.
– Himanshu Ahuja
Nov 23 '18 at 21:19
If you need to set each field then you need to call each setter there is no way around it except for using Reflection but you will need to ask yourself if it's worth all the work to take that route.
– Joakim Danielson
Nov 23 '18 at 21:22
the loops is not the solution, yes i need to call each setter, but do i need to implements this code on each setter?
– Santiago Molano
Nov 23 '18 at 21:55
add a comment |
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;
java design-patterns setter getter
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;
java design-patterns setter getter
java design-patterns setter getter
asked Nov 23 '18 at 21:16
Santiago MolanoSantiago Molano
12
12
Have you heard of loops
– JavaScriptCoder
Nov 23 '18 at 21:18
Just include it in the setter itself.
– Himanshu Ahuja
Nov 23 '18 at 21:19
If you need to set each field then you need to call each setter there is no way around it except for using Reflection but you will need to ask yourself if it's worth all the work to take that route.
– Joakim Danielson
Nov 23 '18 at 21:22
the loops is not the solution, yes i need to call each setter, but do i need to implements this code on each setter?
– Santiago Molano
Nov 23 '18 at 21:55
add a comment |
Have you heard of loops
– JavaScriptCoder
Nov 23 '18 at 21:18
Just include it in the setter itself.
– Himanshu Ahuja
Nov 23 '18 at 21:19
If you need to set each field then you need to call each setter there is no way around it except for using Reflection but you will need to ask yourself if it's worth all the work to take that route.
– Joakim Danielson
Nov 23 '18 at 21:22
the loops is not the solution, yes i need to call each setter, but do i need to implements this code on each setter?
– Santiago Molano
Nov 23 '18 at 21:55
Have you heard of loops
– JavaScriptCoder
Nov 23 '18 at 21:18
Have you heard of loops
– JavaScriptCoder
Nov 23 '18 at 21:18
Just include it in the setter itself.
– Himanshu Ahuja
Nov 23 '18 at 21:19
Just include it in the setter itself.
– Himanshu Ahuja
Nov 23 '18 at 21:19
If you need to set each field then you need to call each setter there is no way around it except for using Reflection but you will need to ask yourself if it's worth all the work to take that route.
– Joakim Danielson
Nov 23 '18 at 21:22
If you need to set each field then you need to call each setter there is no way around it except for using Reflection but you will need to ask yourself if it's worth all the work to take that route.
– Joakim Danielson
Nov 23 '18 at 21:22
the loops is not the solution, yes i need to call each setter, but do i need to implements this code on each setter?
– Santiago Molano
Nov 23 '18 at 21:55
the loops is not the solution, yes i need to call each setter, but do i need to implements this code on each setter?
– Santiago Molano
Nov 23 '18 at 21:55
add a comment |
2 Answers
2
active
oldest
votes
if you have something like this
class Foo {
String var1;
String var2;
void setVar1(String var1) {
this.var1 = var1;
}
void setVar2(String var2) {
this.var2 = var2;
}
}
maybe you can add a constructor to initialize the object with spaces like this
class Foo {
String var1;
String var2;
public Foo(String var1, String var2) {
this.var1 = var1;
this.var2 = var2;
}
void setVar1(String var1) {
this.var1 = var1;
}
void setVar2(String var2) {
this.var2 = var2;
}
}
then you can do
Foo foo = new Foo(
rellenarStringConEspaciosEnBlanco("A chain", 7, 1),
rellenarStringConEspaciosEnBlanco("A chain", 7, 1)
);
add a comment |
i didnt have to modify the DTO, the solution was not modify the DTO it was to create a new method on the class i was calling the setters, heres the code
detailRecord.setReferenceOne(rellenarCadena(datosExcelParaProcesar.getField("Referencia1"),detailRecord.getLengthReference1()));
and rellenar cadena is
public String rellenarCadena(String cadenaParaRellenar, int sizeCadenaTotal){
int size = sizeCadenaTotal-cadenaParaRellenar.length();
if(size>0){
String formato = "%-" + size + "s";
String rellenar = String.format(formato, " ");
cadenaParaRellenar = rellenar+cadenaParaRellenar;
}
return cadenaParaRellenar;
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453020%2fimprovement-setters-and-methods-that-i-need-to-fill-fields%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
if you have something like this
class Foo {
String var1;
String var2;
void setVar1(String var1) {
this.var1 = var1;
}
void setVar2(String var2) {
this.var2 = var2;
}
}
maybe you can add a constructor to initialize the object with spaces like this
class Foo {
String var1;
String var2;
public Foo(String var1, String var2) {
this.var1 = var1;
this.var2 = var2;
}
void setVar1(String var1) {
this.var1 = var1;
}
void setVar2(String var2) {
this.var2 = var2;
}
}
then you can do
Foo foo = new Foo(
rellenarStringConEspaciosEnBlanco("A chain", 7, 1),
rellenarStringConEspaciosEnBlanco("A chain", 7, 1)
);
add a comment |
if you have something like this
class Foo {
String var1;
String var2;
void setVar1(String var1) {
this.var1 = var1;
}
void setVar2(String var2) {
this.var2 = var2;
}
}
maybe you can add a constructor to initialize the object with spaces like this
class Foo {
String var1;
String var2;
public Foo(String var1, String var2) {
this.var1 = var1;
this.var2 = var2;
}
void setVar1(String var1) {
this.var1 = var1;
}
void setVar2(String var2) {
this.var2 = var2;
}
}
then you can do
Foo foo = new Foo(
rellenarStringConEspaciosEnBlanco("A chain", 7, 1),
rellenarStringConEspaciosEnBlanco("A chain", 7, 1)
);
add a comment |
if you have something like this
class Foo {
String var1;
String var2;
void setVar1(String var1) {
this.var1 = var1;
}
void setVar2(String var2) {
this.var2 = var2;
}
}
maybe you can add a constructor to initialize the object with spaces like this
class Foo {
String var1;
String var2;
public Foo(String var1, String var2) {
this.var1 = var1;
this.var2 = var2;
}
void setVar1(String var1) {
this.var1 = var1;
}
void setVar2(String var2) {
this.var2 = var2;
}
}
then you can do
Foo foo = new Foo(
rellenarStringConEspaciosEnBlanco("A chain", 7, 1),
rellenarStringConEspaciosEnBlanco("A chain", 7, 1)
);
if you have something like this
class Foo {
String var1;
String var2;
void setVar1(String var1) {
this.var1 = var1;
}
void setVar2(String var2) {
this.var2 = var2;
}
}
maybe you can add a constructor to initialize the object with spaces like this
class Foo {
String var1;
String var2;
public Foo(String var1, String var2) {
this.var1 = var1;
this.var2 = var2;
}
void setVar1(String var1) {
this.var1 = var1;
}
void setVar2(String var2) {
this.var2 = var2;
}
}
then you can do
Foo foo = new Foo(
rellenarStringConEspaciosEnBlanco("A chain", 7, 1),
rellenarStringConEspaciosEnBlanco("A chain", 7, 1)
);
answered Nov 23 '18 at 22:31
elbraulioelbraulio
742213
742213
add a comment |
add a comment |
i didnt have to modify the DTO, the solution was not modify the DTO it was to create a new method on the class i was calling the setters, heres the code
detailRecord.setReferenceOne(rellenarCadena(datosExcelParaProcesar.getField("Referencia1"),detailRecord.getLengthReference1()));
and rellenar cadena is
public String rellenarCadena(String cadenaParaRellenar, int sizeCadenaTotal){
int size = sizeCadenaTotal-cadenaParaRellenar.length();
if(size>0){
String formato = "%-" + size + "s";
String rellenar = String.format(formato, " ");
cadenaParaRellenar = rellenar+cadenaParaRellenar;
}
return cadenaParaRellenar;
}
add a comment |
i didnt have to modify the DTO, the solution was not modify the DTO it was to create a new method on the class i was calling the setters, heres the code
detailRecord.setReferenceOne(rellenarCadena(datosExcelParaProcesar.getField("Referencia1"),detailRecord.getLengthReference1()));
and rellenar cadena is
public String rellenarCadena(String cadenaParaRellenar, int sizeCadenaTotal){
int size = sizeCadenaTotal-cadenaParaRellenar.length();
if(size>0){
String formato = "%-" + size + "s";
String rellenar = String.format(formato, " ");
cadenaParaRellenar = rellenar+cadenaParaRellenar;
}
return cadenaParaRellenar;
}
add a comment |
i didnt have to modify the DTO, the solution was not modify the DTO it was to create a new method on the class i was calling the setters, heres the code
detailRecord.setReferenceOne(rellenarCadena(datosExcelParaProcesar.getField("Referencia1"),detailRecord.getLengthReference1()));
and rellenar cadena is
public String rellenarCadena(String cadenaParaRellenar, int sizeCadenaTotal){
int size = sizeCadenaTotal-cadenaParaRellenar.length();
if(size>0){
String formato = "%-" + size + "s";
String rellenar = String.format(formato, " ");
cadenaParaRellenar = rellenar+cadenaParaRellenar;
}
return cadenaParaRellenar;
}
i didnt have to modify the DTO, the solution was not modify the DTO it was to create a new method on the class i was calling the setters, heres the code
detailRecord.setReferenceOne(rellenarCadena(datosExcelParaProcesar.getField("Referencia1"),detailRecord.getLengthReference1()));
and rellenar cadena is
public String rellenarCadena(String cadenaParaRellenar, int sizeCadenaTotal){
int size = sizeCadenaTotal-cadenaParaRellenar.length();
if(size>0){
String formato = "%-" + size + "s";
String rellenar = String.format(formato, " ");
cadenaParaRellenar = rellenar+cadenaParaRellenar;
}
return cadenaParaRellenar;
}
answered Nov 24 '18 at 0:09
Santiago MolanoSantiago Molano
12
12
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453020%2fimprovement-setters-and-methods-that-i-need-to-fill-fields%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you heard of loops
– JavaScriptCoder
Nov 23 '18 at 21:18
Just include it in the setter itself.
– Himanshu Ahuja
Nov 23 '18 at 21:19
If you need to set each field then you need to call each setter there is no way around it except for using Reflection but you will need to ask yourself if it's worth all the work to take that route.
– Joakim Danielson
Nov 23 '18 at 21:22
the loops is not the solution, yes i need to call each setter, but do i need to implements this code on each setter?
– Santiago Molano
Nov 23 '18 at 21:55