Advertisement
ElliottE4

Untitled

Feb 18th, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 19.96 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Net.Security 'imports the system io library in order to use the stream reader/writer commands
  3.  
  4. Public Class Checkout
  5.  
  6.     Dim CheckOutData As String 'creates a new string variable for the checkout textfile data to be stored in
  7.     Dim memberData As String 'creates a new string variable for the member textfile data to be stored in
  8.     Dim StaffData As String 'creates a new string variable for the staff textfile data to be stored in
  9.     Dim BookData As String 'creates a new string variable for the book textfile data to be stored in
  10.     Dim LineCount As Integer 'creates a new integer variabe that will store the number of lines in the textfile
  11.     Dim RowIndex As Integer = 0 'creates a new integer variable that will store the index of the current row, and it is set to 0 to begin
  12.  
  13.  
  14.     Private Sub DataGridRefresh() 'creates a new sub that can be called when the data grid needs to be refreshed
  15.  
  16.         CheckOutData = Application.StartupPath & "\checkout.txt" 'sets the CheckOutData variable equal to the file path of the CheckOut text file
  17.         If Dir(CheckOutData) = "" Then 'checks if the CheckOut text file directory has no data
  18.  
  19.             'Opening a streamwriter creates a new textfile if there isn't a textfile to be written in
  20.             Dim sw As New StreamWriter(CheckOutData, True) 'false will overwrite text, so use true unless you need to remove text
  21.             sw.Close() 'closes the streamwriter to avoid data loss
  22.             MsgBox("File has been created") 'shows message to the user to make them aware that a new file has been created
  23.  
  24.         End If
  25.  
  26.         Dim NewRecord As String 'creates a new string variable that will store the data in a record in the texfile
  27.         Dim Fields() As String 'creates a new string array to store each record with comma splits to identify each field
  28.  
  29.         dgv_CheckOut.Rows.Clear() 'clears the data grid
  30.  
  31.         LineCount = File.ReadAllLines(CheckOutData).Length 'makes the linecount equal to the number of lines in the textfile
  32.  
  33.         Dim sr As New StreamReader(CheckOutData) 'opens a new streamwreader to read the data in the textfile
  34.  
  35.         While sr.Peek >= 0 'creates a loop for each line until the end of the file
  36.  
  37.             NewRecord = sr.ReadLine 'the newrecord variable stores the data on the currently viewed line (record) in the text file
  38.             Fields = NewRecord.Split(",") 'the fields array stores each field in the record, by splitting the data at each comma
  39.  
  40.             Dim index = dgv_CheckOut.Rows.Add 'creates a new variable that adds data to the data grid
  41.  
  42.             dgv_CheckOut.Rows(index).SetValues(Decryption(Fields(0)), Decryption(Fields(1)), Decryption(Fields(2)), Decryption(Fields(3)), Decryption(Fields(4)))
  43.             'adds values to the datagrid for each field in the record, and also decrypts them so that they can be viewed
  44.  
  45.         End While
  46.         sr.Close() 'closes streamreader to avoid data loss
  47.  
  48.         dgv_CheckOut.RowHeadersVisible = False 'turns off the row headers
  49.         dgv_CheckOut.ClearSelection() 'unselects any highlighted rows in the datagrid
  50.  
  51.     End Sub
  52.  
  53.     Private Sub Checkout_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  54.  
  55.         'when the CheckOut form loads, the data grid will be refreshed by calling the subroutine
  56.         DataGridRefresh()
  57.  
  58.     End Sub
  59.  
  60.     Private Sub pbx_ExitButton_Click(sender As Object, e As EventArgs) Handles pbx_ExitButton.Click
  61.  
  62.         If MsgBox("Are you sure you want to exit to the book form?", vbYesNo) = vbYes Then 'shows the user a message box that asks if they want to exit to the book form
  63.  
  64.             'when the exit button is clicked, exit the form if yes is selected
  65.             Dim book As New Books 'creates new instance of the book form
  66.             Books.Show() 'shows the book form
  67.             Me.Close() 'closes the currrent form
  68.  
  69.  
  70.         End If
  71.  
  72.     End Sub
  73.  
  74.     Private Sub btn_Search_Click(sender As Object, e As EventArgs) Handles btn_Search.Click
  75.  
  76.         Dim i As Integer 'creates new integer variable to be used in a loop
  77.  
  78.  
  79.         If txt_CheckOutID.Text = "" Then 'if there is no data inputted into the CheckOutid textbox
  80.  
  81.             MsgBox("Please input a CheckOutID to search in the textbox and try again.") 'prompts the user to enter data
  82.  
  83.         Else 'when there is data in the CheckOutid textbox
  84.  
  85.             For i = 0 To LineCount - 1 'starts loop to read every line in the textfile
  86.  
  87.                 If txt_CheckOutID.Text = dgv_CheckOut.Rows(i).Cells(0).Value Then 'if the data in the CheckOutid textbox matches the first field of the currently selected record, then:
  88.  
  89.                     dgv_CheckOut.Rows(i).Selected = True 'highlight the currrently selected row
  90.  
  91.                     'make each textbox show the corresponding field from the data grid:
  92.                     txt_CheckOutID.Text = dgv_CheckOut.Rows(i).Cells(0).Value
  93.                     txt_CheckOutDate.Text = dgv_CheckOut.Rows(i).Cells(1).Value
  94.                     txt_MemberID.Text = dgv_CheckOut.Rows(i).Cells(2).Value
  95.                     txt_BookID.Text = dgv_CheckOut.Rows(i).Cells(3).Value
  96.                     txt_StaffID.Text = dgv_CheckOut.Rows(i).Cells(4).Value
  97.  
  98.                 End If
  99.  
  100.             Next
  101.             DataGridRefresh() 'refreshes data grid
  102.  
  103.             dgv_CheckOut.Rows(txt_CheckOutID.Text - 1).Selected = True 'highlights the row of the data grid view that matches the CheckOutID
  104.             'CheckOutid.text - 1 because the data grid starts with 0
  105.  
  106.  
  107.  
  108.         End If
  109.  
  110.     End Sub
  111.  
  112.     Private Sub btn_up_Click(sender As Object, e As EventArgs) Handles btn_up.Click
  113.  
  114.         If RowIndex > 0 Then 'if the current row selected is more than the first row
  115.  
  116.             RowIndex = RowIndex - 1 'decrease the row index by 1 which moves to the previous row
  117.  
  118.         Else
  119.  
  120.             MsgBox("The first record is selected") 'gives the user a message that tells them that the first row is already selected
  121.  
  122.         End If
  123.  
  124.         dgv_CheckOut.ClearSelection() 'this clears which row is highlighted
  125.         dgv_CheckOut.Rows(RowIndex).Selected = True 'this highlights the currently selected row
  126.  
  127.     End Sub
  128.  
  129.     Private Sub btn_Down_Click(sender As Object, e As EventArgs) Handles btn_Down.Click
  130.  
  131.         If RowIndex < LineCount - 1 Then 'if the current row selected is less than the number of rows then:
  132.  
  133.             RowIndex = RowIndex + 1 'increase index of the row by 1, which moves to the next row
  134.  
  135.         Else
  136.  
  137.             MsgBox("The last record is selected") 'this gives a message to the user that they alrady have the last record selected
  138.  
  139.         End If
  140.  
  141.         dgv_CheckOut.ClearSelection() 'this clears which row is highlighted
  142.         dgv_CheckOut.Rows(RowIndex).Selected = True 'this highlights the currently selected row
  143.  
  144.     End Sub
  145.  
  146.     Private Sub btn_clear_Click(sender As Object, e As EventArgs) Handles btn_clear.Click
  147.  
  148.  
  149.         'clears all textboxes
  150.         txt_CheckOutID.Clear()
  151.         txt_CheckOutDate.Clear()
  152.         txt_MemberID.Clear()
  153.         txt_BookID.Clear()
  154.         txt_StaffID.Clear()
  155.  
  156.         DataGridRefresh() 'refreshes data grid
  157.  
  158.     End Sub
  159.  
  160.     Private Sub btn_checkout_Click(sender As Object, e As EventArgs) Handles btn_checkout.Click
  161.         memberData = Application.StartupPath & "\member.txt" 'creates a constant that holds the location of the member file
  162.         BookData = Application.StartupPath & "\books.txt" 'creates a constant that holds the location of the book file
  163.         StaffData = Application.StartupPath & "\staff.txt" 'creates a constant that holds the location of the staff file
  164.  
  165.  
  166.         Dim validation As New Validation 'creates new instance of the validation class
  167.  
  168.         Dim memberIDcheck As String = txt_MemberID.Text 'creates a variable to store the value in the memberID textbox
  169.         Dim bookIDcheck As String = txt_BookID.Text 'creates a variable to store the value in the bookID textbox
  170.         Dim staffIDcheck As String = txt_StaffID.Text 'creates a variable to store the value in the staffID textbox
  171.  
  172.         Dim IDcheck As New IDcheck 'creates new instance of the IDcheck class
  173.  
  174.  
  175.         'if every textbox has no data inputed / presence check
  176.         If txt_CheckOutID.Text = "" Or txt_CheckOutDate.Text = "" Or txt_MemberID.Text = "" Or txt_BookID.Text = "" Or txt_StaffID.Text = "" Then
  177.  
  178.  
  179.             MsgBox("Please input data into all textboxes") 'shows message to ask user to input data into texboxes
  180.             txt_CheckOutID.Clear() 'clears textboxes
  181.             txt_CheckOutDate.Clear()
  182.             txt_MemberID.Clear()
  183.             txt_BookID.Clear()
  184.             txt_StaffID.Clear()
  185.  
  186.         ElseIf IDcheck.IDcheck(memberData, memberIDcheck) = False Then 'checks if the memberID input matches one in the database
  187.  
  188.             MsgBox("Please input a valid memberID") 'error message
  189.  
  190.         ElseIf IDcheck.IDcheck(BookData, bookIDcheck) = False Then 'checks if the bookID input matches one in the database
  191.  
  192.             MsgBox("Please input a valid bookID") 'error message
  193.  
  194.         ElseIf IDcheck.IDcheck(StaffData, staffIDcheck) = False Then 'checks if the staffID input matches one in the database
  195.  
  196.             MsgBox("Please input a valid staffID") 'error message
  197.  
  198.  
  199.         Else 'if there is data
  200.  
  201.             Dim sw As New StreamWriter(CheckOutData, True) 'opens new streamwriter
  202.  
  203.             'adds the information in the textboxes to the textfile using streamwrite, with commas to split it up
  204.             sw.WriteLine(Encryption(txt_CheckOutID.Text) & "," & Encryption(txt_CheckOutDate.Text) & "," & Encryption(txt_MemberID.Text) & "," & Encryption(txt_BookID.Text) & "," & Encryption(txt_StaffID.Text))
  205.             sw.Close()
  206.  
  207.             MsgBox("New file added") 'shows the user that the file has been added to database
  208.  
  209.             'clear all data in the textboxes
  210.             txt_CheckOutID.Text = ""
  211.             txt_CheckOutDate.Text = ""
  212.             txt_MemberID.Text = ""
  213.             txt_BookID.Text = ""
  214.             txt_StaffID.Text = ""
  215.  
  216.             DataGridRefresh() 'refreshes data grid
  217.  
  218.         End If
  219.  
  220.     End Sub
  221.  
  222.     Private Sub btn_Sort_Click(sender As Object, e As EventArgs) Handles btn_Sort.Click
  223.  
  224.         Dim i As Integer 'creates an integer variable to be used as a counter
  225.         Dim sortingArray(4) As String 'creates an array to store temporary data in order to swap the data in other arrays
  226.         Dim currentRecord(4) As String 'creates an array to store the data in the currently viewed record in the loop
  227.         Dim nextRecord(4) As String 'creates an array to store the data in the next record after the current record in the loop
  228.  
  229.         Dim sw1 As New StreamWriter(CheckOutData, False) 'deletes the data in the textfile
  230.         sw1.Close() 'closes streamwriter
  231.  
  232.         For i = 0 To LineCount - 1 'starts a loop to view every line in the textfile
  233.  
  234.             For counter = 0 To LineCount - 2 'creates another loop with linecount - 2 so that it gets to the second last row in the textfile (won't need to swap if it gets to the last row)
  235.  
  236.                 'makes the currentrecord array store every field of the current record
  237.                 currentRecord(0) = dgv_CheckOut.Rows(counter).Cells(0).Value
  238.                 currentRecord(1) = dgv_CheckOut.Rows(counter).Cells(1).Value
  239.                 currentRecord(2) = dgv_CheckOut.Rows(counter).Cells(2).Value
  240.                 currentRecord(3) = dgv_CheckOut.Rows(counter).Cells(3).Value
  241.                 currentRecord(4) = dgv_CheckOut.Rows(counter).Cells(4).Value
  242.  
  243.                 'makes the nextrecord array store every field in the next record
  244.                 nextRecord(0) = dgv_CheckOut.Rows(counter + 1).Cells(0).Value
  245.                 nextRecord(1) = dgv_CheckOut.Rows(counter + 1).Cells(1).Value
  246.                 nextRecord(2) = dgv_CheckOut.Rows(counter + 1).Cells(2).Value
  247.                 nextRecord(3) = dgv_CheckOut.Rows(counter + 1).Cells(3).Value
  248.                 nextRecord(4) = dgv_CheckOut.Rows(counter + 1).Cells(4).Value
  249.  
  250.                 If currentRecord(0) > nextRecord(0) Then 'if the first field in the current record is larger than the first field in the next array:
  251.  
  252.                     sortingArray = currentRecord 'makes the temporary sorting array store the data in the current record
  253.                     currentRecord = nextRecord 'the current record will then store the data from the next record
  254.                     nextRecord = sortingArray 'the next record will store the data from the current record
  255.  
  256.                     'this is essentially a swap of the data in the two arrays
  257.  
  258.                 End If
  259.  
  260.                 dgv_CheckOut.Rows(counter).SetValues(currentRecord) 'sets the currently viewed row in the datagrid to the data in the currentrecord array
  261.                 dgv_CheckOut.Rows(counter + 1).SetValues(nextRecord) 'sets the next row in the datagrid to the data in the nextrecord array
  262.  
  263.  
  264.  
  265.             Next
  266.  
  267.  
  268.             Dim sw As New StreamWriter(CheckOutData, True) 'opens new streamwriter to write data to the textfi;e
  269.  
  270.             'adds each sorted line in the datagrid to the textfile using the loop
  271.             sw.WriteLine(Encryption(dgv_CheckOut.Rows(i).Cells(0).Value) & "," & Encryption(dgv_CheckOut.Rows(i).Cells(1).Value) & "," & Encryption(dgv_CheckOut.Rows(i).Cells(2).Value) & "," & Encryption(dgv_CheckOut.Rows(i).Cells(3).Value) & "," & Encryption(dgv_CheckOut.Rows(i).Cells(4).Value))
  272.             sw.Close() 'closes streamwriter to avoid data loss
  273.  
  274.         Next
  275.     End Sub
  276.     Private Sub btn_Edit_Click(sender As Object, e As EventArgs) Handles btn_Edit.Click
  277.  
  278.         'shows the selected record in the textboxes to be edited
  279.         txt_CheckOutID.Text = dgv_CheckOut.Rows(RowIndex).Cells(0).Value 'each textbox shows the data from the corresponding field in the record currently selected
  280.         txt_CheckOutDate.Text = dgv_CheckOut.Rows(RowIndex).Cells(1).Value
  281.         txt_MemberID.Text = dgv_CheckOut.Rows(RowIndex).Cells(2).Value
  282.         txt_BookID.Text = dgv_CheckOut.Rows(RowIndex).Cells(3).Value
  283.         txt_StaffID.Text = dgv_CheckOut.Rows(RowIndex).Cells(4).Value
  284.         MsgBox("Edit the data in the textboxes, then click the save edited data button when finished") 'shows a message box to tell the user what to do
  285.  
  286.     End Sub
  287.     Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click
  288.         Dim fileLines() As String 'creates a new string array to store each line in the text file
  289.         Dim i As Integer 'creates new integer variable to be used in a loop
  290.         Dim fieldSplit() As String 'creates an array to store the data in each field of a record
  291.  
  292.         fileLines = File.ReadAllLines(CheckOutData)  'saves all of the data in the textfile to the variable
  293.  
  294.         For i = 0 To fileLines.Length - 1 'starts loop to read all of the lines
  295.  
  296.             fieldSplit = fileLines(i).Split(",") 'splits each record into each field using the commas
  297.  
  298.             If fieldSplit.Length > 0 AndAlso Decryption(fieldSplit(0)) = txt_CheckOutID.Text Then 'when there is data in the textfile, and the checkoutID matches:
  299.  
  300.                 'changes each field to match the new data in the textboxes
  301.                 'The data is also encrypted as the data in the datagrid view is decrypted, so it must be encrypted to be written
  302.                 fieldSplit(0) = Encryption(txt_CheckOutID.Text)
  303.                 fieldSplit(1) = Encryption(txt_CheckOutDate.Text)
  304.                 fieldSplit(2) = Encryption(txt_MemberID.Text)
  305.                 fieldSplit(3) = Encryption(txt_BookID.Text)
  306.                 fieldSplit(4) = Encryption(txt_StaffID.Text)
  307.  
  308.                 fileLines(i) = String.Join(",", fieldSplit) 'joins all data in each line back together with commas to split it
  309.  
  310.             End If
  311.         Next
  312.  
  313.         File.WriteAllLines(CheckOutData, fileLines) 'writes all the data back to the textfile
  314.         DataGridRefresh() 'refreshes datgrid
  315.  
  316.     End Sub
  317.  
  318.     Private Sub btn_Delete_Click(sender As Object, e As EventArgs) Handles btn_Delete.Click
  319.  
  320.         Dim fileLines() As String 'creates a new string array to store each line in the text file
  321.         Dim i As Integer 'creates new integer variable to be used in a loop
  322.         Dim fieldSplit() As String 'creates an array to store the data in each field of a record
  323.  
  324.  
  325.         fileLines = File.ReadAllLines(CheckOutData) 'saves all of the data in the textfile to the variable
  326.  
  327.         If MsgBox("Are you sure you want to delete?", vbYesNo) = vbYes Then 'shows message box to ask the user if they are sure they want to delete the record
  328.  
  329.             'if yes was clicked in the messsage box:
  330.  
  331.             If txt_CheckOutID.Text = "" Then 'if the checkoutid textbox is empty then:
  332.  
  333.                 MsgBox("Please input a checkoutID to delete in the textbox and try again.") 'shows the user a message box prompt to tell them to input data
  334.  
  335.             Else
  336.  
  337.                 Dim sw As New StreamWriter(CheckOutData, False) 'opens new streamwriter using false instead of true in order to delete data
  338.                 For i = 0 To fileLines.Length - 1 'starts new loop for every line in the textfile
  339.  
  340.                     fieldSplit = fileLines(i).Split(",") 'makes the fieldsplit array store each field in the current record by splitting the record at each comma
  341.  
  342.                     If txt_CheckOutID.Text <> dgv_CheckOut.Rows(i).Cells(0).Value AndAlso fieldSplit(i).Length > 0 Then
  343.  
  344.                         'if the data in the checkoutID textbox doesn't match the currently seleted record, and if the length of the field is more than 0, then:
  345.                         'the data will be written back to the textfile
  346.                         sw.WriteLine(Encryption(dgv_CheckOut.Rows(i).Cells(0).Value) & "," & Encryption(dgv_CheckOut.Rows(i).Cells(1).Value) & "," & Encryption(dgv_CheckOut.Rows(i).Cells(2).Value) & "," & Encryption(dgv_CheckOut.Rows(i).Cells(3).Value) & "," & Encryption(dgv_CheckOut.Rows(i).Cells(4).Value))
  347.  
  348.                     End If
  349.  
  350.                     'if the checkoutid in the textbox does match the currently selected record, it will be skipped from being re-written to the textfile
  351.  
  352.                 Next
  353.                 sw.Close() 'closes the streamwriter to avoid data loss
  354.                 DataGridRefresh() 'refreshes the data grid
  355.  
  356.             End If
  357.  
  358.         Else 'if the user choose "no" to continuing to delete the file, then:
  359.  
  360.             MsgBox("Data will not be deleted") 'shows a message box to confirm that the data will not be deleted
  361.  
  362.         End If
  363.  
  364.     End Sub
  365.  
  366.     Private Sub btn_Help_Click(sender As Object, e As EventArgs) Handles btn_Help.Click
  367.  
  368.         Help.txt_help.Text = "This is the checkout form. On the right of the form is a data grid viewm, which displays the checkout database. " _
  369.             & "You can navigate this by using either the arrow buttons underneath, or by searching for a specific checkoutID. " _
  370.             & "To search, you need to input a checkoutID into the checkoutID textbox, then click the search button. To checkout a book, " _
  371.             & "you will need to input all of the correct data into the corresponding textboxes on the left side of the form, then click the checkout book button. " _
  372.             & "If you wish to delete data from the database, you can input the checkoutID of the record you wish to delete into the checkoutID textbox, then click the delete button. " _
  373.             & "If you wish to edit data that is already in the database, navigate to the record you wish to edit, then click the edit button. " _
  374.             & "The existing data in that record will be displayed in the textboxes, and you can edit the data, then click the 'save edited data' button to save your edit. " _
  375.             & "If you want to clear the textboxes of unnecessary data, you can click the clear textboxes button to do this. " _
  376.             & "If the data is in an incorrect order (it should be chronological by checkoutID), click the sort button to sort the data. " _
  377.             & "Finally, to exit back to the menu form, you can click the cross button in the top right of the form."
  378.  
  379.         Help.Show()
  380.  
  381.     End Sub
  382. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement