Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/main/java/io/zipcoder/MilitaryHour.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.zipcoder;

public enum MilitaryHour {

Zero (0), One (1), Two (2), Three (3), Four (4),
Five (5), Six (6), Seven (7), Eight (8),Nine (9),
Ten (10), Eleven (11), Twelve (12), Thirteen (13), Fourteen (14),
Fifteen (15), Sixteen (16), Seventeen (17), Eighteen (18), Nineteen (19),
Twenty (20), Twentyone (21), Twentytwo (22), Twentythree (23), Twentyfour(24);


Integer hourToMatch;

MilitaryHour(Integer hourToMatch){

this.hourToMatch = hourToMatch;
}
}



27 changes: 27 additions & 0 deletions src/main/java/io/zipcoder/MilitaryMinute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.zipcoder;

public enum MilitaryMinute {


Zero (0), One (1), Two (2), Three (3), Four (4),
Five (5), Six (6), Seven (7), Eight (8),Nine (9),
Ten (10), Eleven (11), Twelve (12), Thirteen (13), Fourteen (14),
Fifteen (15), Sixteen (16), Seventeen (17), Eighteen (18), Nineteen (19),
Twenty (20), Twentyone (21), Twentytwo (22), Twentythree (23), Twentyfour(24),
Twentyfive (25), Twentysix (26), Twentyseven (27), Twentyeight (28), Twentynine (29),
Thirty (30), Thirtyone (31), Thirtytwo (32), Thirtythree (33), Thirtyfour (34),
Thirtyfive (35), Thirtysix (36), Thirtyseven (37), Thirtyeight (38), Thirtynine (39),
Forty (40), Fortyone (41), Fortytwo (42), Fortythree (43), Fortyfour (44),
Fortyfive (45), Fortysix (46), Fortyseven (47), Fortyeight (48),Fortynine (49),
Fifty (50), Fiftyone (51), Fiftytwo (52), Fiftythree (53), Fiftyfour (54),
Fiftyfive (55), Fiftysix (56), Fiftyseven (57), Fiftyeight (58),Fiftynine (59);

Integer minutesToMatch;

MilitaryMinute(Integer minutesToMatch){

this.minutesToMatch = minutesToMatch;
}


}
118 changes: 117 additions & 1 deletion src/main/java/io/zipcoder/Problem6.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,120 @@
package io.zipcoder;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class Problem6 {
}

// Given a time given as a String in numerical format, convert that value into its military time phrase.

/* First Breakdown:

1:30 pm to Thirteen Hundred and Thirty Hours
1:30 am to Zero One Hundred and Thirty Hours
2:22 pm to Fourteen Hundred and Twenty Two Hours

1 - one o'clock (1:00) when it is "am" OR thirteen o clock (13:00) when it is a "pm"

: - convert to "and"
30 - minutes, only output name for the number
pm - indicates if one(1) or thirteen(13), or another two(2) or fourteenth(14)

add "Hours" at the end of the output string

string to int

Second Breakdown:
found SimpleDateFormat class

1:30 pm to 13:30 - Thirteen Hundred and Thirty Hours
if "am" do nothing to it just make it into and int
1:30 am to 1:30 - Zero One Hundred and Thirty Hours
2:22 pm to 14:22 - Fourteen Hundred and Twenty Two Hours
2:11 am to 2:11 - Zero Two Hundred and Eleven Hours

convert from standard to military time 1:30 pm to 13:30
//"13:30" to "13"
then convert military time in numbers to word 1 to Thirteen , 30 to Thirty
split number in front of : into a new variable String hour;
the number after the split put into a new variable String minutes;

String hundredWord = "Hundred
then add "and" after the words split String andWord = "and" or just concatenate later
add "Hours" at the end of the output string String wordHour = "Hours"


output: Thirteen Hundred and Thirty Hours
*/

public static void main(String[] args) throws Exception {

System.out.println(convertToMilitaryTime("1:30 pm"));
//System.out.println(convertToMilitaryTimeInWords(convertToMilitaryTime("1:30 pm")));
}

