-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathProblem6.java
More file actions
140 lines (125 loc) · 4.73 KB
/
Problem6.java
File metadata and controls
140 lines (125 loc) · 4.73 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package io.zipcoder;
import java.util.HashMap;
public class Problem6 {
private HashMap<String, String> hours = new HashMap<String, String>();
private HashMap<String, String> minutes = new HashMap<String, String>();
private HashMap<String, String> multiplesOfTen = new HashMap<String, String>();
private String[] input = {};
public Problem6(){
populateHours();
populateMinutes();
populateMultiplesOfTen();
}
// Converts String input to Military Time result
public String convertToMilitaryTime(String input) {
this.input = splitTime(input);
if(inTheMorning(this.input)){
return getHourInMilitaryAM(this.input[0]) +
" Hundred and " +
getMinutesInMilitary(this.input[1]) +
" Hours";
} else {
return getHourInMilitaryPM(this.input[0]) +
" Hundred and " +
getMinutesInMilitary(this.input[1]) +
" Hours";
}
}
// Splits input into array // example: {10, 45, am}
protected String[] splitTime(String time){
String[] splitTimes = time.split(":");
String[] endSplitTime = {
splitTimes[0],
splitTimes[1].substring(0, 2),
time.substring(time.length() - 2)
};
return endSplitTime;
}
// Checks if the input contains (am) - morning time
protected boolean inTheMorning(String[] testArray){
return (testArray[2].equalsIgnoreCase("am"));
}
// Checks what number the minutes starts with and feeds to getMinutesInMilitary
private String minuteNumberStartsWith(String[] timeArray){
String firstMinuteNumber = "";
for(int i = 0; i < 6; i++){
if(timeArray[1].substring(0, 1).equals(String.valueOf(i))){
firstMinuteNumber += String.valueOf(i + "0");
break;
}
}
return firstMinuteNumber;
}
protected String getHourInMilitaryAM(String hour){
if(Integer.parseInt(hour) == 12){
return hours.get("0");
} else {
return hours.get(hour);
}
}
protected String getHourInMilitaryPM(String hour){
Integer pmHour = Integer.parseInt(hour) + 12;
if(Integer.parseInt(hour) == 12){
return hours.get("12");
} else {
return hours.get(String.valueOf(pmHour));
}
}
// Converts standard minutes to military minutes
protected String getMinutesInMilitary(String inputMinutes){
if (Integer.parseInt(inputMinutes.substring(0, 2)) < 10) {
return hours.get(inputMinutes.substring(inputMinutes.length() - 1));
} else if (Integer.parseInt(inputMinutes.substring(0, 2)) < 20){
return hours.get(inputMinutes);
} else if (Integer.parseInt(inputMinutes.substring(0, 2)) % 10 == 0) {
return multiplesOfTen.get(minuteNumberStartsWith(this.input));
} else {
return multiplesOfTen.get(minuteNumberStartsWith(this.input)) + " " +
minutes.get(inputMinutes.substring(inputMinutes.length() - 1));
}
}
private void populateHours(){
hours.put("0", "Zero Zero");
hours.put("1", "Zero One");
hours.put("2", "Zero Two");
hours.put("3", "Zero Three");
hours.put("4", "Zero Four");
hours.put("5", "Zero Five");
hours.put("6", "Zero Six");
hours.put("7", "Zero Seven");
hours.put("8", "Zero Eight");
hours.put("9", "Zero Nine");
hours.put("10", "Ten");
hours.put("11", "Eleven");
hours.put("12", "Twelve");
hours.put("13", "Thirteen");
hours.put("14", "Fourteen");
hours.put("15", "Fifteen");
hours.put("16", "Sixteen");
hours.put("17", "Seventeen");
hours.put("18", "Eighteen");
hours.put("19", "Nineteen");
hours.put("20", "Twenty");
hours.put("21", "Twenty One");
hours.put("22", "Twenty Two");
hours.put("23", "Twenty Three");
}
private void populateMinutes(){
minutes.put("1", "One");
minutes.put("2", "Two");
minutes.put("3", "Three");
minutes.put("4", "Four");
minutes.put("5", "Five");
minutes.put("6", "Six");
minutes.put("7", "Seven");
minutes.put("8", "Eight");
minutes.put("9", "Nine");
}
private void populateMultiplesOfTen(){
multiplesOfTen.put("10", "Ten");
multiplesOfTen.put("20", "Twenty");
multiplesOfTen.put("30", "Thirty");
multiplesOfTen.put("40", "Forty");
multiplesOfTen.put("50", "Fifty");
}
}