Commit 84d2d7e1 authored by neil.whitehead's avatar neil.whitehead

12:19 10/12/18

parent 8982a5cb
import static org.junit.Assert.assertThat;
import java.util.Arrays; import java.util.Arrays;
public class AssessmentPartTwo { public class AssessmentPartTwo
{
public static void main(String[] args)
{
System.out.println(passwordValidator("lo98passwdiI"));
}
public int scrabbleScore(String aWord) public int scrabbleScore(String aWord)
{ {
...@@ -20,21 +29,25 @@ public class AssessmentPartTwo { ...@@ -20,21 +29,25 @@ public class AssessmentPartTwo {
aWord = aWord.toLowerCase(); //converts string to lowercase aWord = aWord.toLowerCase(); //converts string to lowercase
char chaWordArr[] = aWord.toCharArray(); //places string into array as ASCII chars char chaWordArr[] = aWord.toCharArray(); //places string into array as ASCII chars
for(int i = 0; i < chaWordArr.length; i++) for(int i = 0; i < chaWordArr.length; i++)
{ {
//Validation: if value is not lowercase letter (ASCII 97 - 122) then skip this iteration
if((chaWordArr[i] < 97) || (chaWordArr[i] > 122)) if((chaWordArr[i] < 97) || (chaWordArr[i] > 122))
{ {
continue; //Validation: if value is not lowercase letter (ASCII 97 - 122) then skip this iteration continue;
} }
intWordScore = intWordScore + intScoreArr[(chaWordArr[i]-97)]; //Char's ASCII code - 97 = Char's position in intScoreArr intWordScore = intWordScore + intScoreArr[(chaWordArr[i]-97)]; //Char's ASCII code - 97 = Char's position in intScoreArr
} }
return intWordScore; return intWordScore;
} }
public boolean passwordValidator(String password) public static Boolean passwordValidator(String password) //REMEMBER TO TAKE OUT STATIC
{ {
// 04 - Password Validator // 04 - Password Validator
// Complete this method to validate that the String password // Complete this method to validate that the String password
...@@ -45,71 +58,110 @@ public class AssessmentPartTwo { ...@@ -45,71 +58,110 @@ public class AssessmentPartTwo {
// - 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 booPasswordUpper = false; boolean booUpper = false; //Uppercase present
boolean booPasswordLower = false; boolean booLower = false; //Lowercase present
boolean booPasswordNum = false; boolean booNum = false; //Number present
boolean booPasswordIsPassword = false;
char passwordArr[] = password.toCharArray(); char passwordArr[] = password.toCharArray();
//true == correct, false == incorrect String[] strMustNotContain = new String[] {"password", "passwd"};
//Password length between 8 and 16 characters //Password length between 8 and 16 characters
if(!((password.length() >= 8) && (password.length() <= 16))) if((password.length() < 8) || (password.length() > 16))
{ {
System.out.println(password.length());
return false; return false;
} }
//Password does not contain. . .
for(int i = 0; i < strMustNotContain.length; i++)
//selects banned words from array
{
if((password.toLowerCase().contains(strMustNotContain[i])))
{
return false;
}
}
//Password only alphanumeric or !�$%, contains both upper, lower, and numerics //Password only alphanumeric or !�$%, contains both upper, lower, and numerics
for(int i = 0; i > passwordArr.length; i++) for(int i = 0; i < passwordArr.length; i++)
{ {
System.out.println(passwordArr[i]);
if((passwordArr[i] >= 48) && (passwordArr[i] <= 57)) if((passwordArr[i] >= 48) && (passwordArr[i] <= 57))
{ { //48 to 57 = 0 to 9
booPasswordNum = true; booNum = true;
} }
else if((passwordArr[i] >= 65) && (passwordArr[i] <= 90)) //65 to 90 = A to B else if((passwordArr[i] >= 65) && (passwordArr[i] <= 90))
{ { //65 to 90 = A to B
booPasswordUpper = true; booUpper = true;
} }
else if((passwordArr[i] >= 97) && (passwordArr[i] <= 122)) // 97 to 122 = a to b else if((passwordArr[i] >= 97) && (passwordArr[i] <= 122))
{ { // 97 to 122 = a to b
booPasswordLower = true; booLower = true;
} }
else if (passwordArr[i] == 33 || passwordArr[i] == 63 || passwordArr[i] == 36 else if (passwordArr[i] == 33 || passwordArr[i] == 63 || passwordArr[i] == 36
|| passwordArr[i] == 37) //33 = !, 63 = ?, 36 = $, 37 = % || passwordArr[i] == 37)
{ { //33 = !, 63 = ?, 36 = $, 37 = %
} }
else //only runs when invalid character is identified else
{ { //if Char is not valid
return false; return false;
} }
} }
if((booNum == false) || (booUpper == false) || (booLower == false))
{ //if password does not contain Num, Upper, or Lower Chars
return false;
}
//FUCK SAKE. LOOK UP REGEXES.
/* Legacy code that I want to keep if I ever look back on this
// //Password does not contain Password or Passwd
if((booPasswordUpper == true) && (booPasswordLower == true) && (booPasswordNum == true)) for(int i1 = 0; i1 < strMustNotContain.length; i1++)
//selects banned words from array
{ {
return true;
} System.out.println("Is " + strMustNotContain[i1].toString() + " in " + password + "?");
else
for(int i2 = 0; i2 < (passwordArr.length - strMustNotContain[i1].length() + 1); i2++)
{ {
return false; //cycles through password
char[] comparisonArr = new char[strMustNotContain[i1].length()];
for(int i3 = 0; i3 < strMustNotContain[i1].length(); i3++)
//populates a sample from password which is the same length as banned word
{
comparisonArr[i3] = Character.toLowerCase(passwordArr[i3 + i2]);
//i2 represents the offset of the sample from password, i3 the position within the sample
} }
String strComparison = new String(comparisonArr);
//from array to string
System.out.println("Banned word: " + strMustNotContain[i1] + " sample: " + strComparison);
//FUCK SAKE. LOOK UP REGEXES. System.out.println(strComparison.equals(strMustNotContain[i1]));
//str1.toLowerCase().contains(str2.toLowerCase()) if it contains Password.
//for(int i = 0; i < passwordArr.length; i++) if(strComparison.equals(strMustNotContain[i1]))
//comparison between sample from password to banned word
{
System.out.println("MATCH");
return false;
}
else
{
System.out.println("not a match");
}
} }
}
*/
return true;
}
} }
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