Skip to content

Commit 7bbd351

Browse files
authored
Merge pull request #1 from vbetsch/develop
Develop
2 parents 446733a + 882a33a commit 7bbd351

4 files changed

Lines changed: 226 additions & 12 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
package io.github.vbetsch.codecracker;
22

33
public class CodeCracker {
4+
private final TranslationDictionary dictionary = new TranslationDictionary();
45

6+
public char decryptChar(char letter) {
7+
return dictionary.getDecryptions().get(letter);
8+
}
9+
10+
public char encryptLetter(char character) {
11+
return dictionary.getEncryptions().get(character);
12+
}
13+
14+
public String decryptString(String string) {
15+
StringBuilder stringBuilder = new StringBuilder();
16+
for (char character : string.toCharArray()) {
17+
stringBuilder.append(decryptChar(character));
18+
}
19+
return stringBuilder.toString();
20+
}
21+
22+
public String encryptString(String string) {
23+
StringBuilder stringBuilder = new StringBuilder();
24+
for (char character : string.toCharArray()) {
25+
char lowerCaseChar = Character.toLowerCase(character);
26+
if (!dictionary.containsDecryptionValue(lowerCaseChar)) continue;
27+
stringBuilder.append(encryptLetter(lowerCaseChar));
28+
}
29+
return stringBuilder.toString();
30+
}
531
}
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
package io.github.vbetsch.codecracker;
22

3-
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
4-
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
53
public class Main {
64
static void main() {
7-
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
8-
// to see how IntelliJ IDEA suggests fixing it.
9-
IO.println(String.format("Hello and welcome!"));
10-
11-
for (int i = 1; i <= 5; i++) {
12-
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
13-
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
14-
IO.println("i = " + i);
15-
}
5+
CodeCracker codeCracker = new CodeCracker();
6+
IO.println(String.format("Hello world!"));
7+
System.out.println("Encrypted : " + codeCracker.encryptString("Hello world!"));
168
}
179
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.github.vbetsch.codecracker;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import static java.util.Map.entry;
7+
8+
public class TranslationDictionary {
9+
10+
private final Map<Character, Character> decryptions = Map.ofEntries(
11+
entry('!', 'a'),
12+
entry(')', 'b'),
13+
entry('"', 'c'),
14+
entry('(', 'd'),
15+
entry('£', 'e'),
16+
entry('*', 'f'),
17+
entry('%', 'g'),
18+
entry('&', 'h'),
19+
entry('>', 'i'),
20+
entry('<', 'j'),
21+
entry('@', 'k'),
22+
entry('a', 'l'),
23+
entry('b', 'm'),
24+
entry('c', 'n'),
25+
entry('d', 'o'),
26+
entry('e', 'p'),
27+
entry('f', 'q'),
28+
entry('g', 'r'),
29+
entry('h', 's'),
30+
entry('i', 't'),
31+
entry('j', 'u'),
32+
entry('k', 'v'),
33+
entry('l', 'w'),
34+
entry('m', 'x'),
35+
entry('n', 'y'),
36+
entry('o', 'z')
37+
);
38+
39+
private final Map<Character, Character> encryptions = invert(decryptions);
40+
41+
private static Map<Character, Character> invert(Map<Character, Character> map) {
42+
Map<Character, Character> inverted = new HashMap<>();
43+
for (Map.Entry<Character, Character> entry : map.entrySet()) {
44+
inverted.put(entry.getValue(), entry.getKey());
45+
}
46+
return inverted;
47+
}
48+
49+
public Map<Character, Character> getDecryptions() {
50+
return decryptions;
51+
}
52+
53+
public Map<Character, Character> getEncryptions() {
54+
return encryptions;
55+
}
56+
57+
public boolean containsDecryptionValue(char letter) {
58+
return decryptions.containsValue(letter);
59+
}
60+
}

src/test/java/io/github/vbetsch/codecracker/CodeCrackerTest.java

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,151 @@
22

33
import org.junit.jupiter.api.Test;
44

5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
57
class CodeCrackerTest {
68

79
@Test
8-
void should_doSomething_whenSomeCondition() {
10+
void should_returnLetterA_whenDecryptExclamationPoint() {
11+
// Arrange
12+
CodeCracker codeCracker = new CodeCracker();
13+
14+
// Act
15+
char decryptedExclamationPoint = codeCracker.decryptChar('!');
16+
17+
// Assert
18+
assertEquals('a', decryptedExclamationPoint);
19+
}
20+
21+
@Test
22+
void should_returnLetterB_whenDecryptEndParenthesis() {
23+
// Arrange
24+
CodeCracker codeCracker = new CodeCracker();
25+
26+
// Act
27+
char decryptedEndParenthesis = codeCracker.decryptChar(')');
28+
29+
// Assert
30+
assertEquals('b', decryptedEndParenthesis);
31+
}
32+
33+
@Test
34+
void should_returnWordAB_whenDecryptExclamationPointAndEndParenthesis() {
35+
// Arrange
36+
CodeCracker codeCracker = new CodeCracker();
37+
38+
// Act
39+
String decryptedString = codeCracker.decryptString("!)");
40+
41+
// Assert
42+
assertEquals("ab", decryptedString);
43+
}
44+
45+
@Test
46+
void should_decryptWordHello() {
47+
// Arrange
48+
CodeCracker codeCracker = new CodeCracker();
49+
50+
// Act
51+
String decryptedString = codeCracker.decryptString("&£aad");
52+
53+
// Assert
54+
assertEquals("hello", decryptedString);
55+
}
56+
57+
@Test
58+
void should_decryptWordWorld() {
59+
// Arrange
60+
CodeCracker codeCracker = new CodeCracker();
61+
62+
// Act
63+
String decryptedString = codeCracker.decryptString("ldga(");
64+
65+
// Assert
66+
assertEquals("world", decryptedString);
67+
}
68+
69+
@Test
70+
void should_returnExclamationPoint_whenEncryptLetterA() {
71+
// Arrange
72+
CodeCracker codeCracker = new CodeCracker();
73+
74+
// Act
75+
char encryptedLetterA = codeCracker.encryptLetter('a');
76+
77+
// Assert
78+
assertEquals('!', encryptedLetterA);
79+
}
80+
81+
@Test
82+
void should_returnEndParenthesis_whenEncryptLetterB() {
83+
// Arrange
84+
CodeCracker codeCracker = new CodeCracker();
85+
86+
// Act
87+
char encryptedLetterB = codeCracker.encryptLetter('b');
88+
89+
// Assert
90+
assertEquals(')', encryptedLetterB);
91+
}
92+
93+
@Test
94+
void should_returnExclamationPointAndEndParenthesis_whenEncryptAB() {
95+
// Arrange
96+
CodeCracker codeCracker = new CodeCracker();
97+
98+
// Act
99+
String encryptedAB = codeCracker.encryptString("ab");
100+
101+
// Assert
102+
assertEquals("!)", encryptedAB);
103+
}
104+
105+
@Test
106+
void should_encryptWordHello() {
107+
// Arrange
108+
CodeCracker codeCracker = new CodeCracker();
109+
110+
// Act
111+
String encryptedHello = codeCracker.encryptString("hello");
112+
113+
// Assert
114+
assertEquals("&£aad", encryptedHello);
115+
}
116+
117+
@Test
118+
void should_encryptWordWorld() {
9119
// Arrange
10120
CodeCracker codeCracker = new CodeCracker();
11121

12122
// Act
123+
String encryptedWorld = codeCracker.encryptString("world");
124+
13125
// Assert
126+
assertEquals("ldga(", encryptedWorld);
14127
}
15128

129+
@Test
130+
void should_handleUpperCaseInEncrypt() {
131+
// Arrange
132+
CodeCracker codeCracker = new CodeCracker();
133+
134+
// Act
135+
String encryptedHello = codeCracker.encryptString("Hello");
136+
137+
// Assert
138+
assertEquals("&£aad", encryptedHello);
139+
}
140+
141+
@Test
142+
void should_handleUnknownCharacters() {
143+
// Arrange
144+
CodeCracker codeCracker = new CodeCracker();
145+
146+
// Act
147+
String encryptedHello = codeCracker.encryptString("Hello !");
148+
149+
// Assert
150+
assertEquals("&£aad", encryptedHello);
151+
}
16152
}

0 commit comments

Comments
 (0)