java,c ++或c#中的TPIN号验证

Cha*_*kay 5 c# c++ java validation

我在大学项目中对TPIN验证的要求很低,要求是,我们不应该允许用户在下面的场景中设置他的TPIN.

  1. TPIN不应该按顺序排列.(升序或降序除外:123456,456789,987654或654321)
  2. TPIN不应该从零开始(例如:0127865,098764)
  3. TPIN不应该是重复数字(例如:888888,222222等)

对于第三个我的想法是将数字除以11并检查提醒..

任何想法plz ..

public class Validate
{
   public static void main(String[] args)
   {
        int iTpin = Integer.parseInt(args[0]); // Converted string to int
        System.out.println("The entered TPIN is:"+iTpin);
        boolean flag = validate(iTpin);
        if (flag== true)
            System.out.println("The entered TPIN is valid");
        else 
            System.out.println("The entered TPIN is Invalid");
    }

    public static boolean validate(int passedTpin)
    {
        if (passedTpin == 0)
            return false;
        else if ( passedTpin%11 == 0)
            return false;
        else
            return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后创建了数字序列的代码.它可能对其他人有用

private static boolean containsRepetitiveDigits(String tpin) {
    char firstChar = tpin.charAt(0);
    for (int i = 1; i < tpin.length(); i++) {
        char nextChar = tpin.charAt(i);
        if ((Character.valueOf(nextChar)).compareTo(Character
                .valueOf(firstChar)) != 0) {
            return false;
        }
    }
    System.out.println("Error:TPIN contains repetitive digits");
    return true;
}
Run Code Online (Sandbox Code Playgroud)

Gro*_*roo 6

首先,Int32用于存储数字意味着它不应超过2,147,483,647.除此之外,一旦转换为数字,您将无法检查前导零,因为一旦得到数字,前导零显然会消失.

这意味着您应该string在验证期间保持输入.这实际上使您的工作更容易,因为您可以索引单个字符,而无需使用算术运算.

由于您正在使用字符串,因此还应检查输入字符串是否包含无效(非数字)字符,然后再执行其他操作:

bool ContainsInvalidCharacters(string input)
{
    // check if there is a non-digit character
    foreach (char c in input)
        if (!char.IsDigit(c))
            return true;

    return false;
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以继续添加单个规则.例如,要检查字符是否重复,您将执行以下操作:

bool ContainsRepetitiveDigits(string input)
{
    if (input.Length == 0)
        return false;

    // get the first character
    char firstChar = input[0];

    // check if there is a different character
    foreach (char c in input)
        if (c != firstChar)
            return false;

    // if not, it means it's repetitive
    return true;
}

bool StartsWithZero(string input)
{
    if (input.Length == 0)
        return false;

    return (input[0] == '0');
}
Run Code Online (Sandbox Code Playgroud)

要检测序列,最直接的方法是获取前两个字符的差异,然后检查它是否在整个字符串中发生变化:

bool IsSequence(string input)
{
    // we need at least two characters
    // for a sequence
    if (input.Length < 2)
        return false;

    // get the "delta" between first two
    // characters
    int difference = input[1] - input[0];

    // allowed differences are:
    //   -1: descending sequence
    //    0: repetitive digits
    //    1: ascending sequence
    if (difference < -1 || difference > 1)
        return false;

    // check if all characters are equally
    // distributed
    for (int i = 2; i < input.Length; i++)
        if (input[i] - input[i - 1] != difference)
            return false;

    // this is a sequence
    return true;
}
Run Code Online (Sandbox Code Playgroud)

一旦定义了所有规则,就可以创建一个单独的方法来逐个测试它们:

bool Validate(string input)
{
    // list of all predicates to check
    IEnumerable<Predicate<string>> rules = new Predicate<string>[]
    {
        ContainsInvalidCharacters,
        ContainsRepetitiveDigits,
        StartsWithZero,
        IsSequence
    };

    // check if any rule matches
    foreach (Predicate<string> check in rules)
        if (check(input))
            return false;

    // if no match, it means input is valid
    return true;
}
Run Code Online (Sandbox Code Playgroud)

注意,也IsSequence检测重复的数字模式(当字符差异为零时).如果要明确禁止此操作,请更改检查允许差异的条件.或者,您可以ContainsRepetitiveDigits完全删除该规则.


[编辑]

由于我发现您使用的是Java而不是C#,因此我将尝试提供更好的示例.

免责声明:我通常不用Java编程,但据我所知,Java并不像C#那样支持代理.所以我将尝试提供一个Java示例(希望它能够工作),它更好地表达了我的"复合验证"意图.

(可疑的Java代码如下)

首先,定义所有验证规则将实现的接口:

// (java code)
/**
 * Defines a validation rule.
 */
public interface IValidationRule
{
    /**
     * Returns a description of this
     * validation rule.
     */
    String getDescription();

    /**
     * Returns true if this rule
     * is matched.
     */
    boolean matches(String tpin);
}
Run Code Online (Sandbox Code Playgroud)

接下来,在单独的类中定义每个规则,实现两个getDescriptionmatches方法:

// (java code)
public class RepetitiveDigitsRule implements IValidationRule
{
    public String getDescription()
    {
        return "TPIN contains repetitive digits";
    }

    public boolean matches(String tpin)
    {
        char firstChar = tpin.charAt(0);
        for (int i = 1; i < tpin.length(); i++)
            if (tpin.charAt(i) != firstChar)
                return false;
        return true;
    }
}

public class StartsWithZeroRule implements IValidationRule
{
    public String getDescription()
    {
        return "TPIN starts with zero";
    }

    public boolean matches(String tpin)
    {
        if (tpin.length() < 1)
            return false;

        return tpin.charAt(0) == '0';
    }
}
Run Code Online (Sandbox Code Playgroud)

你可以看到,matches方法并没有打印任何东西来安慰.它只是true在规则匹配时返回,并留给其调用者决定是否打印其描述(到控制台,消息框,网页,等等).

最后,您可以实例化所有已知规则(实现IValidationRule)并逐个检查它们:

// (java code)
public class Validator
{
    // instantiate all known rules
    IValidationRule[] rules = new IValidationRule[] {
        new RepetitiveDigitsRule(),
        new StartsWithZeroRule()
    };

    // validate tpin using all known rules
    public boolean validate(String tpin)
    {
        System.out.print("Validating TPIN " + tpin + "... ");

        // for all known rules
        for (int i = 0; i < rules.length; i++)
        {
            IValidationRule rule = rules[i];

            // if rule is matched?
            if (rule.matches(tpin))
            {
                // print rule description
                System.out.println("Error: " + rule.getDescription());
                return false;
            }
        }
        System.out.println("Success.");
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

我建议尝试遵循这种模式.最终,代码将更容易重用和维护.