diff --git a/src/main/java/io/zipcoder/Problem1.java b/src/main/java/io/zipcoder/Problem1.java index 6cd6024..cd96273 100644 --- a/src/main/java/io/zipcoder/Problem1.java +++ b/src/main/java/io/zipcoder/Problem1.java @@ -1,4 +1,31 @@ package io.zipcoder; + +import java.util.Map; + +// Given the following map { ‘f’ : ‘7’, ‘s’:’$’, ‘1’:’!’, ‘a’.:’@’}, +// your method should replace any character represented by a key in the map, with its corresponding value. +// +// Input = “The Farmer went to the store to get 1 dollar’s worth of fertilizer” +// Output = “The 7@rmer went to the $tore to get ! doll@r’$ worth of 7ertilizer” public class Problem1 { + + + + public static String replaceCharsInString(String input,Map map) { + StringBuilder builder = new StringBuilder(); + + for(int i = 0; i < input.length(); i++ ) { + Character currentChar = input.charAt(i); + + if (map.containsKey(currentChar)) { + currentChar = map.get(currentChar); + //get returns the value of the key + } + builder.append(currentChar); + } + return builder.toString(); + } + + } diff --git a/src/test/java/io/zipcoder/Problem1Test.java b/src/test/java/io/zipcoder/Problem1Test.java index de82e99..0c41540 100644 --- a/src/test/java/io/zipcoder/Problem1Test.java +++ b/src/test/java/io/zipcoder/Problem1Test.java @@ -1,4 +1,36 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; + public class Problem1Test { + + Problem1 problem1; + HashMap map; + String input; + + @Before + public void setUp() { + map = new HashMap(); + map.put('f', '7'); + map.put('s', '$'); + map.put('1', '!'); + map.put('a', '@'); + problem1 = new Problem1(); + input = "The farmer went to the store to get 1 dollar’s worth of fertilizer"; + + } + + @Test + public void Test(){ + + + String expected = "The 7@rmer went to the $tore to get ! doll@r’$ worth o7 7ertilizer"; + String actual = Problem1.replaceCharsInString(input,map); + + Assert.assertEquals(expected,actual); + } }