-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbusyschedule.java
More file actions
58 lines (41 loc) · 1.4 KB
/
Copy pathbusyschedule.java
File metadata and controls
58 lines (41 loc) · 1.4 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
58
import java.util.Arrays;
import java.util.Scanner;
public class busyschedule {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true)
{
int times = scan.nextInt();
if (times == 0)
break;
Time[] T = new Time[times];
for (int i = 0; i < T.length; i++)
T[i] = new Time(scan.next() , scan.next());
Arrays.sort(T);
for (Time time : T)
System.out.println(time);
System.out.println();
}
scan.close();
}
}
class Time implements Comparable<Time> {
int hours , mins;
public Time(String time , String format) {
hours = Integer.parseInt(time.substring(0 , time.indexOf(":"))) == 12 ? 0 : Integer.parseInt(time.substring(0 , time.indexOf(":")));
mins = Integer.parseInt(time.substring(time.indexOf(":") + 1));
if (format.equals("p.m."))
hours += 12;
}
public int compareTo(Time time) {
if (this.hours == time.hours)
return this.mins - time.mins;
return this.hours - time.hours;
}
public String toString() {
String mins = ("000" + this.mins).substring(("000" + this.mins).length() - 2);
if (hours >= 12)
return (hours == 12 ? 12 : hours - 12) + ":" + mins + " p.m.";
return (hours == 0 ? 12 : hours) + ":" + mins + " a.m.";
}
}