Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO 'imports the input and output features
- Imports System.Reflection
- Imports System.Xml.Schema
- Public Class CustomerData 'the customer data form
- Dim lineCount As Integer 'The full amount of records in the file
- Dim rowIndex As Integer 'The currently selected row of the datagrid
- Private Sub bubbleSort(ByVal index As Integer)
- Dim tempArray(8) As String 'temporay array for swaps
- Dim record1(8) As String 'records being compared
- Dim record2(8) As String
- For passes = 0 To lineCount - 1
- For counter = 0 To lineCount - 2 'to last but one row in data grid
- record1(0) = dgv_customerData.Rows(counter).Cells(0).Value 'set array values to records
- record1(1) = dgv_customerData.Rows(counter).Cells(1).Value
- record1(2) = dgv_customerData.Rows(counter).Cells(2).Value
- record1(3) = dgv_customerData.Rows(counter).Cells(3).Value
- record1(4) = dgv_customerData.Rows(counter).Cells(4).Value
- record1(5) = dgv_customerData.Rows(counter).Cells(5).Value
- record1(6) = dgv_customerData.Rows(counter).Cells(6).Value
- record1(7) = dgv_customerData.Rows(counter).Cells(7).Value
- record2(0) = dgv_customerData.Rows(counter + 1).Cells(0).Value
- record2(1) = dgv_customerData.Rows(counter + 1).Cells(1).Value
- record2(2) = dgv_customerData.Rows(counter + 1).Cells(2).Value
- record2(3) = dgv_customerData.Rows(counter + 1).Cells(3).Value
- record2(4) = dgv_customerData.Rows(counter + 1).Cells(4).Value
- record2(5) = dgv_customerData.Rows(counter + 1).Cells(5).Value
- record2(6) = dgv_customerData.Rows(counter + 1).Cells(6).Value
- record2(7) = dgv_customerData.Rows(counter + 1).Cells(7).Value
- If record1(index) > record2(index) Then 'swap (comparison based on the index of value)
- tempArray = record1
- record1 = record2
- record2 = tempArray
- End If
- dgv_customerData.Rows(counter).SetValues(record1) 'updates datagrid
- dgv_customerData.Rows(counter + 1).SetValues(record2)
- Next
- Next
- End Sub
- Private Sub fillGrid() 'fill the datagrid
- Dim encryption As New clsEncryption 'instantiate the encryption class
- lineCount = File.ReadAllLines("customerData.txt").Length 'make the linecount be the amount of records in the file
- Dim startupPath As String 'stores the location the program is running
- startupPath = Application.StartupPath
- Dim record As String 'a record from the file
- Dim fields() As String 'the fields in the record
- dgv_customerData.Rows.Clear() 'empty the datagrid
- Dim sr As New StreamReader("customerData.txt") 'Stream reader of the customer data file
- While sr.Peek() >= 0 'while there are records to read
- record = encryption.decrypt(sr.ReadLine) 'decrypt the record
- fields = record.Split(",") 'split the record into fields
- Dim index = dgv_customerData.Rows.Add 'the next row to add to
- dgv_customerData.Rows(index).SetValues(fields) 'add a record to the datagrid by putting each field in its correct place
- End While
- sr.Close() 'close the stream reader
- dgv_customerData.RowHeadersVisible = False 'Removes the symbols by the rows
- dgv_customerData.ClearSelection() 'Removes the blue selection box
- dgv_customerData.Rows(lineCount - 1).Selected = True 'select the final record in the datagrid
- rowIndex = lineCount - 1 'show the currenty corrected row
- txt_customerName.Text = "Customer Name" 'reset text boxes
- txt_surname.Text = "Surname"
- txt_DOB.Text = "Date of Birth"
- txt_email.Text = "Email"
- txt_phoneNumber.Text = "Phone Number"
- txt_postcode.Text = "Postcode"
- txt_accountID.Text = "AccountID"
- txt_search.Text = "Search by CustomerID"
- txt_CustomerID.Text = "CustomerID"
- End Sub
- Private Sub CustomerData_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'when form loads
- Me.CenterToScreen() 'center form to screen
- Dim encryption As New clsEncryption 'instanitate the encryption class
- Dim startupPath As String = Application.StartupPath 'stores the location the program is running
- pbx_logo.ImageLocation = startupPath & "\PennardLibraryLogo.png" 'set the image locations
- pbx_back.ImageLocation = startupPath & "\Arrow.png"
- If levelOfAccess = 2 Then
- Me.BackColor = Color.FromArgb(140, 227, 245) 'for librarians use blue
- End If
- If levelOfAccess = 3 Then
- Me.BackColor = Color.FromArgb(247, 220, 143) 'for admins use yellow
- End If
- If Dir(startupPath & "\customerData.txt") = "" Then 'if file doesnt exist
- Dim sw As New StreamWriter("customerData.txt", True) 'stream writer to append the customer data file
- sw.WriteLine(encryption.encrypt("1,Pam,Hopkins,10/09/1970,pam.hopkin@gmail.com,01792233277,SA3 2AQ,1")) 'records to be added
- sw.WriteLine(encryption.encrypt("2,John,Jones,11/04/1996,johnjones@cantab.net,01792679061,SA3 3LF,2"))
- sw.Close() 'close stream writer
- MsgBox("customerData file has been created") 'notify user the file was made
- End If
- If Dir(startupPath & "\tempCustomerData") = "" Then 'if the temp file doesnt exist
- Dim sw As New StreamWriter("tempCustomerData.txt", False) 'make it with a stream writer
- sw.Close() 'close the stream writer
- End If
- fillGrid() 'fill the data grid
- End Sub
- Private Sub btn_viewAll_Click(sender As Object, e As EventArgs) Handles btn_viewAll.Click
- fillGrid() 'fill the data grid
- End Sub
- Private Sub btn_add_Click(sender As Object, e As EventArgs) Handles btn_add.Click
- Dim encryption As New clsEncryption 'instantiate the encryption class
- Dim validation As New clsValidation 'instantiate the clsValidation class
- Dim validated As Boolean = True 'is validation met
- Dim record As String 'a record from the file
- Dim fields() As String 'the fields in the record
- Dim addCustomerID As Integer = 0 'the customer ID to add
- Dim addCustomerName As String 'the customer data to add
- Dim addSurname As String
- Dim addDOB As String
- Dim addEmail As String
- Dim addPhoneNumber As String
- Dim addPostcode As String
- Dim addAccountID As String
- Dim sr As New StreamReader("customerData.txt") 'Stream reader of the customer data file
- While sr.Peek() >= 0 'while there are records to read
- record = encryption.decrypt(sr.ReadLine) 'decrypt the record
- fields = record.Split(",") 'split the record into fields
- If fields(0) >= addCustomerID Then 'if the ID is higher than the ID being added
- addCustomerID = fields(0) + 1 'Makes sure the bookID being added is always 1 greater than the current largest stored
- End If
- End While
- sr.Close() 'close the stream reader
- If validation.characterCheck(txt_customerName.Text, ",") = False Or validation.characterCheck(txt_surname.Text, ",") = False Or validation.characterCheck(txt_DOB.Text, ",") = False Or validation.characterCheck(txt_email.Text, ",") = False Or validation.characterCheck(txt_phoneNumber.Text, ",") = False Or validation.characterCheck(txt_postcode.Text, ",") = False Or validation.characterCheck(txt_accountID.Text, ",") = False Then 'doesnt allow for commas to be entered
- validated = False 'doesnt allow any field to have a comma in it (this is a long line of every field's validation for this)
- MsgBox("Data cannot contain commas")
- End If
- If validation.presenceCheck(txt_customerName.Text) = True And txt_customerName.Text <> "Customer Name" Then 'presence check for all fields
- addCustomerName = txt_customerName.Text 'set the value
- Else
- validated = False 'validation not met
- End If
- If validation.presenceCheck(txt_surname.Text) = True And txt_surname.Text <> "Surname" Then 'same as with customer name
- addSurname = txt_surname.Text
- Else
- validated = False
- End If
- If validation.presenceCheck(txt_DOB.Text) = True And txt_surname.Text <> "Date of Birth" And validation.lengthCheck(txt_DOB.Text, 10, 10) Then
- addDOB = txt_DOB.Text 'same as with customer name but makes sure the DOB is 10 characters
- Else
- validated = False
- End If
- If validation.presenceCheck(txt_email.Text) = True And txt_email.Text <> "Email" Then
- addEmail = txt_email.Text
- Else
- validated = False
- End If
- If validation.presenceCheck(txt_phoneNumber.Text) = True And txt_phoneNumber.Text <> "Phone Number" And validation.lengthCheck(txt_phoneNumber.Text, 11, 11) Then
- addPhoneNumber = txt_phoneNumber.Text 'same as DOB but with 11 characters
- Else
- validated = False
- End If
- If validation.presenceCheck(txt_postcode.Text) = True And txt_postcode.Text <> "Postcode" Then
- addPostcode = txt_postcode.Text
- Else
- validated = False
- End If
- If validation.presenceCheck(txt_accountID.Text) = True And txt_accountID.Text <> "AccountID" Then
- addAccountID = txt_accountID.Text
- Else
- validated = False
- End If
- If validated = True Then 'if all validation is met
- Dim sw As New StreamWriter("customerData.txt", True) 'Stream writer of the customer data file to append
- sw.WriteLine(encryption.encrypt(addCustomerID & "," & addCustomerName & "," & addSurname & "," & addDOB & "," & addEmail & "," & addPhoneNumber & "," & addPostcode & "," & addAccountID)) 'add a encrypted record to the file
- sw.Close() 'close stream writer
- Else
- MsgBox("You need to fill out all fields and meet all validation requirements") 'notify user not all validation is met
- End If
- fillGrid() 'fill the datag grid
- End Sub
- Private Sub btn_search_Click(sender As Object, e As EventArgs) Handles btn_search.Click
- Dim encryption As New clsEncryption 'instantiate the encryption class
- Dim validation As New clsValidation 'instantiate the validation class
- lineCount = File.ReadAllLines("customerData.txt").Length 'the amount of record in the file
- Dim found As Boolean = False 'has the ID been found
- Dim record As String 'a record from the file
- Dim fields() As String 'the fields in the record
- Dim recordCount As Integer = 0 'the amount of records in the file
- Dim sr As New StreamReader("customerData.txt") 'a stream reader of the customer data file
- If validation.presenceCheck(txt_search.Text) = True And txt_search.Text <> "Search by CustomerID" Then 'if validation is true
- While sr.Peek() >= 0 'while there are records to read
- recordCount = recordCount + 1 'count the amount of records stored
- record = encryption.decrypt(sr.ReadLine) 'decrypt the record
- fields = record.Split(",") 'split tje record into fields
- If fields(0) = txt_search.Text Then 'if the ID being searched for is found in the record
- dgv_customerData.ClearSelection() 'unselect selected row
- dgv_customerData.Rows(recordCount - 1).Selected = True 'make the found record be selected
- found = True 'has been found
- ElseIf recordCount = lineCount And found = False Then 'if the record isnt present
- MsgBox("There is customer with that ID") 'notify user
- End If
- End While
- Else
- MsgBox("You must fill out all fields") 'notify user
- End If
- txt_search.Text = "Search by CustomerID" 'reset text boxes
- End Sub
- Private Sub btn_overwrite_Click(sender As Object, e As EventArgs) Handles btn_overwrite.Click
- Dim swTemp As New StreamWriter("tempCustomerData.txt", False) 'empties the tempBooks file for use in this subroutine
- swTemp.Close() 'A temp file must be used so that the stream reader reads correctly while the streamwriter writes
- Dim validation As New clsValidation 'instantiate classes
- Dim encryption As New clsEncryption
- Dim editCustomerName As String 'details to overwrite with
- Dim editSurname As String
- Dim editDOB As String
- Dim editEmail As String
- Dim editPhoneNumber As String
- Dim editPostcode As String
- Dim editAccountID As String
- Dim validated As Boolean = True 'is data validated
- Dim IDValidated As Boolean = True 'is ID validated
- Dim searchID As String 'the ID being searched for
- Dim record As String 'a record from the file
- Dim fields() As String 'the fields in the record
- Dim found As Boolean = False 'has the record been found
- If validation.presenceCheck(txt_customerName.Text) = True And txt_customerName.Text <> "Customer Name" Then 'presence check
- editCustomerName = txt_customerName.Text 'set values
- Else
- validated = False 'doesnt meet validations
- End If
- If validation.presenceCheck(txt_surname.Text) = True And txt_surname.Text <> "Surname" Then
- editSurname = txt_surname.Text 'same as customer name
- Else
- validated = False
- End If
- If validation.presenceCheck(txt_DOB.Text) = True And txt_surname.Text <> "Date of Birth" And validation.lengthCheck(txt_DOB.Text, 10, 10) Then
- editDOB = txt_DOB.Text 'same as customer name but also checks it is 10 characters
- Else
- validated = False
- End If
- If validation.presenceCheck(txt_email.Text) = True And txt_email.Text <> "Email" Then
- editEmail = txt_email.Text
- Else
- validated = False
- End If
- If validation.presenceCheck(txt_phoneNumber.Text) = True And txt_phoneNumber.Text <> "Phone Number" And validation.lengthCheck(txt_phoneNumber.Text, 11, 11) Then
- editPhoneNumber = txt_phoneNumber.Text 'same as DOB but with 11 characters
- Else
- validated = False
- End If
- If validation.presenceCheck(txt_postcode.Text) = True And txt_postcode.Text <> "Postcode" Then
- editPostcode = txt_postcode.Text
- Else
- validated = False
- End If
- If validation.presenceCheck(txt_accountID.Text) = True And txt_accountID.Text <> "AccountID" Then
- editAccountID = txt_accountID.Text
- Else
- validated = False
- End If
- If validation.presenceCheck(txt_CustomerID.Text) = True And txt_CustomerID.Text <> "CustomerID" Then 'Make sure and ID is entered
- searchID = txt_CustomerID.Text
- Else
- IDValidated = False
- MsgBox("Must enter an ID to replace the record of") 'notify user
- End If
- If IDValidated = True And validated = True Then 'If all validation is true
- Dim sr As New StreamReader("customerData.txt") 'stream reader of the customer data file
- Dim swTemp2 As New StreamWriter("tempCustomerData.txt", True) 'stream writer of the temp file that appends
- While sr.Peek() >= 0 'if there are records to read
- record = encryption.decrypt(sr.ReadLine) 'read line unencrypted
- fields = record.Split(",") 'split into fields
- If fields(0) <> searchID Then 'if the ID is not the ID being seached for
- swTemp2.WriteLine(encryption.encrypt(record)) 'Write old line into temp file
- Else
- swTemp2.WriteLine(encryption.encrypt(searchID & "," & editCustomerName & "," & editSurname & "," & editDOB & "," & editEmail & "," & editEmail & "," & editPhoneNumber & "," & editPostcode & "," & editAccountID)) 'adds new edited data to the temp file
- found = True 'has been found
- End If
- End While
- sr.Close() 'close stream reader
- swTemp2.Close() 'close stream writer
- Else
- MsgBox("Must meet all validation requirements") 'notify user
- End If
- If found = True Then 'if it has been found
- Dim srTemp As New StreamReader("tempCustomerData.txt") 'stream reader of the temp file
- Dim sw As New StreamWriter("customerData.txt", False) 'will overwrite previous file
- sw.Close() 'close stream writer
- Dim sw1 As New StreamWriter("customerData.txt", True) 'stream writer to append file
- While srTemp.Peek() >= 0
- record = srTemp.ReadLine 'each record from the temp file
- sw1.WriteLine(record) 'write the record from the temp file into the normal file (overwriting)
- End While
- srTemp.Close() 'close stream readers and writers
- sw1.Close()
- Else
- MsgBox("No record found with that ID") 'notify user
- End If
- fillGrid()
- End Sub
- Private Sub btn_delete_Click(sender As Object, e As EventArgs) Handles btn_delete.Click
- Dim swTemp As New StreamWriter("tempCustomerData.txt", False) 'empties the tempBooks file for use in this subroutine
- swTemp.Close() 'A temp file must be used so that the stream reader reads correctly while the streamwriter writes
- Dim validation As New clsValidation 'instantiate classes
- Dim encryption As New clsEncryption
- Dim validated As Boolean = True 'is data validated
- Dim IDValidated As Boolean = True 'is ID validated
- Dim searchID As String 'the ID to search for
- Dim record As String 'a record from the file
- Dim fields() As String 'the fields in the record
- Dim found As Boolean = False
- If validation.presenceCheck(txt_CustomerID.Text) = True And txt_CustomerID.Text <> "CustomerID" Then 'Make sure and ID is entered
- searchID = txt_CustomerID.Text 'set value
- Else
- IDValidated = False 'doesnt meet validation
- MsgBox("Must enter an ID to replace the record of") 'notify user
- End If
- If IDValidated = True And validated = True Then 'If all validation is true
- found = True 'has been found
- Dim sr As New StreamReader("customerData.txt") 'stream reader of the customer data file
- Dim swTemp2 As New StreamWriter("tempCustomerData.txt", True) 'stream reader to append temp file
- While sr.Peek() >= 0 'while there are records to read
- record = encryption.decrypt(sr.ReadLine) 'read line unencrypted
- fields = record.Split(",") 'split record
- If fields(0) <> searchID Then 'if record doesnt have the ID
- swTemp2.WriteLine(encryption.encrypt(record)) 'Write old line into temp file
- End If
- End While
- sr.Close() 'close stream readers and writers
- swTemp2.Close()
- Else
- MsgBox("Must meet all validation requirements") 'notify user
- End If
- If found = True Then
- Dim srTemp As New StreamReader("tempCustomerData.txt") 'stream reader of temp file
- Dim sw As New StreamWriter("customerData.txt", False) 'will overwrite previous file
- sw.Close() 'close stream writer
- Dim sw1 As New StreamWriter("customerData.txt", True) 'stream writer to append file
- While srTemp.Peek() >= 0
- record = srTemp.ReadLine 'each record from the temp file
- sw1.WriteLine(record) 'write the record from the temp file into the normal file (overwriting)
- End While
- srTemp.Close() 'close stream readers and writers
- sw1.Close()
- Else
- MsgBox("No record found with that ID") 'notify user
- End If
- fillGrid()
- End Sub
- Private Sub btn_first_Click(sender As Object, e As EventArgs) Handles btn_first.Click
- dgv_customerData.ClearSelection()
- dgv_customerData.Rows(0).Selected = True 'gp back to the first record
- rowIndex = 0
- End Sub
- Private Sub btn_previous_Click(sender As Object, e As EventArgs) Handles btn_previous.Click
- dgv_customerData.ClearSelection()
- If rowIndex = 0 Then
- MsgBox("You are already looking at the first record")
- Else
- rowIndex = rowIndex - 1
- dgv_customerData.Rows(rowIndex).Selected = True 'go one record back unless you are at the fist one already
- End If
- End Sub
- Private Sub btn_next_Click(sender As Object, e As EventArgs) Handles btn_next.Click
- dgv_customerData.ClearSelection()
- If rowIndex = lineCount - 1 Then
- MsgBox("You are already looking at the last record")
- Else
- rowIndex = rowIndex + 1
- dgv_customerData.Rows(rowIndex).Selected = True 'go on record forwards unless you are at the last one already
- End If
- End Sub
- Private Sub btn_last_Click(sender As Object, e As EventArgs) Handles btn_last.Click
- dgv_customerData.ClearSelection()
- dgv_customerData.Rows(lineCount - 1).Selected = True 'go to the last record stored
- rowIndex = lineCount - 1
- End Sub
- Private Sub pbx_back_Click(sender As Object, e As EventArgs) Handles pbx_back.Click
- Dim MainMenu As New MainMenu 'new instance of main menu form
- MainMenu.Show() 'show new form
- Me.Close() 'close current form
- End Sub
- Private Sub pbx_back_MouseHover(sender As Object, e As EventArgs) Handles pbx_back.MouseHover
- Dim startupPath As String 'stores location the program is running
- startupPath = Application.StartupPath
- pbx_back.ImageLocation = startupPath & "\ArrowRollover.png" 'rollover
- End Sub
- Private Sub pbx_back_MouseLeave(sender As Object, e As EventArgs) Handles pbx_back.MouseLeave
- Dim startupPath As String 'stores location the program is running
- startupPath = Application.StartupPath
- pbx_back.ImageLocation = startupPath & "\Arrow.png" 'back to normal
- End Sub
- Private Sub txt_customerName_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_customerName.MouseClick
- If txt_customerName.Text = "Customer Name" Then 'if text is starting text
- txt_customerName.Text = "" 'clear text box
- End If
- End Sub
- Private Sub txt_surname_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_surname.MouseClick
- If txt_surname.Text = "Surname" Then 'same as customer name text box
- txt_surname.Text = ""
- End If
- End Sub
- Private Sub txt_DOB_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_DOB.MouseClick
- If txt_DOB.Text = "Date of Birth" Then
- txt_DOB.Text = ""
- End If
- End Sub
- Private Sub txt_email_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_email.MouseClick
- If txt_email.Text = "Email" Then
- txt_email.Text = ""
- End If
- End Sub
- Private Sub txt_phoneNumber_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_phoneNumber.MouseClick
- If txt_phoneNumber.Text = "Phone Number" Then
- txt_phoneNumber.Text = ""
- End If
- End Sub
- Private Sub txt_postcode_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_postcode.MouseClick
- If txt_postcode.Text = "Postcode" Then
- txt_postcode.Text = ""
- End If
- End Sub
- Private Sub txt_accountID_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_accountID.MouseClick
- If txt_accountID.Text = "AccountID" Then
- txt_accountID.Text = ""
- End If
- End Sub
- Private Sub txt_CustomerID_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_CustomerID.MouseClick
- If txt_CustomerID.Text = "CustomerID" Then
- txt_CustomerID.Text = ""
- End If
- End Sub
- Private Sub txt_search_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_search.MouseClick
- If txt_search.Text = "Search by CustomerID" Then
- txt_search.Text = ""
- End If
- End Sub
- Private Sub rbt_sortCustomerID_CheckedChanged(sender As Object, e As EventArgs) Handles rbt_sortCustomerID.CheckedChanged
- bubbleSort(0) 'sort by ID
- End Sub
- Private Sub rbt_sortCustomerName_CheckedChanged(sender As Object, e As EventArgs) Handles rbt_sortCustomerName.CheckedChanged
- bubbleSort(1) 'sort by customer name
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement