Commit 3f281737 authored by norbertdajnowski's avatar norbertdajnowski

Commit 1

parent 28c76bab
Pipeline #315 failed with stages
import java.util.HashMap;
import java.util.Map;
public class AssessmentPartTwo {
......@@ -13,7 +15,32 @@ public class AssessmentPartTwo {
// You will need to come up with a way of connecting each letter to its score and
// a way of identifying each letter in the word.
return 0;
int sumOfWord = 0;
for (int i = 0; i <= aWord.length() - 1; i++) { //for loop to traverse through each character in the word
switch (Character.toUpperCase(aWord.charAt(i))) { //switch to the number of points the character gives
case 'E' : case 'A' : case 'I' : case 'O' : case 'N' : case 'R' :
case 'T' : case 'L' : case 'S' : case 'U': sumOfWord = sumOfWord + 1; break;
//break applied on switch statement if character points have been successfully added
case 'D' : case 'G' : sumOfWord = sumOfWord + 2; break;
case 'B' : case 'C' : case 'M' : case 'P' : sumOfWord = sumOfWord + 3; break;
case 'F' : case 'H' : case 'V' : case 'W' : case 'Y' : sumOfWord = sumOfWord + 4; break;
case 'K' : sumOfWord = sumOfWord + 5; break;
case 'J' : case 'X' : sumOfWord = sumOfWord + 8; break;
case 'Q' : case 'Z' : sumOfWord = sumOfWord + 10; break;
}
}
return sumOfWord; //returns the points added up for each character in the word
}
......@@ -28,7 +55,59 @@ public class AssessmentPartTwo {
// - has at least one lower case letter, one upper case letter and a number
// - does not contain the phrases 'password' or 'passwd'
return false;
int validChars = 0;
boolean lwrCase = false;
boolean upperCase = false; //set of variables to determine if password is valid
boolean num = false;
if ((password.length() < 8) || (password.length() > 16)){
return false; //if password is inappropriate length then return false (invalid)
}
if ((password.toLowerCase().contains("password") == true) || (password.toLowerCase().contains("passwd") == true)) {
return false; //if password contains "password", "passwd" or "pass" then return false(invalid)
}
for (int i = 0; i <= password.length() - 1; i++) { //iterates through every character in password
if ((password.charAt(i) > 96) && (password.charAt(i) < 123)){
lwrCase = true;
validChars ++; //checks if a lower case char exists, iterates validChars
}
if ((password.charAt(i) > 64) && (password.charAt(i) < 91)) {
upperCase = true;
validChars ++; //checks if an upper case char exists, iterates validChars
}
if ((password.charAt(i) > 47) && (password.charAt(i) < 58)) {
num = true;
validChars ++; //checks if a number char exists, iterates validChars
}
if ((password.charAt(i) == '') || (password.charAt(i) == '!') || (password.charAt(i) == '$') || (password.charAt(i) == '%')) {
validChars ++; //iterates validChars if the special characters in if statement are used
}
//validChars is compared to password length in the end to check if all characters have matched the validation criteria
}
if ((validChars == password.length()) && (num == true) && (upperCase == true) && (lwrCase == true)) {
return true; //returns password as valid if all of the characters are valid and meet the requirements stated
}
return false; // password must be invalid if this part of the code is reached
}
}
......@@ -37,7 +37,7 @@ class AssessmentPartTwoTest {
"a88drTcdmn45tdgjhj,false",
"pmmfj6793,false",
"PASSWORD,false",
"lo98passiI,false",
"lo98passwdiI,false",
"oi!Fcv98ij,true"
})
void testPasswordValidator(String thePassword, Boolean expectedResult) {
......
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