// Function: get_sanitized_number // Description: Returns sanitized version of card number. // Uses Functions: get_corrected_number // Parameters: Number to be sanitized. // Pre-Conditions: Valid card number. // Post-Conditions: Number retains first 5 digits then is all zeros, except for the check digit at the end that will pass the Luhn Algorithm. // Return: Sanitized number. public static ulong get_sanitized_number(ulong num) { ulong returnNum = 0; int checkDigit = CardFunctions.get_last_digit(num); int[] digits = num.ToString().Select(Convert.ToInt32).ToArray(); // Convert num into array of digits. for (int i = 0; i < 5; i++) // Retain first 5 digits of the number, and zero the rest. { digits[i] -= 48; returnNum += Convert.ToUInt64(digits[i]) * Convert.ToUInt64(Math.Pow(10, digits.Length - i - 1)); } return(get_corrected_number(returnNum)); // Return number with the correct check digit at the end. }
// Function: get_response_string // Description: Returns response to inputted card number. // Uses Functions: // Parameters: Card number entry. // Pre-Conditions: String // Post-Conditions: Appropriate string response // Return: Response string. public static string get_response_string(string input) { ulong cardNumber; bool isNumeric = ulong.TryParse(input, out cardNumber); // Check to see if string can be converted to a number. if (!isNumeric) // If not a valid number. { return("The number you entered is not valid."); } else if (!CardFunctions.is_correct_length(cardNumber)) // If number is an incorrect length. { return("The number you entered is not a valid length."); } else if (CardFunctions.luhn_check(cardNumber)) // If number does not pass luhn's algorithm. { return("The number you entered does not pass the luhn algorithm. \nMaybe you meant to type " + CardFunctions.get_corrected_number(cardNumber).ToString() + "?"); } else { return("The number you entered is a valid card number!"); } }