Make Time notation's ToString() pretty print more readable when values are 0

This commit is contained in:
Zontreck 2024-02-16 15:36:16 -07:00
parent 825511a1b6
commit fc6db84289

View file

@ -31,14 +31,18 @@ public class TimeNotation
@Override @Override
public String toString() { public String toString() {
return String str =
Pluralize(Years, "year") + ", " + someOrNone(Years,Pluralize(Years, "year") + ", ") +
Pluralize(Months, "month") + ", " + someOrNone(Months, Pluralize(Months, "month") + ", ") +
Pluralize(Weeks, "week") + ", " + someOrNone(Weeks, Pluralize(Weeks, "week") + ", ") +
Pluralize(Days, "day") + ", " + someOrNone(Days, Pluralize(Days, "day") + ", ") +
Pluralize(Hours, "hour") + ", " + someOrNone(Hours, Pluralize(Hours, "hour") + ", ") +
Pluralize(Minutes, "minute") + ", " + someOrNone(Minutes, Pluralize(Minutes, "minute") + ", ") +
Pluralize(Seconds, "second"); someOrNone(Seconds, Pluralize(Seconds, "second"));
if(str == ""){
return "No Seconds";
} else return str;
} }
/** /**
@ -49,7 +53,30 @@ public class TimeNotation
*/ */
private String Pluralize(int num, String str) private String Pluralize(int num, String str)
{ {
return num + " " + (num > 1 ? str+"s" : str); return num + " " + ((num > 1) ? str+"s" : str);
}
/**
* A simple function to test a number, return a string, or return nothing at all.
* @param num The number to check
* @param str The string to return if the number is greater than zero
* @return Str if num >1, or empty string
*/
private String someOrNone(int num, String str)
{
if(num > 0) return str;
else return "";
}
/**
* A simple function to test a number, return a string, or return something else.
* @param num The number to check
* @param str The string to return if the number is greater than zero
* @return Str if num >1, or other string
*/
private String someOrOther(int num, String str, String other)
{
if(num > 0) return str;
else return other;
} }
/** /**