Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO 'imports the inputs and outputs for the program
- Imports System.Security.Permissions
- Public Class Catalogue 'catalogue 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(3) As String 'temporay array for swaps
- Dim record1(3) As String 'records being compared
- Dim record2(3) 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_catalogue.Rows(counter).Cells(0).Value 'set array values to records
- record1(1) = dgv_catalogue.Rows(counter).Cells(1).Value
- record1(2) = dgv_catalogue.Rows(counter).Cells(2).Value
- record2(0) = dgv_catalogue.Rows(counter + 1).Cells(0).Value
- record2(1) = dgv_catalogue.Rows(counter + 1).Cells(1).Value
- record2(2) = dgv_catalogue.Rows(counter + 1).Cells(2).Value
- If record1(index) > record2(index) Then 'swap (comparison based on the index of value)
- tempArray = record1
- record1 = record2
- record2 = tempArray
- End If
- dgv_catalogue.Rows(counter).SetValues(record1) 'updates datagrid
- dgv_catalogue.Rows(counter + 1).SetValues(record2)
- Next
- Next
- End Sub
- Private Sub fillGrid()
- Dim encryption As New clsEncryption 'instantiates the encryption class
- lineCount = File.ReadAllLines("books.txt").Length 'make the linecount be the amount of records in the file
- Dim startupPath As String 'store the location the file is running
- startupPath = Application.StartupPath
- Dim record As String 'a record from the file
- Dim fields() As String 'the fields in a record
- dgv_catalogue.Rows.Clear() 'clear the datagrid
- Dim sr As New StreamReader("books.txt") 'a new stream reader of the books file
- While sr.Peek() >= 0 'while there are record in the file to be read
- record = encryption.decrypt(sr.ReadLine) 'decrypt the record
- fields = record.Split(",") 'split the record into fields
- Dim index = dgv_catalogue.Rows.Add 'the next row to add to
- dgv_catalogue.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_catalogue.RowHeadersVisible = False 'Removes the symbols by the rows
- dgv_catalogue.ClearSelection() 'Removes the blue selection box
- dgv_catalogue.Rows(lineCount - 1).Selected = True 'select the final record in the datagrid
- rowIndex = lineCount - 1 'show the currenty corrected row
- txt_bookName.Text = "Book Name" 'reset text boxes
- txt_author.Text = "Author"
- txt_bookID.Text = "BookID"
- End Sub
- Private Sub Catalogue_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'when form loads
- Dim encryption As New clsEncryption 'instantiate the encryption class
- Me.CenterToScreen() 'centre the form to the screen
- Dim startupPath As String 'store the location the file is running
- startupPath = Application.StartupPath
- txt_author.Text = "Author" 'reset the text boxes
- txt_bookName.Text = "Book Name"
- txt_search.Text = "Search by Book Name"
- If levelOfAccess = 3 Then
- Me.BackColor = Color.FromArgb(247, 220, 143) 'admins have yellow background
- End If
- If levelOfAccess = 2 Then
- Me.BackColor = Color.FromArgb(140, 227, 245) 'librarians have blue background
- End If
- If levelOfAccess = 1 Then
- Me.BackColor = Color.FromArgb(163, 255, 166) 'customers have green background
- btn_add.Visible = False 'Make the add function not accessible to customers
- btn_overwrite.Visible = False
- txt_bookName.Visible = False
- txt_author.Visible = False
- End If
- pbx_logo.ImageLocation = startupPath & "\PennardLibraryLogo.png" 'set up image locations
- pbx_back.ImageLocation = startupPath & "\Arrow.png"
- If Dir$(startupPath & "\books.txt") = "" Then 'if file doesnt exist
- Dim sw As New StreamWriter("books.txt", True) 'stream writer of the books file
- sw.WriteLine(encryption.encrypt("1,The Way of Kings,Brandon Sanderson")) 'Write a book in to be available no matter what
- sw.Close() 'close the stream writer
- MsgBox("A new file has been created") 'notify user
- End If
- If Dir(startupPath & "\tempBooks.txt") = "" Then 'if temp file doesnt exist
- Dim sw As New StreamWriter("tempBooks.txt", False) 'create file with a stream writer
- sw.Close() 'close stream writer
- End If
- fillGrid() 'fill the data 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 the main menu form
- MainMenu.Show() 'show the new form
- Me.Close() 'close the current form
- End Sub
- Private Sub txt_search_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_search.MouseClick
- If txt_search.Text = "Search by Book Name" Then 'if the current text is the start text
- txt_search.Text = "" 'clear the text box
- End If
- End Sub
- Private Sub txt_bookName_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_bookName.MouseClick
- If txt_bookName.Text = "Book Name" Then 'if the current text is the start text
- txt_bookName.Text = "" 'clear the text box
- End If
- End Sub
- Private Sub txt_author_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_author.MouseClick
- If txt_author.Text = "Author" Then 'if the current text is the start text
- txt_author.Text = "" 'clear the text box
- End If
- End Sub
- Private Sub txt_ID_MouseClick(sender As Object, e As MouseEventArgs) Handles txt_author.MouseClick
- If txt_bookID.Text = "BookID" Then 'if the current text is the start text
- txt_bookID.Text = "" 'clear the text box
- End If
- End Sub
- Private Sub btn_add_Click(sender As Object, e As EventArgs) Handles btn_add.Click
- Dim encryption As New clsEncryption 'insantiate the encryption class
- Dim validation As New clsValidation 'instantiate the clsValidation class
- Dim validated As Boolean = True 'is all validation met
- Dim record As String 'a record from the file
- Dim fields() As String 'the fields in the record
- Dim addBookID As Integer = 0 'the bookID to be added
- Dim addBookName As String 'the book name to be added
- Dim addAuthor As String 'the author to be added
- Dim sr As New StreamReader("books.txt") 'a stream reader of the book file
- While sr.Peek() >= 0 'while there is a record to be read
- record = encryption.decrypt(sr.ReadLine) 'decrypt the record
- fields = record.Split(",") 'split into fields
- If fields(0) >= addBookID Then 'if an ID is found with a higher ID
- addBookID = fields(0) + 1 'Makes sure the bookID being added is always 1 greater than the current largest stored
- End If
- End While
- sr.Close()
- If validation.characterCheck(txt_bookName.Text, ",") = False Or validation.characterCheck(txt_author.Text, ",") = False Then 'doesnt allow for commas to be entered
- validated = False 'validation isnt met
- MsgBox("Data cannot contain commas") 'notify user
- End If
- If validation.presenceCheck(txt_bookName.Text) = True And txt_bookName.Text <> "Book Name" Then 'if the data is valid
- addBookName = txt_bookName.Text 'set value of new book name
- Else
- validated = False 'validation isnt met
- End If
- If validation.presenceCheck(txt_author.Text) = True And txt_author.Text <> "Author" Then 'if the data is valid
- addAuthor = txt_author.Text 'set value of the new author
- Else
- validated = False 'validation isnt met
- End If
- If validated = True Then 'if validation is met
- Dim sw As New StreamWriter("books.txt", True) 'a stream writer of the book file
- sw.WriteLine(encryption.encrypt(addBookID & "," & addBookName & "," & addAuthor)) 'add a record to the file
- sw.Close() 'close the stream writer
- Else
- MsgBox("You need to fill out all fields") 'notify user that validation isnt met
- End If
- fillGrid() 'fill the grid
- 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 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("books.txt").Length 'the amount of records stored
- Dim found As Boolean = False 'has the record 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 record stored
- Dim sr As New StreamReader("books.txt") 'a stream reader of the book file
- If validation.presenceCheck(txt_search.Text) = True And txt_search.Text <> "Search by Book Name" Then 'if validation is met
- While sr.Peek() >= 0 'while there are records to be read
- recordCount = recordCount + 1 'count the amount of records stored
- record = encryption.decrypt(sr.ReadLine) 'decrypt the record
- fields = record.Split(",") 'split the record into fields
- If fields(1) = txt_search.Text Then 'if the name is found in the record
- dgv_catalogue.ClearSelection() 'unselect row
- dgv_catalogue.Rows(recordCount - 1).Selected = True 'make the found record be selected
- found = True 'record has been found
- ElseIf recordCount = lineCount And found = False Then 'if not found
- MsgBox("There is no book stored with that name") 'notify user
- End If
- End While
- Else
- MsgBox("You must fill out all fields") 'notify user they must fill all fields
- End If
- txt_search.Text = "Search by Book Name" 'reset textboxes
- End Sub
- Private Sub btn_first_Click(sender As Object, e As EventArgs) Handles btn_first.Click
- dgv_catalogue.ClearSelection()
- dgv_catalogue.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_catalogue.ClearSelection()
- If rowIndex = 0 Then
- MsgBox("You are already looking at the first record")
- Else
- rowIndex = rowIndex - 1
- dgv_catalogue.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_catalogue.ClearSelection()
- If rowIndex = lineCount - 1 Then
- MsgBox("You are already looking at the last record")
- Else
- rowIndex = rowIndex + 1
- dgv_catalogue.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_catalogue.ClearSelection()
- dgv_catalogue.Rows(lineCount - 1).Selected = True 'go to the last record stored
- rowIndex = lineCount - 1
- End Sub
- Private Sub btn_overwrite_Click(sender As Object, e As EventArgs) Handles btn_overwrite.Click
- Dim swTemp As New StreamWriter("tempBooks.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 the validation class
- Dim encryption As New clsEncryption 'instantiate the encryption class
- Dim editBookName As String 'the bookName to overwrite with
- Dim editAuthor As String 'the author to overwrite with
- 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 'hasnt been found yet
- If validation.presenceCheck(txt_bookName.Text) = True And txt_bookName.Text <> "Book Name" Then 'makes sure that the data to overwrite with makes sense
- editBookName = txt_bookName.Text 'set value
- Else
- validated = False 'not validated
- MsgBox("Must have a value for the new book name") 'notify user
- End If
- If validation.presenceCheck(txt_author.Text) = True And txt_author.Text <> "Author" Then 'validated
- editAuthor = txt_author.Text 'set values
- Else
- validated = False 'not validated
- MsgBox("Must have a value for the new author") 'notify user
- End If
- If validation.presenceCheck(txt_bookID.Text) = True And txt_bookID.Text <> "BookID" Then 'Make sure and ID is entered
- searchID = txt_bookID.Text 'set values
- Else
- IDValidated = False 'not validated
- 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("books.txt") 'stream reader of books file
- Dim swTemp2 As New StreamWriter("tempBooks.txt", True) 'streamwriter of temp book file
- While sr.Peek() >= 0
- record = encryption.decrypt(sr.ReadLine) 'read line unencrypted
- fields = record.Split(",") 'split into fields
- If fields(0) <> searchID Then 'if the ID isnt the searched ID
- swTemp2.WriteLine(encryption.encrypt(record)) 'Write old line into temp file
- Else
- swTemp2.WriteLine(encryption.encrypt(searchID & "," & editBookName & "," & editAuthor)) '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 the ID was found
- Dim srTemp As New StreamReader("tempBooks.txt") 'stream reader of the temp file
- Dim sw As New StreamWriter("books.txt", False) 'will overwrite previous file
- sw.Close() 'close stream writer
- Dim sw1 As New StreamWriter("books.txt", True) 'writes new 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 reader
- sw1.Close() 'close stream writer
- Else
- MsgBox("No record found with that ID") 'notify user that the ID wasnt found
- End If
- fillGrid() 'fill the datagrid
- End Sub
- Private Sub pbx_back_MouseHover(sender As Object, e As EventArgs) Handles pbx_back.MouseHover
- Dim startupPath As String 'stores the location the program is running
- startupPath = Application.StartupPath
- pbx_back.ImageLocation = startupPath & "\ArrowRollover.png" 'change to rollover
- End Sub
- Private Sub pbx_back_MouseLeave(sender As Object, e As EventArgs) Handles pbx_back.MouseLeave
- Dim startupPath As String 'stores the location the program is running
- startupPath = Application.StartupPath
- pbx_back.ImageLocation = startupPath & "\Arrow.png" 'change to normal
- End Sub
- Private Sub rbt_sortBookID_Click(sender As Object, e As EventArgs) Handles rbt_sortBookID.Click
- bubbleSort(0) 'sort by ID
- End Sub
- Private Sub rbt_bookName_CheckedChanged(sender As Object, e As EventArgs) Handles rbt_bookName.CheckedChanged
- bubbleSort(1) 'sort by Name
- End Sub
- End Class
Add Comment
Please, Sign In to add comment