diff --git a/exercises/acronym/src/example/java/Acronym.java b/exercises/acronym/src/example/java/Acronym.java index ea80034f7..7a5b3c214 100644 --- a/exercises/acronym/src/example/java/Acronym.java +++ b/exercises/acronym/src/example/java/Acronym.java @@ -2,7 +2,17 @@ import java.util.regex.Pattern; public class Acronym { - public static String generate(String phrase){ + private final String acronym; + + public Acronym(String phrase) { + acronym = generateAcronym(phrase); + } + + public String get() { + return acronym; + } + + private String generateAcronym(String phrase){ final Pattern BREAK_WORDS = Pattern.compile("[A-Z]+[a-z]*|[a-z]+"); final Matcher matcher = BREAK_WORDS.matcher(phrase); final StringBuilder b = new StringBuilder(); diff --git a/exercises/acronym/src/test/java/AcronymTest.java b/exercises/acronym/src/test/java/AcronymTest.java index 64c4ef837..008306f66 100644 --- a/exercises/acronym/src/test/java/AcronymTest.java +++ b/exercises/acronym/src/test/java/AcronymTest.java @@ -5,12 +5,11 @@ public class AcronymTest { - @Test public void fromTitleCasedPhrases() { final String phrase = "Portable Network Graphics"; final String expected = "PNG"; - assertEquals(expected, Acronym.generate(phrase)); + assertEquals(expected, new Acronym(phrase).get()); } @Ignore @@ -18,7 +17,7 @@ public void fromTitleCasedPhrases() { public void fromOtherTitleCasedPhrases() { final String phrase = "Ruby on Rails"; final String expected = "ROR"; - assertEquals(expected, Acronym.generate(phrase)); + assertEquals(expected, new Acronym(phrase).get()); } @Ignore @@ -26,7 +25,7 @@ public void fromOtherTitleCasedPhrases() { public void fromInconsistentlyCasedPhrases() { final String phrase = "HyperText Markup Language"; final String expected = "HTML"; - assertEquals(expected, Acronym.generate(phrase)); + assertEquals(expected, new Acronym(phrase).get()); } @Ignore @@ -34,7 +33,7 @@ public void fromInconsistentlyCasedPhrases() { public void fromPhrasesWithPunctuation() { final String phrase = "First In, First Out"; final String expected = "FIFO"; - assertEquals(expected, Acronym.generate(phrase)); + assertEquals(expected, new Acronym(phrase).get()); } @Ignore @@ -42,7 +41,7 @@ public void fromPhrasesWithPunctuation() { public void fromOtherPhrasesWithPunctuation() { final String phrase = "PHP: Hypertext Preprocessor"; final String expected = "PHP"; - assertEquals(expected, Acronym.generate(phrase)); + assertEquals(expected, new Acronym(phrase).get()); } @Ignore @@ -50,7 +49,7 @@ public void fromOtherPhrasesWithPunctuation() { public void fromPhrasesWithNonAcronymAllCapsWord() { final String phrase = "GNU Image Manipulation Program"; final String expected = "GIMP"; - assertEquals(expected, Acronym.generate(phrase)); + assertEquals(expected, new Acronym(phrase).get()); } @Ignore @@ -58,7 +57,7 @@ public void fromPhrasesWithNonAcronymAllCapsWord() { public void fromPhrasesWithPunctuationAndSentenceCasing() { final String phrase = "Complementary metal-oxide semiconductor"; final String expected = "CMOS"; - assertEquals(expected, Acronym.generate(phrase)); + assertEquals(expected, new Acronym(phrase).get()); } @Ignore @@ -66,7 +65,7 @@ public void fromPhrasesWithPunctuationAndSentenceCasing() { public void fromPhraseWithSingleLetterWord() { final String phrase = "Cat in a Hat"; final String expected = "CIAH"; - assertEquals(expected, Acronym.generate(phrase)); + assertEquals(expected, new Acronym(phrase).get()); } }