如何检查是否使用功能格式化了票号?

时间:2017-02-06 20:37:30

标签: vb.net

我希望我的功能遵循一些惯例来检查我的票号是否需要格式化。

如果不符合惯例,那么我想对票号进行一些更改。

票号 19K3072216 需要格式化为此 19-K3-07-002216 ,因为它不符合以下条件。 我的功能应该做到以下几点。

  1. 检查前2位数字的值是否为0 - 9(数字)
  2. 检查第3位数字的值是否为A到Z
  3. 检查第4位数字的值是否为0 - 9(数字)
  4. 检查第5和第6位数字是否有日期值(例如2位数年份--17,90,15等)
  5. 检查接下来的6位数字,即第7位到第12位是否为数字。
  6. 因为票号 19K3072216 不符合上述条件,我希望我的功能将其格式化为 19-K3-07-002216

    字符串 strTicketNumber 应返回格式化的票号 19-K3-07-002216

    我的vb.net功能

    Public Class Ticket_Code
    
        Public Shared Sub main()
            Dim strTicketNumber As String = FixTicketNumber("19K3072216")
    
        End Sub
    
        Public Shared Function FixCaseNumber(ByVal astrCaseNumber As String) As String
            Dim strCaseNumber As String = Replace(astrCaseNumber, "-", "")
    
            'Determine if ticket number is formatted
             How do I do this?
    
    
            'If ticket number is formatted add 2 zeros
             'How do I do this?
    
            'Else return unchanged
             'If ticket number is already formatted, just returned the number (original number)
    
            Return strCaseNumber
    
        End Function
    
    
    End Class
    

1 个答案:

答案 0 :(得分:0)

这将取决于您的输入以及它与示例的不同之处。 例如,无效输入始终采用相同的格式19K3072216,或者是否有可能是所有数字,所有字母,少于/超过10个字符等等。所有这些规则都需要考虑和处理为必要的。

如果输入来自用户,则永远不要相信它,并始终假设它尽可能远离有效。如果应用程序可以处理该情况,它可以处理其他所有事情

这样的事情应该让你开始:

Public Sub Main()
        Dim strTicketNumber As String = FixTicketNumber("19-K3-07-002216") ' or 19K3072216
        Console.WriteLine(strTicketNumber)
        Console.ReadKey()
    End Sub


    Private Function FixTicketNumber(p1 As String) As String
        Dim fixed As String = ''
        Dim valid As Boolean = checkTicketNumber(p1)

        If valid Then
            Return p1 ' Ticket number is valid, no transformation needed
        Else
            'Assume invalid input will always be 10 characters (e.g. 19K3072216)
            'Split the input and Step through each rule one at a time
            'returning the necessary result/format string as you go

            '#1 Check if the 1st 2 digits has a value 0 - 9 (numeric)
            Dim ruleOne As String = p1.Substring(0, 2)
            'perform isNumeric, concatenate to fixed if everything is ok
            'fixed += ruleOne+"-"

            '#2 Check if the 3rd digit has a value of A to Z
            Dim ruleTwo As String = p1.Substring(3, 1)
            'check if its a letter, concatenate to fixed if everything is ok

            '... same for all the rules

        End If

    End Function

    Private Function checkTicketNumber(p1 As String) As Boolean
        'See if the input matches the rules
        'Check if the 1st 2 digits has a value 0 - 9 (numeric)
        'Check if the 3rd digit has a value of A to Z
        'Check if the 4th digit has a value 0 - 9 (numeric)
        'Check if the 5th and 6th digits has a date value (e.g.2 digit year - 17, 90, 15 etc)
        'Check if the next 6 digits i.e. 7th - 12th digits are numeric.
        Dim pattern As String = "\d{2}-[A-Z]\d-\d{2}-\d{6}"
        Dim match As Match = Regex.Match(p1, pattern)

        Return match.Success

    End Function

很难产生一个完全可行的解决方案,因为作为局外人有太多关于输入的未知数。