Can these “statements” be simplified?

I have the following method:

public static String format_String(int hours, int minutes, int seconds)
{   
    if(hours > 0 && minutes > 0 && seconds > 0) return hours + " hours, " + minutes + " minutes and " + seconds + " seconds.";
    else if(hours > 0 && minutes > 0 && seconds == 0) return hours + " hours and " + minutes + " minutes.";
    else if(hours > 0 && minutes == 0 && seconds > 0) return hours + " hours and " + seconds + " seconds.";
    else if(hours > 0 && minutes == 0 && seconds == 0) return hours + " hours.";
    else if(hours == 0 && minutes > 0 && seconds > 0) return minutes + " minutes and " + seconds + " seconds.";
    else if(hours == 0 && minutes > 0 && seconds == 0) return minutes + " minutes.";
    else //if(hours == 0 && minutes == 0 && seconds > 0)
    return seconds + " seconds.";
}

Can this method be simplified?

+3
source share
5 answers

The hard part is whether to separate the parts with " and "or ",", which depends on how many nonzero parts are displayed to the right of the part that you are currently printing. The rest (printing names and numbers) are simple.

Therefore, you can reduce the number of branches by building a row from right to left.

public static String format_String(int hours, int minutes, int seconds)
{
    StringBuilder result = new StringBuilder(".");
    String sep = "", nextSep = " and ";
    if (seconds > 0) {
        result.insert(0, " seconds").insert(0, seconds);
        sep = nextSep;
        nextSep = ", ";
    }
    if (minutes > 0) {
        result.insert(0, sep).insert(0, " minutes").insert(0, minutes);
        sep = nextSep;
        nextSep = ", ";
    }
    if (hours > 0) {
        result.insert(0, sep).insert(0, " hours").insert(0, hours);
    }
    return result.toString();
}

or more broadly:

public static String formatString(SortedMap<TimeUnit, Integer> parts) {
    StringBuilder result = new StringBuilder(".");
    String sep = "", nextSep = " and ";
    for (Map.Entry<TimeUnit, Integer> e: parts.entrySet()) {
        TimeUnit field = e.getKey();
        Integer quantity = e.getValue();
        if (quantity > 0) {
            result.insert(0, sep)
                  .insert(0, field.toString().toLowerCase())
                  .insert(0, ' ')
                  .insert(0, quantity);
            sep = nextSep;
            nextSep = ", ";
        }
    }
    return result.toString();
}
+4
source

Think about how you will prepare to SAY the same sentence.

if hours > 0 then say the hours
if minutes > 0 then say the minutes
if seconds > 0 then say the seconds

Then create the logic using a StringBuilder with one return at the end.

+2
source

, :

return
(hours > 0 && minutes > 0 && seconds > 0)   ? hours + " hours, " + minutes + " minutes and " + seconds + " seconds." :
(hours > 0 && minutes > 0 && seconds == 0)  ? hours + " hours and " + minutes + " minutes." :
(hours > 0 && minutes == 0 && seconds > 0)  ? hours + " hours and " + seconds + " seconds." :
(hours > 0 && minutes == 0 && seconds == 0) ? hours + " hours." :
(hours == 0 && minutes > 0 && seconds > 0)  ? minutes + " minutes and " + seconds + " seconds." :
(hours == 0 && minutes > 0 && seconds == 0) ? minutes + " minutes." :
seconds + " seconds.";
+1

?

public static String formatTime(int hours, int minutes, int seconds) {
    List<String> parts = new ArrayList<String>(3);
    if (hours > 0) parts.add(hours + " hours");
    if (minutes > 0) parts.add(minutes + " minutes");
    if (parts.isEmpty() || seconds > 0) parts.add(seconds + " seconds");
    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < parts.size(); i++) {
        if (i > 0) builder.append((i < parts.size() - 1) ? ", " : " and " );
        builder.append(parts.get(i));
    }

    return builder.append(".").toString();
}

. , Java.

+1

, >= 0. , !

if(hours > 0) {
    if(minutes > 0) {
        if(seconds > 0) return hours + " hours, " + minutes + " minutes and " + seconds + " seconds.";
        return hours + " hours and " + minutes + " minutes.";
    }
    if(seconds > 0) return hours + " hours and " + seconds + " seconds.";
    return hours + " hours.";
}
if(minutes > 0 ) {
    if(seconds > 0 ) return minutes + " minutes and " + seconds + " seconds.";
    return minutes + " minutes.";
}
return seconds + "seconds.";
0
source

All Articles