The code below works, but it seems it could be done better. Is there a better/more efficient way to do this?
Given:
- All days off ("relief days") will be consecutive.
- Relief days will be imported as a string. Days of week are represented by their first letter. "S" can represent Saturday or Sunday, "T" can represent Tuesday or Thursday. I.e., someone with Sat/Sun off would be "SS", someone with Tue/Wed/Thu off would be "TWT".
- Depending on how many hours a day they work, they'll either have 2, 3, or 4 days off.
The existing code:
public static String getReliefDays(){
Map<String, String> user = new HashMap<>(); //test example user
user.put("name", "Schmoe, Joe");
user.put("employeeNo", "123456");
user.put("startTime", "0800");
user.put("endTime", "1700");
user.put("reliefDays", "TF");
String rd = (String) user.get("reliefDays");
int rdLength = rd.length();
String reliefDays = "";
String fl = rd.substring(0, 1); //first letter in the days off string
String sl = rd.substring(1, 2); //second letter in the days off string
switch (fl) {
case "S":
if (sl.equalsIgnoreCase("M")) {
reliefDays = "SUN MON";
if (rdLength == 3) {
reliefDays += " TUE";
}
if (rdLength == 4) {
reliefDays += " WED";
}
}else{
reliefDays = "SAT SUN";
if (rdLength == 3) {
reliefDays += " MON";
}
if (rdLength == 4) {
reliefDays += " TUE";
}
}
break;
case "M":
reliefDays = "MON TUE";
if (rdLength == 3) {
reliefDays += " WED";
}
if (rdLength == 4) {
reliefDays += " THU";
}
break;
case "T":
if (sl.equalsIgnoreCase("W")) {
reliefDays = "TUE WED";
if (rdLength == 3) {
reliefDays += " THU";
}
if (rdLength == 4) {
reliefDays += " FRI";
}
} else {
reliefDays = "THU FRI";
if (rdLength == 3) {
reliefDays += " SAT";
}
if (rdLength == 4) {
reliefDays += " SUN";
}
}
break;
case "W":
reliefDays = "WED THU";
if (rdLength == 3) {
reliefDays += " FRI";
}
if (rdLength == 4) {
reliefDays += " SAT";
}
break;
case "F":
reliefDays = "FRI SAT";
if (rdLength == 3) {
reliefDays += "SUN";
}
if (rdLength == 4) {
reliefDays += " MON";
}
break;
}
return reliefDays;
}
main
to help clarify the behavior at a glance -- I'm not sure what formatString rd
is or what the output should be for different inputs. How are you supposed to differentate between Tu/Th and Sa/Su ifrd
is single-letter abbreviations? Thanks. \$\endgroup\$return "SAT SUN";
which seems rather useless (not to mention probably wrong, as "THU FRI" would seem a more likely result). \$\endgroup\$"SAT SUN MON TUE"
? As opposed to[DaysOfWeek.SATURDAY, 4]
or something else. \$\endgroup\$