Commit c33a2831 authored by Leon's avatar Leon

Third Commit

parent aecf333f
public class AssessmentPartThree {
// The simplest form of encryption is the rotation cipher (also known as Caeser's Cipher)
// An offset value is chosen to encrypt a message. Each letter is replaced by the
// letter that that number of places away from it.
// So if an offset value of 1 is chosen, each letter is replaced by the one after it
// - so a becomes b, b becomes c, etc
// If a value of -2 is chosen a becomes y, b becomes z and so on
public char enryptedCharacter(char theChar, int theOffset)
{
// 05 - encryptedCharacter
// Complete this method so that it returns the encrypted character for
// theChar when and offset of theOffset is used
// So if theChar='m' and theOffset is 3 the method will return 'p'
// Lower case characters remain lower case, upper case remain upper case
// Any other characters are returned unchanged
return theChar;
}
public String encryptedString(String theMessage, int theOffset)
{
// 06 - encryptedMessage
// Complete this method so that it uses encryptedCharacter to
// return the encrypted version of theMessage using theOffset
if(theOffset>26) {
theOffset = theOffset%26;
}
else if(theOffset<0) {
theOffset = (theOffset%26)+26;
}//makes sure the Offset is between 1 and 26 so a shift can happen
String newText = "";
int length = theMessage.length();//works out the length of the message to be decrypted
for(int i =0;i<length;i++) {
char ch = theMessage.charAt(i);//goes through and shifts each letter one at a time.
if(Character.isLetter(ch)) {//makes sure it is shitfing a letter
if(Character.isLowerCase(ch)) {//checks to see if the letter is lowercase
char c = (char) (ch + theOffset);//shifts the letter
if (c>'z') {
newText+= (char) (ch - (26-theOffset));//makes sure the new position of the letter if in range
}
else {
newText += c; //adds the new letter to the text
}
}
else if(Character.isUpperCase(ch)) {//checks to see if the letter is uppercase
char c = (char) (ch + theOffset);
if (c>'Z') {
newText+= (char) (ch - (26-theOffset));
}
else {
newText += c;
}
}
}
else {
newText += ch;
}
}
return newText;
}
}
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
class AssessmentPartThreeTest {
public static AssessmentPartThree test;
@BeforeAll
static void setUpBeforeClass() throws Exception {
test = new AssessmentPartThree();
}
@ParameterizedTest
@DisplayName("Testing encryptedCharacter")
@CsvSource({
"a,5,f",
"q,-3,n",
"G,6,M",
"5,-3,5",
"T,-7,M"
})
void testEnryptedCharacter(char theChar, int theOffset, char encChar) {
assertEquals(encChar,test.enryptedCharacter(theChar, theOffset));
}
@ParameterizedTest
@DisplayName("Testing encryptedString")
@CsvSource({
"hello,5,mjqqt",
"Java Coding,-3,Gxsx Zlafkd",
"dandelion,8,livlmtqwv",
"ktixevzout,-6,encryption"
})
void testEncryptedString(String theMessage, int theOffset, String encMessage) {
assertEquals(encMessage, test.encryptedString(theMessage, theOffset));
}
}
...@@ -124,31 +124,31 @@ public class AssessmentPartTwo { ...@@ -124,31 +124,31 @@ public class AssessmentPartTwo {
// - made up of letters (upper or lower), numbers, and the following characters !£$% // - made up of letters (upper or lower), numbers, and the following characters !£$%
// - has at least one lower case letter, one upper case letter and a number // - has at least one lower case letter, one upper case letter and a number
// - does not contain the phrases 'password' or 'passwd' // - does not contain the phrases 'password' or 'passwd'
boolean pass = false; boolean pass = false; //checks to see if the password meets the conditions
boolean hasUppercase = !password.equals(password.toLowerCase()); boolean hasUppercase = !password.equals(password.toLowerCase());//checks if there is a lowercase letter
boolean hasLowercase = !password.equals(password.toUpperCase()); boolean hasLowercase = !password.equals(password.toUpperCase());//checks if there is an uppercase letter
boolean noConditions = !(password.contains("password") || password.contains("passwd")); boolean noConditions = !(password.contains("password") || password.contains("passwd"));//checks it doesn't incude password or passwd
char [] pW = password.toCharArray(); char [] pW = password.toCharArray();//converts the password to a string of characters
if(pW.length <8) { if(pW.length <8) {//checks if the password is too short
pass = false; pass = false;//fails the password if the condition is not met
} }
else if(pW.length>16) { else if(pW.length>16) {//checks if the password is too long
pass = false; pass = false;
} }
else if(!hasUppercase) { else if(!hasUppercase) {//runs the uppercase letter check
pass= false; pass= false;
} }
else if(!hasLowercase) { else if(!hasLowercase) {//runs the lowercase letter check
pass= false; pass= false;
} }
else if(!noConditions) { else if(!noConditions) {//runs the illegal phrase check
pass= false; pass= false;
} }
else { else {
pass = true; pass = true;//if all conditions are met allows the password
} }
return pass; return pass;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment