Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO 'imports input and output functions
- Public Class BorrowingData 'borrowing 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 fillGrid()
- Dim encryption As New clsEncryption 'instantiates the encryption class
- lineCount = File.ReadAllLines("borrowingData.txt").Length 'make the linecount be the amount of records in the file (doesnt need to look in other file because it will just add more data so its linecount doesnt matter)
- Dim startupPath As String 'stores location the program is running
- startupPath = Application.StartupPath
- Dim record1 As String 'record from borrowing data file
- Dim fields1() As String 'fields from record
- Dim record2 As String 'record from individual borrowings file
- Dim fields2() As String 'fields from record
- Dim customerID As String ''the customer ID found
- Dim found As Boolean = False 'has it been found
- dgv_borrowingData.Rows.Clear() 'clear text box
- Dim sr1 As New StreamReader("borrowingData.txt") 'stream reader of borrowing data file
- While sr1.Peek() >= 0 'while there are record to read
- record1 = encryption.decrypt(sr1.ReadLine) 'decrypt record
- fields1 = record1.Split(",") 'split record into fields
- Dim index = dgv_borrowingData.Rows.Add 'the row to add to
- Dim sr2 As New StreamReader("individualBorrowings.txt") 'stream reader of individual borrowings file
- While sr2.Peek() >= 0 'check every record of the individual borrowing file
- record2 = encryption.decrypt(sr2.ReadLine) 'decrypt record
- fields2 = record2.Split(",") 'split into fields
- If fields1(0) = fields2(0) Then 'if there is a match on borrowingID
- customerID = fields2(1) 'Add the customerID
- found = True 'has been found
- Else
- If found = False Then
- customerID = "N/A" 'There is no customerID
- End If
- End If
- dgv_borrowingData.Rows(index).SetValues(fields1)
- dgv_borrowingData.Rows(index).Cells(4).Value = customerID 'add the value into the customerID cell
- End While
- sr2.Close() 'close stream reader
- End While
- sr1.Close() 'close stream reader
- dgv_borrowingData.RowHeadersVisible = False 'Removes the symbols by the rows
- dgv_borrowingData.ClearSelection() 'Removes the blue selection box
- dgv_borrowingData.Rows(lineCount - 1).Selected = True 'select the final record in the datagrid
- rowIndex = lineCount - 1 'show the currenty corrected row
- txt_bookID.Text = "BookID" 'reset text boxes
- txt_dateBorrowed.Text = "Date Borrowed"
- txt_dueDate.Text = "Due Date"
- txt_search.Text = "Search by BorrowingID"
- txt_customerID.Text = "CustomerID"
- End Sub
- Private Sub BorrowingData_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'when form loads
- Me.CenterToScreen() 'center form to screen
- Dim encryption As New clsEncryption 'instantiate encryption class
- Dim startupPath As String = Application.StartupPath 'stores location the program is running
- pbx_logo.ImageLocation = startupPath & "\PennardLibraryLogo.png" 'sets image locations
- pbx_back.ImageLocation = startupPath & "\Arrow.png"
- If levelOfAccess = 2 Then
- Me.BackColor = Color.FromArgb(140, 227, 245) 'use blue for librarians
- End If
- If levelOfAccess = 3 Then
- Me.BackColor = Color.FromArgb(247, 220, 143) 'use yellow for admins
- End If
- If Dir$(startupPath & "\borrowingData.txt") = "" Then 'if file doesnt exist
- Dim sw As New StreamWriter("borrowingData.txt", True) 'stream writer to append borrowing data file
- sw.WriteLine(encryption.encrypt("1,1,01/01/2000,09/01/2000")) 'file containg borrowing information
- sw.Close() 'close stream writer
- MsgBox("New file created") 'notify user the file was made
- End If
- If Dir(startupPath & "\individualBorrowings.txt") = "" Then 'if file doesnt exist
- Dim sw As New StreamWriter("individualBorrowings.txt", True) 'file containing borrowingID and customerID for linking entities
- sw.WriteLine(encryption.encrypt("1,1"))
- sw.Close() 'close stream writer
- MsgBox("New file created") 'notify user
- End If
- 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 classes
- Dim validation As New clsValidation
- Dim validated As Boolean = True 'is data validated
- Dim record As String 'record from file
- Dim fields() As String 'fields in record
- Dim addBookID As String 'the data to add to file
- Dim addBorrowingDate As String
- Dim addDueDate As String
- Dim addCustomerID As String
- Dim addBorrowingID As Integer = 0
- Dim sr As New StreamReader("borrowingData.txt") 'stream reader of borrowing data file
- While sr.Peek() >= 0
- record = encryption.decrypt(sr.ReadLine) 'decrypt record
- fields = record.Split(",") 'split record
- If fields(0) >= addBorrowingID Then 'if ID is higher than ID being added
- addBorrowingID = 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 stream reader
- If validation.characterCheck(txt_bookID.Text, ",") = False Or validation.characterCheck(txt_dateBorrowed.Text, ",") = False Or validation.characterCheck(txt_dueDate.Text, ",") = False Or validation.characterCheck(txt_customerID.Text, ",") = False Then 'doesnt allow for commas to be entered
- validated = False 'not all validation is met
- MsgBox("Data cannot contain commas") 'notify user
- End If
- If validation.presenceCheck(txt_bookID.Text) = True And txt_bookID.Text <> "BookID" Then 'presence check
- addBookID = txt_bookID.Text 'set value
- Else
- validated = False 'doesnt meet validation
- End If
- If validation.presenceCheck(txt_customerID.Text) = True And txt_customerID.Text <> "CustomerID" Then
- addCustomerID = txt_customerID.Text 'same as bookID
- Else
- validated = False
- End If
- If validation.presenceCheck(txt_dateBorrowed.Text) = True And txt_dateBorrowed.Text <> "Date Borrowed" And validation.lengthCheck(txt_dateBorrowed.Text, 10, 10) Then
- addBorrowingDate = txt_dateBorrowed.Text 'also has length check
- Else
- validated = False
- End If
- If validation.presenceCheck(txt_dueDate.Text) = True And txt_dueDate.Text <> "Due Date" And validation.lengthCheck(txt_dueDate.Text, 10, 10) Then
- addDueDate = txt_dueDate.Text
- Else
- validated = False
- End If
- If validated = True Then 'if validation is met
- Dim sw1 As New StreamWriter("borrowingData.txt", True) 'stream writers of the respective files
- Dim sw2 As New StreamWriter("individualBorrowings.txt", True)
- sw1.WriteLine(encryption.encrypt(addBorrowingID & "," & addBookID & "," & addBorrowingDate & "," & addDueDate)) 'writes encrypted records
- sw2.WriteLine(encryption.encrypt(addBorrowingID & "," & addCustomerID))
- sw1.Close() 'close stream writers
- sw2.Close()
- Else
- MsgBox("Must meet all validation requirements") 'notify user
- End If
- fillGrid() 'fill the data grid
- End Sub
- Private Sub btn_search_Click(sender As Object, e As EventArgs) Handles btn_search.Click
- Dim encryption As New clsEncryption 'instantiate classes
- Dim validation As New clsValidation
- lineCount = File.ReadAllLines("borrowingData.txt").Length 'the amount of records in the file
- Dim found As Boolean = False 'has it been found
- Dim record As String 'record from file
- Dim fields() As String 'fields in record
- Dim recordCount As Integer = 0 'the amount of records
- Dim sr As New StreamReader("borrowingData.txt") 'stream reader of the borrowing data file
- If validation.presenceCheck(txt_search.Text) = True And txt_search.Text <> "Search by BorrowingID" Then 'presence check
- 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 record
- fields = record.Split(",") 'split record
- If fields(0) = txt_search.Text Then 'if the ID is found
- dgv_borrowingData.ClearSelection() 'unselect row
- dgv_borrowingData.Rows(recordCount - 1).Selected = True 'make the found record be selected
- found = True 'has been found
- ElseIf recordCount = lineCount And found = False Then 'if not found
- MsgBox("There is customer with that ID") 'notify user
- End If
- End While
- Else
- MsgBox("You must fill out all fields")
- End If
- sr.Close() 'close stream reader
- txt_search.Text = "Search by BorrowingID" 'reset text box
- End Sub
- Private Sub btn_first_Click(sender As Object, e As EventArgs) Handles btn_first.Click
- dgv_borrowingData.ClearSelection()
- dgv_borrowingData.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_borrowingData.ClearSelection()
- If rowIndex = 0 Then
- MsgBox("You are already looking at the first record")
- Else
- rowIndex = rowIndex - 1
- dgv_borrowingData.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_borrowingData.ClearSelection()
- If rowIndex = lineCount - 1 Then
- MsgBox("You are already looking at the last record")
- Else
- rowIndex = rowIndex + 1
- dgv_borrowingData.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_borrowingData.ClearSelection()
- dgv_borrowingData.Rows(lineCount - 1).Selected = True 'go to the last record stored
- rowIndex = lineCount - 1
- End Sub
- Private Sub txt_bookID_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_bookID.MouseClick
- If txt_bookID.Text = "BookID" Then 'if text is start text
- txt_bookID.Text = "" 'clear text box
- End If
- End Sub
- Private Sub txt_dateBorrowed_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_dateBorrowed.MouseClick
- If txt_dateBorrowed.Text = "Date Borrowed" Then 'same as bookID
- txt_dateBorrowed.Text = ""
- End If
- End Sub
- Private Sub txt_dueDate_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_dueDate.MouseClick
- If txt_dueDate.Text = "Due Date" Then
- txt_dueDate.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 BorrowingID" Then
- txt_search.Text = ""
- End If
- End Sub
- Private Sub btn_viewAll_Click(sender As Object, e As EventArgs) Handles btn_viewAll.Click
- fillGrid() 'fill the grid
- 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 the new form
- Me.Close() 'close the 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
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement