I have 7 boolean variables in a java object for the days of the week (Monday through Sunday). I am trying to find a succinct way from a for loop of a list of days to say the following:
private boolean isWorkDay(String day, Object pgMasterTaskList) {
if(day=="Sunday" && pgMasterTaskList.Sunday==true) {
return true;
}
if(day=="Monday" && pgMasterTaskList.Monday==true) {
return true;
}
if(day=="Tuesday" && pgMasterTaskList.Tuesday==true) {
return true;
}
if(day=="Wednesday" && pgMasterTaskList.Wednesday==true) {
return true;
}
if(day=="Thursday" && pgMasterTaskList.Thursday==true) {
return true;
}
if(day=="Friday" && pgMasterTaskList.Friday==true) {
return true;
}
if(day=="Saturday" && pgMasterTaskList.Saturday==true) {
return true;
}
else return false;
}
I'm hoping I can minimize the repetition with something like...
private boolean isWorkDay(String day, Object pgMasterTaskList) {
if(pgMasterTaskList.getVariableByName(day)==true) {
return true;
}
else return false;
}
My thought is to use reflection to check if the value of the variable named "Monday" and so on, but I'm not sure how to use it in such a way.
Edit
I have submitted an answer that works for my specific case. A little more detail...
I'm writing this information out to a pdf so I was handling string comparisons because I have to map them to field names in the pdf. So I was trying to be consistent. ANY combination of days could be a work day. I think this leaves me with 128 combos? (2^7). The information is being fed in by csv so I wanted to read day in as a string since it's coming in as such.