Skip to content
40 changes: 40 additions & 0 deletions src/main/java/CaesarShift.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.ArrayList;

public class CaesarShift {
private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //Encapsulate and make the alphabet immutable
private static int key = 25; //Initialize and encapsulate the shift key
private static String encryptedMsg; //Create and encapsulate the message container
private static String decryptedMsg;

public String encrypt(String encryptMsg){
String upCased = encryptMsg.toUpperCase();
char[] upCasedArrs = upCased.toCharArray();//split the string to a character array
ArrayList<Character> encryptedChars = new ArrayList<Character>();

//loop through the character array
for(Character character : upCasedArrs ){
int index = ALPHABET.indexOf(character.toString());//get the character rank in the alphabet
int encryptedCharIndex = Math.floorMod((index+key),26);//shift the character using the key and get the new characters rank in the alphabet
encryptedChars.add(ALPHABET.charAt(encryptedCharIndex));//get the character from the alphabet rank and add it to the char array
encryptedMsg = encryptedChars.toString().replaceAll("\\[|\\]|\\s","").replaceAll(",","");//convert and cleanup the char array to a string
}
return encryptedMsg;
}

public String decrypt(String decryptMsg) {
String upCased = decryptMsg.toUpperCase();
char[] upCasedArrs = upCased.toCharArray();//split the string to a character array
ArrayList<Character> decryptedChars = new ArrayList<Character>();

//loop through the character array
for (Character character : upCasedArrs) {
int index = ALPHABET.indexOf(character.toString());//get the character rank in the alphabet
int decryptedCharIndex = Math.floorMod((index - key), 26);//shift the character using the key and get the new characters rank in the alphabet
decryptedChars.add(ALPHABET.charAt(decryptedCharIndex));//get the character from the alphabet rank and add it to the char array
decryptedMsg = decryptedChars.toString().replaceAll("\\[|\\]|\\s", "").replaceAll(",", "");//convert and cleanup the char array to a string
}
return decryptedMsg;

}

}
31 changes: 31 additions & 0 deletions src/test/java/CaesarShiftTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class CaesarShiftTest {
@Test
public void caesarShift_isEncryptUpcasedTest_Boolean() {
CaesarShift encryptCaseCheck = new CaesarShift();
assertEquals(true, Character.isUpperCase(encryptCaseCheck.encrypt("a").charAt(0)));
}

@Test
void caesarShift_encryptStringTest_String() {
CaesarShift encryptString = new CaesarShift();
assertEquals("GDKKN", encryptString.encrypt("hello"));
}

@Test
public void caesarShift_isDecryptMsgUpcasedTest_String() {
CaesarShift caseTest = new CaesarShift();
assertEquals(true, Character.isUpperCase(caseTest.decrypt("a").charAt(0)));
}

@Test
public void caesarShift_decryptStringTest_String(){
CaesarShift decryptStringTest = new CaesarShift();
assertEquals("Z", decryptStringTest.decrypt("Y"));
}


}