Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tried to clean it up but it's still pretty dirty #42

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
105 changes: 105 additions & 0 deletions src/main/java/io/zipcoder/MilitaryTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package io.zipcoder;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MilitaryTime {

private static String[] timeWords = {
"Zero",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen",
};

private static String[] tensWords = {
"Twenty",
"Thirty",
"Forty",
"Fifty"
};

public String convertToMilitaryTime(String time) throws Exception{
time = convertNumerically(time);
String[] splitTime = time.split(":");
Integer hours = Integer.parseInt(splitTime[0]);
Integer minutes = Integer.parseInt(splitTime[1]);

String output = new StringBuilder()
.append(convertHours(hours))
.append(convertMinutes(minutes))
.toString();

return output;
}

public String convertHours(Integer hours) {
StringBuilder output = new StringBuilder();
if(hours >= 10 && hours < 20) {
output.append(timeWords[hours] + " Hundred and ");
} else if(hours < 10) {
output.append(timeWords[hours / 10]);
output.append(" ");
output.append(timeWords[hours % 10] + " Hundred and ");
} else if(hours > 19) {
output.append(tensWords[(hours / 10) - 2]);
output.append(" ");
output.append(timeWords[hours % 10] + " Hundred and ");
}
return output.toString();
}

public String convertMinutes(Integer minutes) {
StringBuilder output = new StringBuilder();
if(minutes > 10 && minutes < 20) {
output.append(timeWords[minutes] + " Hours");
} else if(minutes < 10) {
output.append(timeWords[minutes / 10]);
output.append(" ");
output.append(timeWords[minutes % 10] + " Hours");
} else if(minutes > 19) {
output.append(tensWords[(minutes / 10) - 2]);
output.append(" ");
if(minutes % 10 != 0) {
output.append(timeWords[minutes % 10]);
output.append(" ");
}
output.append("Hours");
}
return output.toString();
}

public String convertNumerically(String time) throws Exception {
SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
Date date = parseFormat.parse(formatTime(time));
return displayFormat.format(date);
}

public String formatTime(String time) {
String[] splitTime = time.split("");
for(int i = splitTime.length - 1; i > splitTime.length - 4; i--) {
if(i == splitTime.length - 3) {
splitTime[i] = splitTime[i] + " ";
}
splitTime[i] = splitTime[i].toUpperCase();
}
return String.join("", splitTime);
}
}
4 changes: 0 additions & 4 deletions src/main/java/io/zipcoder/Problem6.java

This file was deleted.

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

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;


public class MilitaryTimeTest {

private MilitaryTime militaryTime;

@Before
public void setup() {
militaryTime = new MilitaryTime();
}

@Test
public void formatTest() {
String input = "11:30pm";
String expected = "11:30 PM";

String actual = militaryTime.formatTime(input);

Assert.assertEquals(expected, actual);
}

@Test
public void convertNumericallyPMTest() throws Exception{
String input = "1:30pm";
String expected = "13:30";

String actual = militaryTime.convertNumerically(input);

Assert.assertEquals(expected, actual);
}

@Test
public void convertNumericallyAMTest() throws Exception {
String input = "1:30am";
String expected = "01:30";

String actual = militaryTime.convertNumerically(input);
Assert.assertEquals(expected, actual);
}

@Test
public void convertHoursTest() {
Integer input = 11;
String expected = "Eleven Hundred and ";

String actual = militaryTime.convertHours(input);

Assert.assertEquals(expected, actual);
}

@Test
public void convertHoursTest2() {
Integer input = 23;
String expected = "Twenty Three Hundred and ";

String actual = militaryTime.convertHours(input);

Assert.assertEquals(expected, actual);
}

@Test
public void convertHoursTest3() {
Integer input = 58;
String expected = "Fifty Eight Hundred and ";

String actual = militaryTime.convertHours(input);

Assert.assertEquals(expected, actual);
}

@Test
public void convertHoursTest4() {
Integer input = 3;
String expected = "Zero Three Hundred and ";

String actual = militaryTime.convertHours(input);

Assert.assertEquals(expected, actual);
}

@Test
public void convertMinutesTest() {
Integer input = 3;
String expected = "Zero Three Hours";

String actual = militaryTime.convertMinutes(input);

Assert.assertEquals(expected, actual);
}

@Test
public void convertMinutesTest2() {
Integer input = 14;
String expected = "Fourteen Hours";

String actual = militaryTime.convertMinutes(input);

Assert.assertEquals(expected, actual);
}

@Test
public void militaryTimePM() throws Exception {
String input = "1:30pm";
String expected = "Thirteen Hundred and Thirty Hours";

String actual = militaryTime.convertToMilitaryTime(input);

Assert.assertEquals(expected, actual);
}

@Test
public void militaryTimePM2() throws Exception {
String input = "2:22pm";
String expected = "Fourteen Hundred and Twenty Two Hours";

String actual = militaryTime.convertToMilitaryTime(input);

Assert.assertEquals(expected, actual);
}

@Test
public void militaryTimeAM() throws Exception {
String input = "1:30am";
String expected = "Zero One Hundred and Thirty Hours";

String actual = militaryTime.convertToMilitaryTime(input);

Assert.assertEquals(expected, actual);
}

@Test
public void militaryTimeAM2() throws Exception {
String input = "2:11am";
String expected = "Zero Two Hundred and Eleven Hours";

String actual = militaryTime.convertToMilitaryTime(input);

Assert.assertEquals(expected, actual);
}

@Test
public void militaryTimeAM3() throws Exception {
String input = "10:02am";
String expected = "Ten Hundred and Zero Two Hours";

String actual = militaryTime.convertToMilitaryTime(input);

Assert.assertEquals(expected, actual);
}

}
4 changes: 0 additions & 4 deletions src/test/java/io/zipcoder/Problem6Test.java

This file was deleted.