我想从文本文件输出特定行,但我不知道如何

时间:2017-10-30 10:37:56

标签: vb.net visual-studio file oop

所以基本上我正在制作一本跟踪新书,查看标题和作者的图书馆书籍记录。现在,我正处于按字母顺序查看所有标题和作者的阶段,我制作了它们存储在文本文件中。我的老师建议我使用冒泡排序按字母排序,但现在我需要更深入的解释。请帮助一个人谢谢:D

哦,文本文件是这样的(除了最后一个使用& vbcrlf之外没有空格):

标题

作者

发布日期

页次

ISBN

Sub Main()
    'Making a book using the attributes and methods

    Dim title As String = ""
    Dim datepublished As Date = #01/01/0001#    '   date is in month/day/year
    Dim pagenum As Integer = 0
    Dim isbn As String = ""
    Dim author As String = ""
    Dim amountbooks As Integer = 0
    Dim choice As String = ""


    'Declaring an object variable 'filename' which shows the file name (bookrecords.txt) and the path given
    Dim filename As String = "C:\Users\Local_PC\Desktop\Try_oop_book\bookrecords.txt"


    'Making a menu for the client
    ' A While loop for the menu that checks the user's input
    While choice <> "1" AndAlso choice <> "2" AndAlso choice <> "3" AndAlso choice <> "4"
        Console.Clear()
        Console.WriteLine("----------LIBRARY MENU----------")
        Console.WriteLine("[ 1 ]" & "Add New Book(s)")
        Console.WriteLine("[ 2 ]" & "View all Book Titles")
        Console.WriteLine("[ 3 ]" & "View all Authors")
        Console.WriteLine("[ 4 ]" & "Exit")
        Console.WriteLine(" ")
        Console.Write("Pick a Menu Number: ")
        choice = Console.ReadLine()
        If choice <> "1" And choice <> "2" And choice <> "3" And choice <> "4" Then
            Console.WriteLine("Please type in a valid input next time, press enter to retry again.")
            Console.ReadLine()

        Else
            'Using choice, it goes to check which menu the user typed in.
            If choice = "1" Then
                'The user has chosen 1 which is to add new book(s) to the menu system.
                'Setting the title of the book using mybook.setTitle
                Console.WriteLine("How many books do you want to add?")
                amountbooks = Console.ReadLine()

                Dim bookarr(amountbooks) As Book    'This will initialise after amountbooks has been entered. Prevents from getting invalid index number of 0.

                For x = 1 To amountbooks '  This loop will go over how many amount of books the user wants to add in to.
                    bookarr(x) = New Book()

                    Console.WriteLine("What is the title of the book?")
                    title = Console.ReadLine()      'This gives the value to store inside the variable 'title'
                    bookarr(x).setTitle(title)      'This line will set that 'title' into array bookarr

                    Console.WriteLine("Who is the author of the book?")
                    author = Console.ReadLine()
                    bookarr(x).setAuthor(author)

                    Console.WriteLine("When is the book published?")
                    datepublished = Console.ReadLine()
                    bookarr(x).setDatePublished(datepublished)

                    Console.WriteLine("How many page numbers are there?")
                    pagenum = Console.ReadLine()
                    bookarr(x).setPageNum(pagenum)

                    Console.WriteLine("What is the ISBN(code) of the book?")
                    isbn = Console.ReadLine()
                    bookarr(x).setISBN(isbn)

                    Console.Clear() 'clears everything after it has been written into a file

                    If System.IO.File.Exists(filename) = True Then
                        '   Setting up an object variable for the streamwriter (which is the path folder and its name).
                        Dim objwriter As New System.IO.StreamWriter(filename, True) ' Has been set to true because the file exists and this will append(ADD) for the next str.
                        '   if a file doesn't exists, it should be false which will ensure that a text file is created first.

                        objwriter.WriteLine(bookarr(x).getTitle & vbCrLf & bookarr(x).getAuthor & vbCrLf & bookarr(x).getDatePublished & vbCrLf & bookarr(x).getPageNum & vbCrLf & bookarr(x).getISBN & vbCrLf)
                        objwriter.Close()   '   Closes the pathway to filename.
                        MsgBox("TEXT HAS BEEN SUCCESSFULLY REGISTERED")

                    Else

                        MsgBox("FILE DOES NOT EXIST")

                    End If
                Next x

            Else

                If choice = "2" Then    'View all book titles
                    Dim readall As String = ""
                    Dim readtitle As String = ""

                    If System.IO.File.Exists(filename) = True Then
                        Dim objreader As New System.IO.StreamReader(filename)


                        readtitle = objreader.ReadLine
                        objreader.Close()   'Closes the pathway to filename



                        Console.WriteLine(readtitle)  'Outputs the all book title in alphabetical
                    Else

                        MsgBox("FILE DOES NOT EXIST")

                    End If
                    Console.ReadLine()
                Else

                    If choice = "3" Then
                        Dim readauthor As String
                        Dim amountauthor As Integer = 0
                        Dim objreader As New System.IO.StreamReader(filename)

                    Else
                        Environment.Exit(0)     'idk I found this online :P but it works for exiting the console
                    End If
                    Console.ReadLine()
                End If
            End If
        End If
    End While


End Sub

0 个答案:

没有答案