diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..773c56d --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,9 @@ +.classpath +.project +.settings/ +target/ +nbproject/* +.settings +runtime/* +*~ +.DS_Store diff --git a/bin/README.md b/bin/README.md new file mode 100644 index 0000000..42412fc --- /dev/null +++ b/bin/README.md @@ -0,0 +1,33 @@ +# Archarithm's Coding Challenge - Overview + +## Software Requirements +Java - https://www.java.com/en/ + +Maven - http://maven.apache.org + +Junit 5 - https://junit.org/junit5/ + +## Getting started +Clone the project, clean, and compile (no unit tests) with: `mvn clean install -DskipTests`. + +Run unit tests with: `mvn test` or `mvn install`. + +Refer to the coding challenge instructions found in `codingChallenge.md` + +Complete both of the challenges listed in `codingChallenge.md` and follow the instructions below to submit +your solution. Good luck! + +## Instructions +1. Create a feature branch + +2. Create all challenge 1 unit tests (and write additional unit tests if needed) in `com.github.archarithms.test.TitleCaseTests`. + +3. Write corresponding method in `com.github.archarithms.App` for challenge 1. + +4. Create all challenge 2 unit tests (and write additional unit tests if needed) in `com.github.archarithms.test.DateStringTests`. + +5. Write corresponding method in `com.github.archarithms.App` for challenge 2. + +6. Ensure all unit tests run successfully with: `mvn test`. + +7. Push your feature branch and submit a pull request. \ No newline at end of file diff --git a/bin/codingChallenge.md b/bin/codingChallenge.md new file mode 100644 index 0000000..bdebf15 --- /dev/null +++ b/bin/codingChallenge.md @@ -0,0 +1,26 @@ +# Archarithm's Coding Challenge - 1 & 2 + +## Challenge 1 +Create a function that takes in a constant-formatted (all caps, +words separated by underscores) string and returns the string +in "title case" (see examples below). The function should remove all non alphanumeric +characters and replace them with spaces. But no spaces at the end +of the result. A `null` input should throw an error. + +If you so choose feel free to leverage third party libraries to complete your solution. + +For example: +`"THIS_INPUT"` should return `"This Input"` +`"CASE-THREE_extra[chars]///"` should return `"Case Three Extra Chars"` + + +## Challenge 2 +Create a function that takes in a unix epoch time in seconds (long data type) and returns a String +that is the input timestamp converted and formatted as month day, year (see below). If no timestamp is given the function +should return today’s date. If the input is null or has an incorrect type, an error should be +thrown. + +If you so choose feel free to leverage third party libraries to complete your solution. + +For example, passing a unix epoch time `1499144400` in seconds (long data type) +would return `"July 4, 2017"` in this date format (string data type). diff --git a/bin/pom.xml b/bin/pom.xml new file mode 100644 index 0000000..8f6b076 --- /dev/null +++ b/bin/pom.xml @@ -0,0 +1,56 @@ + + 4.0.0 + + com.github.archarithms + java-interview + 0.0.1 + jar + + java-interview + http://archarithms.com + + + UTF-8 + 1.8 + 3.8.1 + 2.22.1 + ${maven.compiler.source} + 5.4.2 + 2.9.7 + + + + + org.junit.jupiter + junit-jupiter-api + ${junit.jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + joda-time + joda-time + ${joda.time.version} + + + + + + + maven-compiler-plugin + ${maven.compiler.version} + + + maven-surefire-plugin + ${maven.surefire.version} + + + + diff --git a/bin/src/main/java/com/github/archarithms/App.class b/bin/src/main/java/com/github/archarithms/App.class new file mode 100644 index 0000000..ff3e69b Binary files /dev/null and b/bin/src/main/java/com/github/archarithms/App.class differ diff --git a/bin/src/test/java/com/github/archarithms/test/DateStringTests.class b/bin/src/test/java/com/github/archarithms/test/DateStringTests.class new file mode 100644 index 0000000..4e34280 Binary files /dev/null and b/bin/src/test/java/com/github/archarithms/test/DateStringTests.class differ diff --git a/bin/src/test/java/com/github/archarithms/test/TitleCaseTests.class b/bin/src/test/java/com/github/archarithms/test/TitleCaseTests.class new file mode 100644 index 0000000..745c68f Binary files /dev/null and b/bin/src/test/java/com/github/archarithms/test/TitleCaseTests.class differ diff --git a/pom.xml b/pom.xml index 8f6b076..25acd3b 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,11 @@ joda-time ${joda.time.version} + + org.apache.commons + commons-lang3 + 3.11 + diff --git a/src/main/java/com/github/archarithms/App.java b/src/main/java/com/github/archarithms/App.java index dcb6723..9726d7d 100644 --- a/src/main/java/com/github/archarithms/App.java +++ b/src/main/java/com/github/archarithms/App.java @@ -1,15 +1,28 @@ package com.github.archarithms; +import java.io.IOException; +import java.util.Date; + +import org.apache.commons.lang3.text.WordUtils; public class App { public static String convertToTitleCase(final String inpStr) { - return inpStr; + String cleanString = inpStr.replaceAll("[^a-zA-Z0-9]", " "); + return WordUtils.capitalizeFully(cleanString); } - public static String convertUnixToDateString(final Long inpUnixSeconds) + public static String convertUnixToDateString(final Long inpUnixSeconds) throws IOException { - return String.valueOf(inpUnixSeconds); + if(inpUnixSeconds == null) + throw new IOException("null input"); + Date finalDate = new Date(inpUnixSeconds*1000); + DateObject returnDate = new DateObject(finalDate); + return String.valueOf(formatDate(returnDate)); + } + + private static String formatDate(DateObject dateObject) { + return String.valueOf(dateObject.getMonth() + " " + dateObject.getDay() + ", " + dateObject.getYear()); } } diff --git a/src/main/java/com/github/archarithms/DateObject.java b/src/main/java/com/github/archarithms/DateObject.java new file mode 100644 index 0000000..fb9ed95 --- /dev/null +++ b/src/main/java/com/github/archarithms/DateObject.java @@ -0,0 +1,93 @@ +package com.github.archarithms; + +import java.time.Month; +import java.time.format.TextStyle; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; + +public class DateObject { + + private int year; + private String month; + private int day; + + /*The md says to throw an exception if null or wrong type and provide today's date if "no timestamp is given" + * Since the function takes in a long, it can't take a "wrong type" and if it's null it throws an exception + * So I'm not really sure how this would be called, but I added a default constructor that returns today's date if no parameter is passed in + */ + public DateObject() { + this.year = Calendar.getInstance().get(Calendar.YEAR); + this.month = Month.of(Calendar.getInstance().get(Calendar.MONTH)).getDisplayName(TextStyle.FULL_STANDALONE, null); + this.day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); + } + + public DateObject(int year, String month, int day) { + super(); + this.year = year; + this.month = month; + this.day = day; + } + + public DateObject(Date date) { + this.year = date.getYear() + 1900; + this.month = Month.of(date.getMonth()+1).getDisplayName(TextStyle.FULL_STANDALONE, Locale.ENGLISH); + this.day = date.getDate(); + } + + public int getYear() { + return year; + } + + public void setYear(int year) { + this.year = year; + } + + public String getMonth() { + return month; + } + + public void setMonth(String month) { + this.month = month; + } + + public int getDay() { + return day; + } + + public void setDay(int day) { + this.day = day; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + day; + result = prime * result + ((month == null) ? 0 : month.hashCode()); + result = prime * result + year; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DateObject other = (DateObject) obj; + if (day != other.day) + return false; + if (month == null) { + if (other.month != null) + return false; + } else if (!month.equals(other.month)) + return false; + if (year != other.year) + return false; + return true; + } + +} diff --git a/src/test/java/com/github/archarithms/test/DateStringTests.java b/src/test/java/com/github/archarithms/test/DateStringTests.java index 04e9943..c5e8aa1 100644 --- a/src/test/java/com/github/archarithms/test/DateStringTests.java +++ b/src/test/java/com/github/archarithms/test/DateStringTests.java @@ -2,6 +2,9 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.IOException; import org.junit.jupiter.api.Test; @@ -22,8 +25,13 @@ public class DateStringTests */ @Test public void testConvertUnixToDateString() { - String testStr = "July 4, 2017"; - assertTrue(testStr.equals(App.convertUnixToDateString(1499144400L))); + try { + String testStr = "July 4, 2017"; + String result = App.convertUnixToDateString(1499144400L); + assertTrue(testStr.equals(result)); + } catch(IOException e) { + fail("Exception thrown"); + } } /**