Advertisement
UsernameHere1

Untitled

Feb 21st, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 13.17 KB | None | 0 0
  1. Imports System.IO 'imports input and output functions
  2.  
  3. Public Class BorrowingData 'borrowing data form
  4.     Dim lineCount As Integer 'The full amount of records in the file
  5.     Dim rowIndex As Integer 'The currently selected row of the datagrid
  6.     Private Sub fillGrid()
  7.         Dim encryption As New clsEncryption 'instantiates the encryption class
  8.         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)
  9.         Dim startupPath As String 'stores location the program is running
  10.         startupPath = Application.StartupPath
  11.         Dim record1 As String 'record from borrowing data file
  12.         Dim fields1() As String 'fields from record
  13.         Dim record2 As String 'record from individual borrowings file
  14.         Dim fields2() As String 'fields from record
  15.         Dim customerID As String ''the customer ID found
  16.         Dim found As Boolean = False 'has it been found
  17.         dgv_borrowingData.Rows.Clear() 'clear text box
  18.         Dim sr1 As New StreamReader("borrowingData.txt") 'stream reader of borrowing data file
  19.         While sr1.Peek() >= 0 'while there are record to read
  20.             record1 = encryption.decrypt(sr1.ReadLine) 'decrypt record
  21.             fields1 = record1.Split(",") 'split record into fields
  22.             Dim index = dgv_borrowingData.Rows.Add 'the row to add to
  23.             Dim sr2 As New StreamReader("individualBorrowings.txt") 'stream reader of individual borrowings file
  24.             While sr2.Peek() >= 0 'check every record of the individual borrowing file
  25.                 record2 = encryption.decrypt(sr2.ReadLine) 'decrypt record
  26.                 fields2 = record2.Split(",") 'split into fields
  27.                 If fields1(0) = fields2(0) Then 'if there is a match on borrowingID
  28.                     customerID = fields2(1) 'Add the customerID
  29.                     found = True 'has been found
  30.                 Else
  31.                     If found = False Then
  32.                         customerID = "N/A" 'There is no customerID
  33.                     End If
  34.                 End If
  35.                 dgv_borrowingData.Rows(index).SetValues(fields1)
  36.                 dgv_borrowingData.Rows(index).Cells(4).Value = customerID 'add the value into the customerID cell
  37.             End While
  38.             sr2.Close() 'close stream reader
  39.         End While
  40.         sr1.Close() 'close stream reader
  41.         dgv_borrowingData.RowHeadersVisible = False 'Removes the symbols by the rows
  42.         dgv_borrowingData.ClearSelection() 'Removes the blue selection box
  43.         dgv_borrowingData.Rows(lineCount - 1).Selected = True 'select the final record in the datagrid
  44.         rowIndex = lineCount - 1 'show the currenty corrected row
  45.         txt_bookID.Text = "BookID" 'reset text boxes
  46.         txt_dateBorrowed.Text = "Date Borrowed"
  47.         txt_dueDate.Text = "Due Date"
  48.         txt_search.Text = "Search by BorrowingID"
  49.         txt_customerID.Text = "CustomerID"
  50.     End Sub
  51.     Private Sub BorrowingData_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'when form loads
  52.         Me.CenterToScreen() 'center form to screen
  53.         Dim encryption As New clsEncryption 'instantiate encryption class
  54.         Dim startupPath As String = Application.StartupPath 'stores location the program is running
  55.         pbx_logo.ImageLocation = startupPath & "\PennardLibraryLogo.png" 'sets image locations
  56.         pbx_back.ImageLocation = startupPath & "\Arrow.png"
  57.         If levelOfAccess = 2 Then
  58.             Me.BackColor = Color.FromArgb(140, 227, 245) 'use blue for librarians
  59.         End If
  60.         If levelOfAccess = 3 Then
  61.             Me.BackColor = Color.FromArgb(247, 220, 143) 'use yellow for admins
  62.         End If
  63.         If Dir$(startupPath & "\borrowingData.txt") = "" Then 'if file doesnt exist
  64.             Dim sw As New StreamWriter("borrowingData.txt", True) 'stream writer to append borrowing data file
  65.             sw.WriteLine(encryption.encrypt("1,1,01/01/2000,09/01/2000")) 'file containg borrowing information
  66.             sw.Close() 'close stream writer
  67.             MsgBox("New file created") 'notify user the file was made
  68.         End If
  69.         If Dir(startupPath & "\individualBorrowings.txt") = "" Then 'if file doesnt exist
  70.             Dim sw As New StreamWriter("individualBorrowings.txt", True) 'file containing borrowingID and customerID for linking entities
  71.             sw.WriteLine(encryption.encrypt("1,1"))
  72.             sw.Close() 'close stream writer
  73.             MsgBox("New file created") 'notify user
  74.         End If
  75.         fillGrid() 'fill the data grid
  76.     End Sub
  77.     Private Sub btn_add_Click(sender As Object, e As EventArgs) Handles btn_add.Click
  78.         Dim encryption As New clsEncryption 'instantiate classes
  79.         Dim validation As New clsValidation
  80.         Dim validated As Boolean = True 'is data validated
  81.         Dim record As String 'record from file
  82.         Dim fields() As String 'fields in record
  83.         Dim addBookID As String 'the data to add to file
  84.         Dim addBorrowingDate As String
  85.         Dim addDueDate As String
  86.         Dim addCustomerID As String
  87.         Dim addBorrowingID As Integer = 0
  88.         Dim sr As New StreamReader("borrowingData.txt") 'stream reader of borrowing data file
  89.         While sr.Peek() >= 0
  90.             record = encryption.decrypt(sr.ReadLine) 'decrypt record
  91.             fields = record.Split(",") 'split record
  92.             If fields(0) >= addBorrowingID Then 'if ID is higher than ID being added
  93.                 addBorrowingID = fields(0) + 1 'Makes sure the bookID being added is always 1 greater than the current largest stored
  94.             End If
  95.         End While
  96.         sr.Close() 'close stream reader
  97.         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
  98.             validated = False 'not all validation is met
  99.             MsgBox("Data cannot contain commas") 'notify user
  100.         End If
  101.         If validation.presenceCheck(txt_bookID.Text) = True And txt_bookID.Text <> "BookID" Then 'presence check
  102.             addBookID = txt_bookID.Text 'set value
  103.         Else
  104.             validated = False 'doesnt meet validation
  105.         End If
  106.         If validation.presenceCheck(txt_customerID.Text) = True And txt_customerID.Text <> "CustomerID" Then
  107.             addCustomerID = txt_customerID.Text 'same as bookID
  108.         Else
  109.             validated = False
  110.         End If
  111.         If validation.presenceCheck(txt_dateBorrowed.Text) = True And txt_dateBorrowed.Text <> "Date Borrowed" And validation.lengthCheck(txt_dateBorrowed.Text, 10, 10) Then
  112.             addBorrowingDate = txt_dateBorrowed.Text 'also has length check
  113.         Else
  114.             validated = False
  115.         End If
  116.         If validation.presenceCheck(txt_dueDate.Text) = True And txt_dueDate.Text <> "Due Date" And validation.lengthCheck(txt_dueDate.Text, 10, 10) Then
  117.             addDueDate = txt_dueDate.Text
  118.         Else
  119.             validated = False
  120.         End If
  121.         If validated = True Then 'if validation is met
  122.             Dim sw1 As New StreamWriter("borrowingData.txt", True) 'stream writers of the respective files
  123.             Dim sw2 As New StreamWriter("individualBorrowings.txt", True)
  124.             sw1.WriteLine(encryption.encrypt(addBorrowingID & "," & addBookID & "," & addBorrowingDate & "," & addDueDate)) 'writes encrypted records
  125.             sw2.WriteLine(encryption.encrypt(addBorrowingID & "," & addCustomerID))
  126.             sw1.Close() 'close stream writers
  127.             sw2.Close()
  128.         Else
  129.             MsgBox("Must meet all validation requirements") 'notify user
  130.         End If
  131.         fillGrid() 'fill the data grid
  132.     End Sub
  133.     Private Sub btn_search_Click(sender As Object, e As EventArgs) Handles btn_search.Click
  134.         Dim encryption As New clsEncryption 'instantiate classes
  135.         Dim validation As New clsValidation
  136.         lineCount = File.ReadAllLines("borrowingData.txt").Length 'the amount of records in the file
  137.         Dim found As Boolean = False 'has it been found
  138.         Dim record As String 'record from file
  139.         Dim fields() As String 'fields in record
  140.         Dim recordCount As Integer = 0 'the amount of records
  141.         Dim sr As New StreamReader("borrowingData.txt") 'stream reader of the borrowing data file
  142.         If validation.presenceCheck(txt_search.Text) = True And txt_search.Text <> "Search by BorrowingID" Then 'presence check
  143.             While sr.Peek() >= 0 'while there are records to read
  144.                 recordCount = recordCount + 1 'count the amount of records stored
  145.                 record = encryption.decrypt(sr.ReadLine) 'decrypt record
  146.                 fields = record.Split(",") 'split record
  147.                 If fields(0) = txt_search.Text Then 'if the ID is found
  148.                     dgv_borrowingData.ClearSelection() 'unselect row
  149.                     dgv_borrowingData.Rows(recordCount - 1).Selected = True 'make the found record be selected
  150.                     found = True 'has been found
  151.                 ElseIf recordCount = lineCount And found = False Then 'if not found
  152.                     MsgBox("There is customer with that ID") 'notify user
  153.                 End If
  154.             End While
  155.         Else
  156.             MsgBox("You must fill out all fields")
  157.         End If
  158.         sr.Close() 'close stream reader
  159.         txt_search.Text = "Search by BorrowingID" 'reset text box
  160.     End Sub
  161.     Private Sub btn_first_Click(sender As Object, e As EventArgs) Handles btn_first.Click
  162.         dgv_borrowingData.ClearSelection()
  163.         dgv_borrowingData.Rows(0).Selected = True 'gp back to the first record
  164.         rowIndex = 0
  165.     End Sub
  166.  
  167.     Private Sub btn_previous_Click(sender As Object, e As EventArgs) Handles btn_previous.Click
  168.         dgv_borrowingData.ClearSelection()
  169.         If rowIndex = 0 Then
  170.             MsgBox("You are already looking at the first record")
  171.         Else
  172.             rowIndex = rowIndex - 1
  173.             dgv_borrowingData.Rows(rowIndex).Selected = True 'go one record back unless you are at the fist one already
  174.         End If
  175.     End Sub
  176.  
  177.     Private Sub btn_next_Click(sender As Object, e As EventArgs) Handles btn_next.Click
  178.         dgv_borrowingData.ClearSelection()
  179.         If rowIndex = lineCount - 1 Then
  180.             MsgBox("You are already looking at the last record")
  181.         Else
  182.             rowIndex = rowIndex + 1
  183.             dgv_borrowingData.Rows(rowIndex).Selected = True 'go on record forwards unless you are at the last one already
  184.         End If
  185.     End Sub
  186.  
  187.     Private Sub btn_last_Click(sender As Object, e As EventArgs) Handles btn_last.Click
  188.         dgv_borrowingData.ClearSelection()
  189.         dgv_borrowingData.Rows(lineCount - 1).Selected = True 'go to the last record stored
  190.         rowIndex = lineCount - 1
  191.     End Sub
  192.     Private Sub txt_bookID_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_bookID.MouseClick
  193.         If txt_bookID.Text = "BookID" Then 'if text is start text
  194.             txt_bookID.Text = "" 'clear text box
  195.         End If
  196.     End Sub
  197.  
  198.     Private Sub txt_dateBorrowed_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_dateBorrowed.MouseClick
  199.         If txt_dateBorrowed.Text = "Date Borrowed" Then 'same as bookID
  200.             txt_dateBorrowed.Text = ""
  201.         End If
  202.     End Sub
  203.  
  204.     Private Sub txt_dueDate_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_dueDate.MouseClick
  205.         If txt_dueDate.Text = "Due Date" Then
  206.             txt_dueDate.Text = ""
  207.         End If
  208.     End Sub
  209.  
  210.     Private Sub txt_customerID_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_customerID.MouseClick
  211.         If txt_customerID.Text = "CustomerID" Then
  212.             txt_customerID.Text = ""
  213.         End If
  214.     End Sub
  215.     Private Sub txt_search_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_search.MouseClick
  216.         If txt_search.Text = "Search by BorrowingID" Then
  217.             txt_search.Text = ""
  218.         End If
  219.     End Sub
  220.  
  221.     Private Sub btn_viewAll_Click(sender As Object, e As EventArgs) Handles btn_viewAll.Click
  222.         fillGrid() 'fill the grid
  223.     End Sub
  224.     Private Sub pbx_back_Click(sender As Object, e As EventArgs) Handles pbx_back.Click
  225.         Dim MainMenu As New MainMenu 'new instance of main menu form
  226.         MainMenu.Show() 'show the new form
  227.         Me.Close() 'close the current form
  228.     End Sub
  229.     Private Sub pbx_back_MouseHover(sender As Object, e As EventArgs) Handles pbx_back.MouseHover
  230.         Dim startupPath As String 'stores location the program is running
  231.         startupPath = Application.StartupPath
  232.         pbx_back.ImageLocation = startupPath & "\ArrowRollover.png" 'rollover
  233.     End Sub
  234.  
  235.     Private Sub pbx_back_MouseLeave(sender As Object, e As EventArgs) Handles pbx_back.MouseLeave
  236.         Dim startupPath As String 'stores location the program is running
  237.         startupPath = Application.StartupPath
  238.         pbx_back.ImageLocation = startupPath & "\Arrow.png" 'back to normal
  239.     End Sub
  240. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement