Skip to content

Commit 5df46fc

Browse files
authored
Merge pull request #1 from Chal13W1zz/backend
Add Backend logic.
2 parents 361e96e + ae18931 commit 5df46fc

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/main/java/CaesarShift.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.ArrayList;
2+
3+
public class CaesarShift {
4+
private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //Encapsulate and make the alphabet immutable
5+
private static int key = 25; //Initialize and encapsulate the shift key
6+
private static String encryptedMsg; //Create and encapsulate the message container
7+
private static String decryptedMsg;
8+
9+
public String encrypt(String encryptMsg){
10+
String upCased = encryptMsg.toUpperCase();
11+
char[] upCasedArrs = upCased.toCharArray();//split the string to a character array
12+
ArrayList<Character> encryptedChars = new ArrayList<Character>();
13+
14+
//loop through the character array
15+
for(Character character : upCasedArrs ){
16+
int index = ALPHABET.indexOf(character.toString());//get the character rank in the alphabet
17+
int encryptedCharIndex = Math.floorMod((index+key),26);//shift the character using the key and get the new characters rank in the alphabet
18+
encryptedChars.add(ALPHABET.charAt(encryptedCharIndex));//get the character from the alphabet rank and add it to the char array
19+
encryptedMsg = encryptedChars.toString().replaceAll("\\[|\\]|\\s","").replaceAll(",","");//convert and cleanup the char array to a string
20+
}
21+
return encryptedMsg;
22+
}
23+
24+
public String decrypt(String decryptMsg) {
25+
String upCased = decryptMsg.toUpperCase();
26+
char[] upCasedArrs = upCased.toCharArray();//split the string to a character array
27+
ArrayList<Character> decryptedChars = new ArrayList<Character>();
28+
29+
//loop through the character array
30+
for (Character character : upCasedArrs) {
31+
int index = ALPHABET.indexOf(character.toString());//get the character rank in the alphabet
32+
int decryptedCharIndex = Math.floorMod((index - key), 26);//shift the character using the key and get the new characters rank in the alphabet
33+
decryptedChars.add(ALPHABET.charAt(decryptedCharIndex));//get the character from the alphabet rank and add it to the char array
34+
decryptedMsg = decryptedChars.toString().replaceAll("\\[|\\]|\\s", "").replaceAll(",", "");//convert and cleanup the char array to a string
35+
}
36+
return decryptedMsg;
37+
38+
}
39+
40+
}

src/test/java/CaesarShiftTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import org.junit.jupiter.api.Test;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
class CaesarShiftTest {
6+
@Test
7+
public void caesarShift_isEncryptUpcasedTest_Boolean() {
8+
CaesarShift encryptCaseCheck = new CaesarShift();
9+
assertEquals(true, Character.isUpperCase(encryptCaseCheck.encrypt("a").charAt(0)));
10+
}
11+
12+
@Test
13+
void caesarShift_encryptStringTest_String() {
14+
CaesarShift encryptString = new CaesarShift();
15+
assertEquals("GDKKN", encryptString.encrypt("hello"));
16+
}
17+
18+
@Test
19+
public void caesarShift_isDecryptMsgUpcasedTest_String() {
20+
CaesarShift caseTest = new CaesarShift();
21+
assertEquals(true, Character.isUpperCase(caseTest.decrypt("a").charAt(0)));
22+
}
23+
24+
@Test
25+
public void caesarShift_decryptStringTest_String(){
26+
CaesarShift decryptStringTest = new CaesarShift();
27+
assertEquals("Z", decryptStringTest.decrypt("Y"));
28+
}
29+
30+
31+
}

0 commit comments

Comments
 (0)