C# Please
Write Unit tests for a boolean method validateStudentID() that accepts a string. It validates a DMACC 900 number. Then write the code to make the tests pass.
Test 1. A valid 900 as input (method returns true)
Test 2. An invalid 900 number -- does not start with 9 (method returns false)
Test 3. An invalid 900 number -- second character is not zero (method returns false)
Test 4. An invalid 900 number -- contains digits (method returns false)
Write your method so all tests will pass. You should not need to change your Unit Tests unless you made an error in them. Likely, the change will be adding logic to your method.
Question : -
Solution :-
public bool validateStudentID(string DMACC)
{
int number;
int.TryParse(DMACC, out number); //convert string to int
int[] nums = new int[900]; //array to store digits of number .
int i = 0; // initialization of i variable which is used for
iteration.
// while loop to fill array and get reverse digit.
while (number != 0)
{
nums[i] = number % 10;
number = number / 10;
i++;
}
// to place digit in proper place
Array.Reverse(nums);
//Test 2. An invalid 900 number -- does not start with 9 (method
returns false)
if (nums[0] == 9)
{
return false;
}
// Test 3.An invalid 900 number-- second character is not zero
(method returns false)
if (nums[1] == 0)
{
return false;
}
// Test 4.An invalid 900 number-- contains digits(method returns
false)
if (isNumber(DMACC))
{
return false;
}
return true; //otherwise .. A valid 900 as input (method returns
true)
}
//method to check String contain any digit / number or not and this
method is used to solve Test 4
public static bool isNumber(string str)
{
bool status = str.Where(y => Char.IsDigit(y)).Any();
return status;
}
Snapshots: -
code:-
output : -
there is specific output on console because of I'm not using that method .but it clearly indicate that code doesn't contain any error. i.e. 0 error ,0 warnings.
thank you ..?!
Get Answers For Free
Most questions answered within 1 hours.