public static String convertToMilitaryTime(String standardTime) {
DateFormat parseStandardTime = new SimpleDateFormat("hh:mm a");
Date d = null;

try {
d = parseStandardTime.parse(standardTime);
} catch (ParseException e) {
e.printStackTrace();
}

DateFormat displayMilitaryTime = new SimpleDateFormat("HH:mm");
String militaryOutputString = displayMilitaryTime.format(d);
return militaryOutputString;
}

public static int parseMilitaryHour(String militaryOutputString) {

String hourString = militaryOutputString.substring(0, 2);
int hourInt = Integer.parseInt(hourString);
return hourInt;
}

public static int parseMilitaryMinutes(String militaryOutputString) {
String minutesString = militaryOutputString.substring(3);
int minutesInt = Integer.parseInt(minutesString);
return minutesInt;
}

public static String convertHourToWord(int hourInt) {

String hourWord = "";

for (MilitaryHour m : MilitaryHour.values())
if (hourInt == m.hourToMatch)
hourWord = m.toString();

return hourWord;
}


public static String convertMinuteToWord(int minutesInt) {
String minuteWord = "";

for (MilitaryMinute m : MilitaryMinute.values())
if (minutesInt == m.minutesToMatch)
minuteWord = m.toString();

return minuteWord;
}


public static String convertToMilitaryTimeInWords(String hourWord, String minuteWord) {
String hundredWord = "Hundred";
String andWord = "and";
String hoursWord = "Hours";

return hourWord + " " + hundredWord + " " + andWord + " " + minuteWord + " " + hoursWord;


}

}
100 changes: 99 additions & 1 deletion src/test/java/io/zipcoder/Problem6Test.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,102 @@
package io.zipcoder;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.theories.suppliers.TestedOn;

public class Problem6Test {
}

private Problem6 problem;

@Before
public void setUp() {
problem = new Problem6();
}

@Test
public void convertToMilitaryTimeTestOne() {
String expected = "13:30";
String actual = problem.convertToMilitaryTime("1:30 pm");
Assert.assertEquals(expected, actual);
}

@Test
public void convertToMilitaryTimeTestTwo() {
String expected = "01:30";
String actual = problem.convertToMilitaryTime("1:30 am");
Assert.assertEquals(expected, actual);
}

@Test
public void convertToMilitaryTimeTestThree() {
String expected = "14:22";
String actual = problem.convertToMilitaryTime("2:22 pm");
Assert.assertEquals(expected, actual);
}

@Test
public void convertToMilitaryTimeTestFour() {
String expected = "02:22";
String actual = problem.convertToMilitaryTime("2:22 am");
Assert.assertEquals(expected, actual);
}

@Test
public void parseMilitaryHourTest() {
int expected = 13;
int actual = problem.parseMilitaryHour("13:30");
Assert.assertEquals(expected, actual);
}

@Test
public void parseMilitaryMinutesTest() {
int expected = 30;
int actual = problem.parseMilitaryMinutes("13:30");
Assert.assertEquals(expected, actual);
}

@Test
public void convertHourToWordTestONe() {
String expected = "Thirteen";
String actual = problem.convertHourToWord(13);
Assert.assertEquals(expected, actual);
}

@Test
public void convertHourToWordTestTwo() {
String expected = "Twentyfour";
String actual = problem.convertHourToWord(24);
Assert.assertEquals(expected, actual);
}

@Test
public void convertMinuteToWordTestOne() {
String expected = "Seven";
String actual = problem.convertMinuteToWord(7);
Assert.assertEquals(expected, actual);
}

@Test
public void convertMinuteToWordTestTwo() {
String expected = "Fortyone";
String actual = problem.convertMinuteToWord(41);
Assert.assertEquals(expected, actual);
}


@Test
public void convertToMilitaryTimeInWordsTestOne() {
String expected = "Thirteen Hundred and Thirty Hours";
String actual = problem.convertToMilitaryTimeInWords("Thirteen", "Thirty");
Assert.assertEquals(expected, actual);
}

@Test
public void convertToMilitaryTimeInWordsTestTwo() {
String expected = "Fourteen Hundred and Twentytwo Hours";
String actual = problem.convertToMilitaryTimeInWords("Fourteen", "Twentytwo");
Assert.assertEquals(expected, actual);
}

}