UsernameHere1

Untitled

Feb 21st, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 16.91 KB | None | 0 0
  1. Imports System.IO 'imports the inputs and outputs for the program
  2. Imports System.Security.Permissions
  3.  
  4. Public Class Catalogue 'catalogue form
  5.     Dim lineCount As Integer 'The full amount of records in the file
  6.     Dim rowIndex As Integer 'The currently selected row of the datagrid
  7.     Private Sub bubbleSort(ByVal index As Integer)
  8.         Dim tempArray(3) As String 'temporay array for swaps
  9.         Dim record1(3) As String 'records being compared
  10.         Dim record2(3) As String
  11.         For passes = 0 To lineCount - 1
  12.             For counter = 0 To lineCount - 2 'to last but one row in data grid
  13.                 record1(0) = dgv_catalogue.Rows(counter).Cells(0).Value 'set array values to records
  14.                 record1(1) = dgv_catalogue.Rows(counter).Cells(1).Value
  15.                 record1(2) = dgv_catalogue.Rows(counter).Cells(2).Value
  16.                 record2(0) = dgv_catalogue.Rows(counter + 1).Cells(0).Value
  17.                 record2(1) = dgv_catalogue.Rows(counter + 1).Cells(1).Value
  18.                 record2(2) = dgv_catalogue.Rows(counter + 1).Cells(2).Value
  19.                 If record1(index) > record2(index) Then 'swap (comparison based on the index of value)
  20.                     tempArray = record1
  21.                     record1 = record2
  22.                     record2 = tempArray
  23.                 End If
  24.                 dgv_catalogue.Rows(counter).SetValues(record1) 'updates datagrid
  25.                 dgv_catalogue.Rows(counter + 1).SetValues(record2)
  26.             Next
  27.         Next
  28.     End Sub
  29.     Private Sub fillGrid()
  30.         Dim encryption As New clsEncryption 'instantiates the encryption class
  31.         lineCount = File.ReadAllLines("books.txt").Length 'make the linecount be the amount of records in the file
  32.         Dim startupPath As String 'store the location the file is running
  33.         startupPath = Application.StartupPath
  34.         Dim record As String 'a record from the file
  35.         Dim fields() As String 'the fields in a record
  36.         dgv_catalogue.Rows.Clear() 'clear the datagrid
  37.         Dim sr As New StreamReader("books.txt") 'a new stream reader of the books file
  38.         While sr.Peek() >= 0 'while there are record in the file to be read
  39.             record = encryption.decrypt(sr.ReadLine) 'decrypt the record
  40.             fields = record.Split(",") 'split the record into fields
  41.             Dim index = dgv_catalogue.Rows.Add 'the next row to add to
  42.             dgv_catalogue.Rows(index).SetValues(fields) 'add a record to the datagrid by putting each field in its correct place
  43.         End While
  44.         sr.Close() 'close the stream reader
  45.         dgv_catalogue.RowHeadersVisible = False 'Removes the symbols by the rows
  46.         dgv_catalogue.ClearSelection() 'Removes the blue selection box
  47.         dgv_catalogue.Rows(lineCount - 1).Selected = True 'select the final record in the datagrid
  48.         rowIndex = lineCount - 1 'show the currenty corrected row
  49.         txt_bookName.Text = "Book Name" 'reset text boxes
  50.         txt_author.Text = "Author"
  51.         txt_bookID.Text = "BookID"
  52.     End Sub
  53.     Private Sub Catalogue_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'when form loads
  54.         Dim encryption As New clsEncryption 'instantiate the encryption class
  55.         Me.CenterToScreen() 'centre the form to the screen
  56.         Dim startupPath As String 'store the location the file is running
  57.         startupPath = Application.StartupPath
  58.         txt_author.Text = "Author" 'reset the text boxes
  59.         txt_bookName.Text = "Book Name"
  60.         txt_search.Text = "Search by Book Name"
  61.         If levelOfAccess = 3 Then
  62.             Me.BackColor = Color.FromArgb(247, 220, 143) 'admins have yellow background
  63.         End If
  64.         If levelOfAccess = 2 Then
  65.             Me.BackColor = Color.FromArgb(140, 227, 245) 'librarians have blue background
  66.         End If
  67.         If levelOfAccess = 1 Then
  68.             Me.BackColor = Color.FromArgb(163, 255, 166) 'customers have green background
  69.             btn_add.Visible = False 'Make the add function not accessible to customers
  70.             btn_overwrite.Visible = False
  71.             txt_bookName.Visible = False
  72.             txt_author.Visible = False
  73.         End If
  74.         pbx_logo.ImageLocation = startupPath & "\PennardLibraryLogo.png" 'set up image locations
  75.         pbx_back.ImageLocation = startupPath & "\Arrow.png"
  76.         If Dir$(startupPath & "\books.txt") = "" Then 'if file doesnt exist
  77.             Dim sw As New StreamWriter("books.txt", True) 'stream writer of the books file
  78.             sw.WriteLine(encryption.encrypt("1,The Way of Kings,Brandon Sanderson")) 'Write a book in to be available no matter what
  79.             sw.Close() 'close the stream writer
  80.             MsgBox("A new file has been created") 'notify user
  81.         End If
  82.         If Dir(startupPath & "\tempBooks.txt") = "" Then 'if temp file doesnt exist
  83.             Dim sw As New StreamWriter("tempBooks.txt", False) 'create file with a stream writer
  84.             sw.Close() 'close stream writer
  85.         End If
  86.         fillGrid() 'fill the data grid
  87.     End Sub
  88.     Private Sub pbx_back_Click(sender As Object, e As EventArgs) Handles pbx_back.Click
  89.         Dim MainMenu As New MainMenu 'new instance of the main menu form
  90.         MainMenu.Show() 'show the new form
  91.         Me.Close() 'close the current form
  92.     End Sub
  93.     Private Sub txt_search_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_search.MouseClick
  94.         If txt_search.Text = "Search by Book Name" Then 'if the current text is the start text
  95.             txt_search.Text = "" 'clear the text box
  96.         End If
  97.     End Sub
  98.     Private Sub txt_bookName_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_bookName.MouseClick
  99.         If txt_bookName.Text = "Book Name" Then 'if the current text is the start text
  100.             txt_bookName.Text = "" 'clear the text box
  101.         End If
  102.     End Sub
  103.     Private Sub txt_author_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_author.MouseClick
  104.         If txt_author.Text = "Author" Then 'if the current text is the start text
  105.             txt_author.Text = "" 'clear the text box
  106.         End If
  107.     End Sub
  108.     Private Sub txt_ID_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_author.MouseClick
  109.         If txt_bookID.Text = "BookID" Then 'if the current text is the start text
  110.             txt_bookID.Text = "" 'clear the text box
  111.         End If
  112.     End Sub
  113.     Private Sub btn_add_Click(sender As Object, e As EventArgs) Handles btn_add.Click
  114.         Dim encryption As New clsEncryption 'insantiate the encryption class
  115.         Dim validation As New clsValidation 'instantiate the clsValidation class
  116.         Dim validated As Boolean = True 'is all validation met
  117.         Dim record As String 'a record from the file
  118.         Dim fields() As String 'the fields in the record
  119.         Dim addBookID As Integer = 0 'the bookID to be added
  120.         Dim addBookName As String 'the book name to be added
  121.         Dim addAuthor As String 'the author to be added
  122.         Dim sr As New StreamReader("books.txt") 'a stream reader of the book file
  123.         While sr.Peek() >= 0 'while there is a record to be read
  124.             record = encryption.decrypt(sr.ReadLine) 'decrypt the record
  125.             fields = record.Split(",") 'split into fields
  126.             If fields(0) >= addBookID Then 'if an ID is found with a higher ID
  127.                 addBookID = fields(0) + 1 'Makes sure the bookID being added is always 1 greater than the current largest stored
  128.             End If
  129.         End While
  130.         sr.Close()
  131.         If validation.characterCheck(txt_bookName.Text, ",") = False Or validation.characterCheck(txt_author.Text, ",") = False Then 'doesnt allow for commas to be entered
  132.             validated = False 'validation isnt met
  133.             MsgBox("Data cannot contain commas") 'notify user
  134.         End If
  135.         If validation.presenceCheck(txt_bookName.Text) = True And txt_bookName.Text <> "Book Name" Then 'if the data is valid
  136.             addBookName = txt_bookName.Text 'set value of new book name
  137.         Else
  138.             validated = False 'validation isnt met
  139.         End If
  140.         If validation.presenceCheck(txt_author.Text) = True And txt_author.Text <> "Author" Then 'if the data is valid
  141.             addAuthor = txt_author.Text 'set value of the new author
  142.         Else
  143.             validated = False 'validation isnt met
  144.         End If
  145.         If validated = True Then 'if validation is met
  146.             Dim sw As New StreamWriter("books.txt", True) 'a stream writer of the book file
  147.             sw.WriteLine(encryption.encrypt(addBookID & "," & addBookName & "," & addAuthor)) 'add a record to the file
  148.             sw.Close() 'close the stream writer
  149.         Else
  150.             MsgBox("You need to fill out all fields") 'notify user that validation isnt met
  151.         End If
  152.         fillGrid() 'fill the grid
  153.     End Sub
  154.  
  155.     Private Sub btn_viewAll_Click(sender As Object, e As EventArgs) Handles btn_viewAll.Click
  156.         fillGrid() 'fill the grid
  157.     End Sub
  158.  
  159.     Private Sub btn_search_Click(sender As Object, e As EventArgs) Handles btn_search.Click
  160.         Dim encryption As New clsEncryption 'instantiate the encryption class
  161.         Dim validation As New clsValidation 'instantiate the validation class
  162.         lineCount = File.ReadAllLines("books.txt").Length 'the amount of records stored
  163.         Dim found As Boolean = False 'has the record been found
  164.         Dim record As String 'a record from the file
  165.         Dim fields() As String 'the fields in the record
  166.         Dim recordCount As Integer = 0 'the amount of record stored
  167.         Dim sr As New StreamReader("books.txt") 'a stream reader of the book file
  168.         If validation.presenceCheck(txt_search.Text) = True And txt_search.Text <> "Search by Book Name" Then 'if validation is met
  169.             While sr.Peek() >= 0 'while there are records to be read
  170.                 recordCount = recordCount + 1 'count the amount of records stored
  171.                 record = encryption.decrypt(sr.ReadLine) 'decrypt the record
  172.                 fields = record.Split(",") 'split the record into fields
  173.                 If fields(1) = txt_search.Text Then 'if the name is found in the record
  174.                     dgv_catalogue.ClearSelection() 'unselect row
  175.                     dgv_catalogue.Rows(recordCount - 1).Selected = True 'make the found record be selected
  176.                     found = True 'record has been found
  177.                 ElseIf recordCount = lineCount And found = False Then 'if not found
  178.                     MsgBox("There is no book stored with that name") 'notify user
  179.                 End If
  180.             End While
  181.         Else
  182.             MsgBox("You must fill out all fields") 'notify user they must fill all fields
  183.         End If
  184.         txt_search.Text = "Search by Book Name" 'reset textboxes
  185.     End Sub
  186.  
  187.     Private Sub btn_first_Click(sender As Object, e As EventArgs) Handles btn_first.Click
  188.         dgv_catalogue.ClearSelection()
  189.         dgv_catalogue.Rows(0).Selected = True 'gp back to the first record
  190.         rowIndex = 0
  191.     End Sub
  192.  
  193.     Private Sub btn_previous_Click(sender As Object, e As EventArgs) Handles btn_previous.Click
  194.         dgv_catalogue.ClearSelection()
  195.         If rowIndex = 0 Then
  196.             MsgBox("You are already looking at the first record")
  197.         Else
  198.             rowIndex = rowIndex - 1
  199.             dgv_catalogue.Rows(rowIndex).Selected = True 'go one record back unless you are at the fist one already
  200.         End If
  201.     End Sub
  202.  
  203.     Private Sub btn_next_Click(sender As Object, e As EventArgs) Handles btn_next.Click
  204.         dgv_catalogue.ClearSelection()
  205.         If rowIndex = lineCount - 1 Then
  206.             MsgBox("You are already looking at the last record")
  207.         Else
  208.             rowIndex = rowIndex + 1
  209.             dgv_catalogue.Rows(rowIndex).Selected = True 'go on record forwards unless you are at the last one already
  210.         End If
  211.     End Sub
  212.  
  213.     Private Sub btn_last_Click(sender As Object, e As EventArgs) Handles btn_last.Click
  214.         dgv_catalogue.ClearSelection()
  215.         dgv_catalogue.Rows(lineCount - 1).Selected = True 'go to the last record stored
  216.         rowIndex = lineCount - 1
  217.     End Sub
  218.  
  219.     Private Sub btn_overwrite_Click(sender As Object, e As EventArgs) Handles btn_overwrite.Click
  220.         Dim swTemp As New StreamWriter("tempBooks.txt", False) 'empties the tempBooks file for use in this subroutine
  221.         swTemp.Close() 'A temp file must be used so that the stream reader reads correctly while the streamwriter writes
  222.         Dim validation As New clsValidation 'instantiate the validation class
  223.         Dim encryption As New clsEncryption 'instantiate the encryption class
  224.         Dim editBookName As String 'the bookName to overwrite with
  225.         Dim editAuthor As String 'the author to overwrite with
  226.         Dim validated As Boolean = True 'is data validated
  227.         Dim IDValidated As Boolean = True 'is ID validated
  228.         Dim searchID As String 'the ID being searched for
  229.         Dim record As String 'a record from the file
  230.         Dim fields() As String 'the fields in the record
  231.         Dim found As Boolean = False 'hasnt been found yet
  232.         If validation.presenceCheck(txt_bookName.Text) = True And txt_bookName.Text <> "Book Name" Then 'makes sure that the data to overwrite with makes sense
  233.             editBookName = txt_bookName.Text 'set value
  234.         Else
  235.             validated = False 'not validated
  236.             MsgBox("Must have a value for the new book name") 'notify user
  237.         End If
  238.         If validation.presenceCheck(txt_author.Text) = True And txt_author.Text <> "Author" Then 'validated
  239.             editAuthor = txt_author.Text 'set values
  240.         Else
  241.             validated = False 'not validated
  242.             MsgBox("Must have a value for the new author") 'notify user
  243.         End If
  244.         If validation.presenceCheck(txt_bookID.Text) = True And txt_bookID.Text <> "BookID" Then 'Make sure and ID is entered
  245.             searchID = txt_bookID.Text 'set values
  246.         Else
  247.             IDValidated = False 'not validated
  248.             MsgBox("Must enter an ID to replace the record of") 'notify user
  249.         End If
  250.         If IDValidated = True And validated = True Then 'If all validation is true
  251.             Dim sr As New StreamReader("books.txt") 'stream reader of books file
  252.             Dim swTemp2 As New StreamWriter("tempBooks.txt", True) 'streamwriter of temp book file
  253.             While sr.Peek() >= 0
  254.                 record = encryption.decrypt(sr.ReadLine) 'read line unencrypted
  255.                 fields = record.Split(",") 'split into fields
  256.                 If fields(0) <> searchID Then 'if the ID isnt the searched ID
  257.                     swTemp2.WriteLine(encryption.encrypt(record)) 'Write old line into temp file
  258.                 Else
  259.                     swTemp2.WriteLine(encryption.encrypt(searchID & "," & editBookName & "," & editAuthor)) 'adds new edited data to the temp file
  260.                     found = True 'has been found
  261.                 End If
  262.             End While
  263.             sr.Close() 'close stream reader
  264.             swTemp2.Close() 'close stream writer
  265.         Else
  266.             MsgBox("Must meet all validation requirements") 'notify user
  267.         End If
  268.         If found = True Then 'if the ID was found
  269.             Dim srTemp As New StreamReader("tempBooks.txt") 'stream reader of the temp file
  270.             Dim sw As New StreamWriter("books.txt", False) 'will overwrite previous file
  271.             sw.Close() 'close stream writer
  272.             Dim sw1 As New StreamWriter("books.txt", True) 'writes new file
  273.             While srTemp.Peek() >= 0
  274.                 record = srTemp.ReadLine 'each record from the temp file
  275.                 sw1.WriteLine(record) 'write the record from the temp file into the normal file (overwriting)
  276.             End While
  277.             srTemp.Close() 'close stream reader
  278.             sw1.Close() 'close stream writer
  279.         Else
  280.             MsgBox("No record found with that ID") 'notify user that the ID wasnt found
  281.         End If
  282.         fillGrid() 'fill the datagrid
  283.     End Sub
  284.  
  285.     Private Sub pbx_back_MouseHover(sender As Object, e As EventArgs) Handles pbx_back.MouseHover
  286.         Dim startupPath As String 'stores the location the program is running
  287.         startupPath = Application.StartupPath
  288.         pbx_back.ImageLocation = startupPath & "\ArrowRollover.png" 'change to rollover
  289.     End Sub
  290.  
  291.     Private Sub pbx_back_MouseLeave(sender As Object, e As EventArgs) Handles pbx_back.MouseLeave
  292.         Dim startupPath As String 'stores the location the program is running
  293.         startupPath = Application.StartupPath
  294.         pbx_back.ImageLocation = startupPath & "\Arrow.png" 'change to normal
  295.     End Sub
  296.  
  297.     Private Sub rbt_sortBookID_Click(sender As Object, e As EventArgs) Handles rbt_sortBookID.Click
  298.         bubbleSort(0) 'sort by ID
  299.     End Sub
  300.  
  301.     Private Sub rbt_bookName_CheckedChanged(sender As Object, e As EventArgs) Handles rbt_bookName.CheckedChanged
  302.         bubbleSort(1) 'sort by Name
  303.     End Sub
  304. End Class
Add Comment
Please, Sign In to add comment