diff --git a/Drafts/WordSplitDemo/.gitignore b/Drafts/WordSplitDemo/.gitignore new file mode 100644 index 0000000..b7590e2 --- /dev/null +++ b/Drafts/WordSplitDemo/.gitignore @@ -0,0 +1,27 @@ +/out + +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ diff --git a/Drafts/WordSplitDemo/src/RuleInterface.java b/Drafts/WordSplitDemo/src/RuleInterface.java new file mode 100644 index 0000000..d475d57 --- /dev/null +++ b/Drafts/WordSplitDemo/src/RuleInterface.java @@ -0,0 +1,9 @@ +public interface RuleInterface { + + boolean check(String word); + String action(String word); + + default String newMethod() { + return "Test"; + } +} diff --git a/Drafts/WordSplitDemo/src/TransformerRuleLength5.java b/Drafts/WordSplitDemo/src/TransformerRuleLength5.java new file mode 100644 index 0000000..01bba0f --- /dev/null +++ b/Drafts/WordSplitDemo/src/TransformerRuleLength5.java @@ -0,0 +1,12 @@ +public class TransformerRuleLength5 implements RuleInterface { + + @Override + public boolean check(String word) { + return word.length() == 5; + } + + @Override + public String action(String word) { + return word.toLowerCase(); + } +} diff --git a/Drafts/WordSplitDemo/src/TransformerRuleLentgh3.java b/Drafts/WordSplitDemo/src/TransformerRuleLentgh3.java new file mode 100644 index 0000000..c92e037 --- /dev/null +++ b/Drafts/WordSplitDemo/src/TransformerRuleLentgh3.java @@ -0,0 +1,14 @@ +public class TransformerRuleLentgh3 implements RuleInterface { + //no fields -> no state + public boolean check(String input) { + return input.length() == 3; + } + + public String action(String input) { + return input.toUpperCase(); + } + + public String newMethod() { + return "Hello from Rule 3"; + } +} diff --git a/Drafts/WordSplitDemo/src/WordTransformer.java b/Drafts/WordSplitDemo/src/WordTransformer.java new file mode 100644 index 0000000..414489f --- /dev/null +++ b/Drafts/WordSplitDemo/src/WordTransformer.java @@ -0,0 +1,27 @@ +public class WordTransformer { + //length == 3 -> to UpperCase + //transform("aaa bcDE eklm iii") -> "AAA bdDE eklm III" + + //length == 5 -> toLowerCase + //transform("aaa bcDEf eklm iiiPM") -> "aaa bcdef eklm iiipm" + + //length ==2 -> relace first char with * + //transform("aa bcDEf ek") -> "*a bcDEf *k" + + public static String transform(String input, RuleInterface c) { + String[] words = input.split(" "); + for(int i = 0; i< words.length; i++ ) { + String word = words[i]; + if (c.check(word)) { + words[i] = c.action(word); + } + } + return String.join(" ", words); + //nothing is executed after this line + } + + public static String transform(String input) { + RuleInterface c = new TransformerRuleLentgh3(); + return transform(input, c); + } +} diff --git a/Drafts/WordSplitDemo/src/WordTransformerTest.java b/Drafts/WordSplitDemo/src/WordTransformerTest.java new file mode 100644 index 0000000..c205929 --- /dev/null +++ b/Drafts/WordSplitDemo/src/WordTransformerTest.java @@ -0,0 +1,39 @@ +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class WordTransformerTest { + + @Test + public void testTransform() { + //test data + String input = "aaa bcDE eklm iii"; + + //call method we test with test data + String actual = WordTransformer.transform(input); + + //expected result + String expected = "AAA bcDE eklm III"; + + //compare actual and expected + assertEquals("string length", expected.length(), actual.length()); + assertEquals(expected, actual); + } + + @Test + public void testTransformRule5() { + //test data + String input = "aaa bcDEF eklm iiiOP"; + + //call method we test with test data + String actual = WordTransformer.transform(input, new TransformerRuleLength5()); + + //expected result + String expected = "aaa bcdef eklm iiiop"; + + //compare actual and expected + assertEquals("string length", expected.length(), actual.length()); + assertEquals(expected, actual); + } + +}