diff --git a/.gitignore b/.gitignore index d9f7c8d..8265628 100644 --- a/.gitignore +++ b/.gitignore @@ -1,49 +1,8 @@ -# These are some examples of commonly ignored file patterns. -# You should customize this list as applicable to your project. -# Learn more about .gitignore: -# https://www.atlassian.com/git/tutorials/saving-changes/gitignore - -# Node artifact files -node_modules/ -dist/ - -# Compiled Java class files -*.class - -# Compiled Python bytecode -*.py[cod] - -# Log files -*.log - -# Package files -*.jar - -# Maven +#Ignore all build files target/ -dist/ -# JetBrains IDE -.idea/ - -# Unit test reports -TEST*.xml - -# Generated by MacOS -.DS_Store - -# Generated by Windows -Thumbs.db - -# Applications -*.app -*.exe -*.war - -# Large media files -*.mp4 -*.tiff -*.avi -*.flv -*.mov -*.wmv +#Ignore my vscode files +.vscode/ +.settings/ +.classpath +.project diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..0d309af --- /dev/null +++ b/pom.xml @@ -0,0 +1,93 @@ + + + + 4.0.0 + + mattrixwv + CipherStreamJava + 1.0-SNAPSHOT + + CipherStreamJava + http://www.mattrixwv.com + + + UTF-8 + 14 + 14 + + + + + junit + junit + 4.13.2 + test + + + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + 3.0.0-M3 + + + enforce-maven + + enforce + + + + + 3.6.3 + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.2.0 + + + maven-compiler-plugin + 3.8.1 + + + maven-surefile-plugin + 3.0.0-M5 + + + maven-jar-plugin + 3.2.0 + + + maven-install-plugin + 3.0.0-M1 + + + maven-deploy-plugin + 3.0.0-M1 + + + + maven-site-plugin + 3.9.1 + + + maven-project-info-reports-plugin + 3.1.1 + + + + diff --git a/src/main/java/mattrixwv/CipherStreamJava/Caesar.java b/src/main/java/mattrixwv/CipherStreamJava/Caesar.java new file mode 100644 index 0000000..f4fefc4 --- /dev/null +++ b/src/main/java/mattrixwv/CipherStreamJava/Caesar.java @@ -0,0 +1,121 @@ +//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/Caesar.java +//Matthew Ellison +// Created: 07-25-21 +//Modified: 07-25-21 +//This is the declaration of the Caesar class +package mattrixwv.CipherStreamJava; + + +public class Caesar{ + private String inputString; //The string that needs encoded/decoded + private String outputString; //The encoded/decoded string + public static final String version = "1.0"; //The current version number for the library + private int shift; //The amount that you need to shift each letter + //Sets shift and makes sure it is within the propper bounds + private void setShift(int shiftAmount){ + //If you shift more than 26 you will just be wrapping back around again + shift = shiftAmount % 26; + } + //Sets the input string + private void setInputString(String inputString){ + this.inputString = inputString; + } + //Encodes the inputString and stores the result in outputString + private String encode(){ + for(int cnt = 0;cnt < inputString.length();++cnt){ + char temp = inputString.charAt(cnt); //A temperary holder for the current working character + //If it is an upper case letter shift it and wrap if necessary + if(Character.isUpperCase(temp)){ + temp += shift; + //Wrap around if the letter is now out of bounds + if(temp < 'A'){ + temp += 26; + } + else if(temp > 'Z'){ + temp -= 26; + } + } + //If it is a lower case letter shift it and wrap if necessary + else if(Character.isLowerCase(temp)){ + temp += shift; + //Wrap around if the letter is now out of bounds + if(temp < 'a'){ + temp += 26; + } + else if(temp > 'z'){ + temp -= 26; + } + } + //If it is whitespace, number, or punctuation just let it pass through + //Add it to the output string + outputString += temp; + } + return outputString; + } + //Decodes the inputString and stores the result in outputString + private String decode(){ + for(int cnt = 0;cnt < inputString.length();++cnt){ + char temp = inputString.charAt(cnt); //A temperary holder for the current working character + //If it is an upper case letter shift it and wrap if necessary + if(Character.isUpperCase(temp)){ + temp -= shift; + if(temp < 'A'){ + temp += 26; + } + else if(temp > 'Z'){ + temp -= 26; + } + } + //If it is a lower case letter shift it and wrap if necessary + else if(Character.isLowerCase(temp)){ + temp -= shift; + if(temp < 'a'){ + temp += 26; + } + else if(temp > 'z'){ + temp -= 26; + } + } + //If it is whitespace, number, or punctuation just let it pass through + //Add it to the output string + outputString += temp; + } + return outputString; + } + + //Constructor + public Caesar(){ + reset(); + } + //Returns the inputString + public String getInputString(){ + return inputString; + } + //Returns shift + public int getShift(){ + return shift; + } + //Returns the outputString + public String getOutputString(){ + return outputString; + } + //Sets the shift and inputString and encodes the message + public String encode(int shiftAmount, String inputString){ + reset(); + setShift(shiftAmount); + setInputString(inputString); + return encode(); + } + //Sets the shift and inputString and decodes the message + public String decode(int shiftAmount, String inputString){ + reset(); + setShift(shiftAmount); + setInputString(inputString); + return decode(); + } + //Makes sure all of the variables are empty + public void reset(){ + inputString = outputString = ""; + shift = 0; + } +} diff --git a/src/test/java/mattrixwv/CipherStreamJava/TestCaesar.java b/src/test/java/mattrixwv/CipherStreamJava/TestCaesar.java new file mode 100644 index 0000000..998fcfe --- /dev/null +++ b/src/test/java/mattrixwv/CipherStreamJava/TestCaesar.java @@ -0,0 +1,73 @@ +//CipherStreamJava/src/main/java/mattrixwv/CipherStreamJava/TestCaesar.java +//Matthew Ellison +// Created: 07-25-21 +//Modified: 07-25-21 +//These are the tests for the Caesar class +package mattrixwv.CipherStreamJava; + + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + + +public class TestCaesar{ + @Test + public void testDecode(){ + //Test 1 + Caesar cipher = new Caesar(); + String input = "def"; + int shift = 3; + String correctOutput = "abc"; + String output = cipher.decode(shift, input); + assertEquals("Caesar Decoding failed the first test - Expected: " + correctOutput + "; actual: " + output, correctOutput, output); + //Test 2 + input = "def"; + shift = 29; + correctOutput = "abc"; + output = cipher.decode(shift, input); + assertEquals("Caesar Decoding failed the second test - Expected: " + correctOutput + "; actual: " + output, correctOutput, output); + + //Test 3 + input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; + shift = -3; + correctOutput = "The quick brown fox jumps over - the lazy dog"; + output = cipher.decode(shift, input); + assertEquals("Caesar Decoding failed the third test - Expected: " + correctOutput + "; actual: " + output, correctOutput, output); + //Test 4 + input = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; + shift = 23; + correctOutput = "The quick brown fox jumps over - the lazy dog"; + output = cipher.decode(shift, input); + assertEquals("Caesar Decoding failed the fourth test - Expected: " + correctOutput + "; actual: " + output, correctOutput, output); + } + @Test + public void testEncode(){ + //Test 1 + Caesar cipher = new Caesar(); + String input = "abc"; + int shift = 3; + String correctOutput = "def"; + String output = cipher.encode(shift, input); + assertEquals("Caesar Encoding failed the first test - Expected: " + correctOutput + "; actual: " + output, correctOutput, output); + //Test 2 + input = "abc"; + shift = 29; + correctOutput = "def"; + output = cipher.encode(shift, input); + assertEquals("Caesar Encoding failed the second test - Expected: " + correctOutput + "; actual" + output, correctOutput, output); + + //Test 3 + input = "The quick brown fox jumps over - the lazy dog"; + shift = -3; + correctOutput = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; + output = cipher.encode(shift, input); + assertEquals("Caesar Encoding failed the third test - Expected: " + correctOutput + "; actual: " + output, correctOutput, output); + //Test 4 + input = "The quick brown fox jumps over - the lazy dog"; + shift = 23; + correctOutput = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; + output = cipher.encode(shift, input); + assertEquals("Caesar Encoding failed the third test - Expected: " + correctOutput + "; actual: " + output, correctOutput, output); + } +}