1 Replies - 2389 Views - Last Post: 30 August 2010 - 07:10 AM

#1 browngod2002   User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 27
  • Joined: 16-September 06

LUHN CHECK PROGRAM

Posted 23 September 2006 - 05:29 AM

Description: click the button to see.this is the luhn check program used by banks and credit card companies to check if a caredit card number is valid.
'Matthew Schofield
'Date: 1/10/06
'CPT 212-02
'Luhn Check

'This program will take in credit card numbers
'from a text file.
'Then determine if the number is valid.

Option Explicit On 
Option Strict On
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnDisplay As System.Windows.Forms.Button
    Friend WithEvents lblMatthew As System.Windows.Forms.Label
    Friend WithEvents lstResults As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnDisplay = New System.Windows.Forms.Button
        Me.lstResults = New System.Windows.Forms.ListBox
        Me.lblMatthew = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'btnDisplay
        '
        Me.btnDisplay.Location = New System.Drawing.Point(20, 16)
        Me.btnDisplay.Name = "btnDisplay"
        Me.btnDisplay.Size = New System.Drawing.Size(296, 23)
        Me.btnDisplay.TabIndex = 0
        Me.btnDisplay.Text = "&Display"
        '
        'lstResults
        '
        Me.lstResults.Font = New System.Drawing.Font("Courier New", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lstResults.ItemHeight = 14
        Me.lstResults.Location = New System.Drawing.Point(21, 56)
        Me.lstResults.Name = "lstResults"
        Me.lstResults.Size = New System.Drawing.Size(295, 214)
        Me.lstResults.TabIndex = 1
        '
        'lblMatthew
        '
        Me.lblMatthew.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblMatthew.ForeColor = System.Drawing.Color.Blue
        Me.lblMatthew.Location = New System.Drawing.Point(96, 280)
        Me.lblMatthew.Name = "lblMatthew"
        Me.lblMatthew.Size = New System.Drawing.Size(144, 16)
        Me.lblMatthew.TabIndex = 2
        Me.lblMatthew.Text = "Matthew Schofield"
        Me.lblMatthew.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(336, 310)
        Me.Controls.Add(Me.lblMatthew)
        Me.Controls.Add(Me.lstResults)
        Me.Controls.Add(Me.btnDisplay)
        Me.Name = "Form1"
        Me.Text = "Luhn Check"
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region " btnDisplay "
    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        'envokes the functions to determine if 
        'the credit card number is valid.
        'then displays it in a llistbox.

        'declare the variables
        Dim sr As IO.StreamReader
        Dim fmtStr As String = "{0,-18}  {1,-5}  {2,10}"
        Dim ccnum As String
        Dim sumStr As String

        'clear the listbox
        lstResults.Items.Clear()

        'display the header row
        lstResults.Items.Add(String.Format(fmtStr, "Credit Card Number", _
                                   "Valid", "Sum"))
        lstResults.Items.Add(String.Format(fmtStr, "------------------", _
                            "-----", "----------"))

        'open text file
        sr = IO.File.OpenText("CCNumbers.txt")

        'loop through the file
        Do Until sr.Peek = -1
            'read the next credit card number
            ccnum = sr.ReadLine

            'determine if the credit card number is really a number,
            'and if not set sumStr to "Not Numeric"
            If LuhnSum(ccnum) <> -1 Then sumStr = CStr(LuhnSum(ccnum)) _
                       Else sumStr = "Not Digits"

            'display the credit card number,its validity, 
            'and the Luhn sum
            lstResults.Items.Add(String.Format(fmtStr, ccnum, _
                   CStr(LuhnCheck(ccnum)), sumStr))

        Loop
        'close the file
        sr.Close()
    End Sub
#End Region

#Region " LuhnCheck Function "
    Function LuhnCheck(ByVal CCNumber As String) As Boolean
        'checks to see if the credit card number is valid

        If Luhnsum(CCNumber) Mod 10 <> 0 Then
            Return False
        End If
        Return True

    End Function
#End Region

#Region " Luhnsum Function "
    Function Luhnsum(ByVal CCNumber As String) As Integer
        'gets the credit card number,then checks to see if the number
        'is a number. Then adds the number and returns the sum.

        'declare the variables
        Dim count, num, digit, sum As Integer

        'loop through the numbers
        For count = 1 To CCNumber.Length
            If Not IsNumeric(CCNumber.Substring(count - 1, 1)) Then
                Return -1
            End If

            'get the next number
            num = CInt(CCNumber.Substring(count - 1, 1))
            If Not (count Mod 2 = 0) Then
                num *= 2
            End If
            For digit = 1 To CStr(num).Length
                sum += CInt(CStr(num).Substring(digit - 1, 1))
            Next
        Next
        Return sum
    End Function
#End Region

End Class



Is This A Good Question/Topic? 0
  • +

Replies To: LUHN CHECK PROGRAM

#2 YanivC   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 30-August 10

Re: LUHN CHECK PROGRAM

Posted 30 August 2010 - 07:10 AM

Matt Your LUHN check is off. First of all the LUHN sum starts from the right not the left as you have it. Also, your not factoring our numbers > 9 (i.e. if the mod 2 number is a 7 then 2 * 7 = 14 = 1+4 = 5). This code wont work this way. Try this. For count = CCNumber.Length To 1 Step -1 If Not IsNumeric(CCNumber.Substring(count - 1, 1)) Then Return -1 End If 'get the next number num = CInt(CCNumber.Substring(count - 1, 1)) If (count Mod 2 = 0) Then num *= 2 If num > 9 Then num = CInt(Left(num.ToString, 1)) + CInt(Right(num.ToString, 1)) End If End If For digit = 1 To CStr(num).Length sum += CInt(CStr(num).Substring(digit - 1, 1)) Next Next Submitted by Yaniv C.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1