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

All finished #24

Open
wants to merge 1 commit 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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>io.zipcoder</groupId>
<artifactId>InterviewProblem5</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
Expand Down
152 changes: 152 additions & 0 deletions src/main/java/io/zipcoder/Problem6.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,156 @@
package io.zipcoder;

import java.util.ArrayList;

public class Problem6 {

private ArrayList<String> hoursArrayList = new ArrayList<>();
private ArrayList<String> minutesArrayList = new ArrayList<>();

public Problem6() {
populateHours();
populateMinutes();
}

public String convertToMilitaryTime(String input) {
String militaryTime = "";

boolean isPm = checkIfPm(input);
Integer hours = getHours(input);
Integer minutes = getMinutes(input);

militaryTime += convertHoursToString(hours, isPm);
militaryTime += " Hundred ";
militaryTime += convertMinutesToString(minutes);
militaryTime += " Hours";

return militaryTime;
}

public boolean checkIfPm(String input) {
input = input.toLowerCase();
return input.contains("pm");
}

public Integer getHours(String input) throws NumberFormatException{
String[] splitAtColon = input.split(":");
String hoursString = splitAtColon[0];
return verifyValidInput(hoursString);
}

public Integer getMinutes(String input) throws NumberFormatException {
String[] splitAtColon = input.split(":");
String minutesString = splitAtColon[1].substring(0,2);
return verifyValidInput(minutesString);
}

public Integer verifyValidInput(String input) throws NumberFormatException {
Integer returnInteger;
try {
returnInteger = Integer.parseInt(input);
return returnInteger;
}
catch (NumberFormatException e) {
throw new NumberFormatException();
}
}

public void populateHours() {
hoursArrayList.add("Nothing");
hoursArrayList.add("One");
hoursArrayList.add("Two");
hoursArrayList.add("Three");
hoursArrayList.add("Four");
hoursArrayList.add("Five");
hoursArrayList.add("Six");
hoursArrayList.add("Seven");
hoursArrayList.add("Eight");
hoursArrayList.add("Nine");
hoursArrayList.add("Ten");
hoursArrayList.add("Eleven");
hoursArrayList.add("Twelve");
hoursArrayList.add("Thirteen");
hoursArrayList.add("Fourteen");
hoursArrayList.add("Fifteen");
hoursArrayList.add("Sixteen");
hoursArrayList.add("Seventeen");
hoursArrayList.add("Eighteen");
hoursArrayList.add("Nineteen");
hoursArrayList.add("Twenty");
hoursArrayList.add("Twenty One");
hoursArrayList.add("Twenty Two");
hoursArrayList.add("Twenty Three");
hoursArrayList.add("Twenty Four");
}

public void populateMinutes() {
for (String hour : hoursArrayList) {
minutesArrayList.add(hour);
}
minutesArrayList.add("Twenty Five");
minutesArrayList.add("Twenty Six");
minutesArrayList.add("Twenty Seven");
minutesArrayList.add("Twenty Eight");
minutesArrayList.add("Twenty Nine");
minutesArrayList.add("Thirty");
minutesArrayList.add("Thirty One");
minutesArrayList.add("Thirty Two");
minutesArrayList.add("Thirty Three");
minutesArrayList.add("Thirty Four");
minutesArrayList.add("Thirty Five");
minutesArrayList.add("Thirty Six");
minutesArrayList.add("Thirty Seven");
minutesArrayList.add("Thirty Eight");
minutesArrayList.add("Thirty Nine");
minutesArrayList.add("Forty");
minutesArrayList.add("Forty One");
minutesArrayList.add("Forty Two");
minutesArrayList.add("Forty Three");
minutesArrayList.add("Forty Four");
minutesArrayList.add("Forty Five");
minutesArrayList.add("Forty Six");
minutesArrayList.add("Forty Seven");
minutesArrayList.add("Forty Eight");
minutesArrayList.add("Forty Nine");
minutesArrayList.add("Fifty");
minutesArrayList.add("Fifty One");
minutesArrayList.add("Fifty Two");
minutesArrayList.add("Fifty Three");
minutesArrayList.add("Fifty Four");
minutesArrayList.add("Fifty Five");
minutesArrayList.add("Fifty Six");
minutesArrayList.add("Fifty Seven");
minutesArrayList.add("Fifty Eight");
minutesArrayList.add("Fifty Nine");
}

public ArrayList<String> getHoursArrayList() {
return hoursArrayList;
}

public ArrayList<String> getMinutesArrayList() {
return minutesArrayList;
}

public String convertHoursToString(Integer hour, boolean isPm) {
String hours = "";
if (!isPm && hour < 10) {
hours += "Zero ";
}
if (isPm) {
return hours + hoursArrayList.get(hour + 12);
}
return hours + hoursArrayList.get(hour);
}

public String convertMinutesToString(Integer minute) {
String minutes = "";
if (minute < 10) {
minutes += "Zero ";
}
else {
minutes += "and ";
}
return minutes + minutesArrayList.get(minute);
}
}
142 changes: 142 additions & 0 deletions src/test/java/io/zipcoder/Problem6Test.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,146 @@
package io.zipcoder;

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

