Commit f2838112 authored by benn.robinson's avatar benn.robinson

Update AssessmentPartTwo.java

parent b27a60bd
public class AssessmentPartTwo { public class AssessmentPartTwo {
public int scrabbleScore(String aWord) public int scrabbleScore(String aWord)
...@@ -12,11 +11,53 @@ public class AssessmentPartTwo { ...@@ -12,11 +11,53 @@ public class AssessmentPartTwo {
// https://en.wikipedia.org/wiki/Scrabble_letter_distributions // https://en.wikipedia.org/wiki/Scrabble_letter_distributions
// You will need to come up with a way of connecting each letter to its score and // 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. // a way of identifying each letter in the word.
int total = 0;
for (int length = 0; length < aWord.length(); length++) // for loop to count up through the word length
{
char aChar = aWord.charAt(length); // selects the letter at the current char placement
Character.toString(aChar); // converts to a string value
Character.toLowerCase(aChar); // converts to lowercase so that it matches
switch (String.valueOf(aChar)) { // Allows the selection of the correct string, and the ability to add a correlative value
return 0; case "a": case "e": case "i": case "o": case "n": case "r": case "t": case "l": case "s": case "u":
} total = total + 1;
break;
case "d": case "g":
total = total + 2;
break;
case "b": case "c": case "m": case "p":
total = total + 3;
break;
case "f": case "h": case "v": case "w": case "y":
total = total + 4;
break;
case "k":
total = total + 5;
break;
case "j": case "x":
total = total + 8;
break;
case "q": case "z":
total = total + 10;
break;
}
}
return total; // returns scrabble score in total
}
public Boolean passwordValidator(String password) public Boolean passwordValidator(String password)
{ {
// 04 - Password Validator // 04 - Password Validator
...@@ -24,11 +65,56 @@ public class AssessmentPartTwo { ...@@ -24,11 +65,56 @@ public class AssessmentPartTwo {
// is a valid password // is a valid password
// A password is valid if it is // A password is valid if it is
// - between 8 and 16 characters long (inclusive) // - between 8 and 16 characters long (inclusive)
// - 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'
int lengthP = password.length();
return false; boolean passwd = true;
boolean lowerCase = false; // The booleans are inputed so that the needs for the tests are made
boolean upperCase = false;
boolean number = false;
boolean lengthA = false;
boolean character = true;
boolean finalTest = false;
String test = password;
if (lengthP >= 8 && lengthP <=16)
{ // this is inputed so that it is between 8 & 16
lengthA = true;
}
for (int i = 0; i < password.length(); i ++) //for loop to cycle through each character of the given password
{
String cOne= ""; // used to see the variable when debugging
char charOne = password.charAt(i); // selects the letter at the current char placement
Character special = Character.toLowerCase(charOne); //converts character to lowercase
String dollarTest = Character.toString(charOne); //converts character to a string to test for a $
int speciall = (int)special; //converts password char to ascii code
if (!(speciall >= 97 && speciall <= 122 || speciall >= 48 && speciall <= 57
|| speciall == 33 || speciall == 36 || speciall == 37 || dollarTest.equals("$"))) // makes sure characters are only the permitted ones
{
finalTest = false;
return finalTest;
}
if (Character.isUpperCase(charOne))
{ // checks for an uppercase character
upperCase = true;
}
if (Character.isLowerCase(charOne)) // checks for a lowercase character
{
lowerCase = true;
}
if (Character.isDigit(charOne)) //checks for a digit (number)
{
number = true;
}
if (password.toLowerCase().contains("password") || password.toLowerCase().contains("passwd"))
{
passwd = false; // converts the passwd to a lowercase and then checks for the phrases "password"
} // and "passwd" to ensure all combinations of case are covered
}
if (passwd == true && upperCase == true && lowerCase == true && character == true && number == true && lengthA == true)
{
finalTest = true; // if all the loop tests are true then the final result will become true as the password passes
}
return finalTest;
} }
} }
\ No newline at end of file
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