-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathProblem6.java
More file actions
95 lines (77 loc) · 2.89 KB
/
Problem6.java
File metadata and controls
95 lines (77 loc) · 2.89 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package io.zipcoder;
import java.util.Arrays;
public class Problem6 {
public static String[] militaryNum = new String[]{"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
"Nineteen", "Twenty", "Thirty", "Forty", "Fifty"};
public String militaryConverter(String time) {
Integer[] fullClock = splitHourAndMinutes(time);
if(militaryPhrasing(fullClock).charAt(0) == militaryPhrasing(fullClock).charAt(5)){
return militaryPhrasing(fullClock).substring(5);
}
return militaryPhrasing(fullClock);
}
public Integer[] splitHourAndMinutes(String time) {
String[] clockGroup = time.split(":");
String amOrPm = clockGroup[1].substring(2);
int hours = Integer.parseInt(clockGroup[0]);
if (hours != 12) {
if (amOrPm.equals("pm")) {
hours += 12;
}
} else {
if (amOrPm.equals("am")) {
hours -= hours;
}
}
int minutes = Integer.parseInt(clockGroup[1].substring(0, 2));
Integer[] fullClock = new Integer[2];
fullClock[0] = hours;
fullClock[1] = minutes;
return fullClock;
}
public String militaryPhrasing(Integer[] fullClock){
int hours = fullClock[0];
int minutes = fullClock[1];
StringBuilder stringBuilder = new StringBuilder();
if (hours < 10){
stringBuilder.append(militaryNum[0] + " ");
}
if (hours <= 20){
stringBuilder.append(militaryNum[hours] + " ");
}
else{
stringBuilder.append(militaryNum[20] + (militaryNum[hours - 20]) + " ");
}
stringBuilder.append("Hundred ");
if (minutes > 0) {
stringBuilder.append("and ");
if (minutes <= 20) {
if (minutes < 10) {
stringBuilder.append(militaryNum[0] + " ");
}
stringBuilder.append(militaryNum[minutes] + " ");
} else {
int tenBase = minutes / 10;
int oneBase = minutes % 10;
stringBuilder.append(militaryNum[18 + tenBase] + " ");
if (oneBase > 0) {
stringBuilder.append(militaryNum[oneBase] + " ");
}
}
}
stringBuilder.append("Hours");
return stringBuilder.toString();
}
public static void main(String[] args) {
Problem6 test = new Problem6();
String time = "1:35am";
test.splitHourAndMinutes(time);
System.out.println(Arrays.toString(test.splitHourAndMinutes(time)));
System.out.println(militaryNum[20]);
}
}
//1:30pm
//1 = index[0]
//30pm = index[1] substring(2) = pm
//30 = index[1] substring(0,2)