public class Problem6Test {

Problem6 problem6;

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

@Test
public void convertToMilitaryTimeTest_Input130pm() {
String input = "1:30pm";
String expected = "Thirteen Hundred and Thirty Hours";
String actual = problem6.convertToMilitaryTime(input);

Assert.assertEquals(expected, actual);
}

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

String actual = problem6.convertToMilitaryTime(input);

Assert.assertEquals(expected, actual);
}

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

String actual = problem6.convertToMilitaryTime(input);

Assert.assertEquals(expected, actual);
}

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

String actual = problem6.convertToMilitaryTime(input);

Assert.assertEquals(expected, actual);
}

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

String actual = problem6.convertToMilitaryTime(input);
Assert.assertEquals(expected, actual);
}

@Test
public void checkIfPmTest_expectedTrue() {
String input = "1:30pm";
Assert.assertTrue(problem6.checkIfPm(input));
}

@Test
public void checkIfPmTest_expectedFalse() {
String input = "5:30am";
Assert.assertFalse(problem6.checkIfPm(input));
}

@Test
public void getHoursTest_expected1() {
String input = "1:30pm";
Integer expected = 1;
Integer actual = problem6.getHours(input);
Assert.assertEquals(expected, actual);
}

@Test (expected = NumberFormatException.class)
public void verifyValidInputTestFromGetHours_ExpectedNumberFormatException() {
String input = "Tariq:30pm";
Integer actual = problem6.getHours(input);
}

@Test
public void getMinutesTest_expected30() {
String input = "1:30pm";
Integer expected = 30;
Integer actual = problem6.getMinutes(input);
Assert.assertEquals(expected, actual);
}

@Test (expected = NumberFormatException.class)
public void verifyValidInputTestFromGetMinutes_expectedNumberFormatException() {
String input = "1:lolpm";
Integer actual = problem6.getMinutes(input);
}

@Test
public void testPopulateHours_ExpectedSize25() {
int expected = 25;
int actual = problem6.getHoursArrayList().size();

Assert.assertEquals(expected, actual);
}

@Test
public void testPopulateMinutes_ExpectedSize60() {
int expected = 60;
int actual = problem6.getMinutesArrayList().size();

Assert.assertEquals(expected, actual);
}

@Test
public void convertHoursToString_expectedFourteen() {
String input = "2:30pm";
String expected = "Fourteen";

Integer hours = problem6.getHours(input);

boolean isPm = problem6.checkIfPm(input);

String actual = problem6.convertHoursToString(hours, isPm);

Assert.assertEquals(expected, actual);
}

@Test
public void convertMinutesToString_expectedThirty() {
String input = "2:30pm";
String expected = "and Thirty";

Integer minutes = problem6.getMinutes(input);

String actual = problem6.convertMinutesToString(minutes);

Assert.assertEquals(expected, actual);
}

}