-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondsAndMinutes.java
More file actions
57 lines (37 loc) · 1.35 KB
/
SecondsAndMinutes.java
File metadata and controls
57 lines (37 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.company;
public class Main {
private static final String INVALID_MESSAGE = "Invalid value";
public static void main(String[] args) {
System.out.println("Seconds And Minutes Challenge");
System.out.println(getDurationString(65, 45));
System.out.println(getDurationString(3945L));
}
private static String getDurationString (long minutes, long seconds) {
if ((seconds < 0) || (minutes < 0) || (seconds > 59)) {
return INVALID_MESSAGE;
}
long hours = minutes / 60;
long remainingMinutes = minutes % 60;
String hoursString = hours + "h";
if (hours < 10) {
hoursString = "0" + hoursString;
}
String minutesString = remainingMinutes + "m";
if (remainingMinutes < 10) {
minutesString = "0" + minutesString;
}
String secondsString = seconds + "s";
if (seconds < 10) {
secondsString = "0" + secondsString;
}
return hoursString + " " + minutesString + " " + secondsString;
}
private static String getDurationString (long seconds) {
if (seconds < 0) {
return INVALID_MESSAGE;
}
long minutes = seconds / 60;
long remainingSeconds = seconds % 60;
return getDurationString(minutes,remainingSeconds);
}
}