Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Module Module1
- Structure Person
- Dim Name As String
- Dim DoB As Date
- End Structure
- Dim sFileName As String = "Z:/" ' the name and file path of the saved file
- Public Sub Main()
- 'Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)) ' Users area xD
- Dim localDir As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
- localDir = localDir & "/People.txt"
- sFileName = localDir
- Dim tempDir As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "/temp.tmp"
- ' create a new list to store our structure data type 'Person'
- Dim lstPeople As New List(Of Person)
- Dim chMenuOpt As Char
- Do
- DisplayMenu() ' call sub and display menu options
- Console.WriteLine("There are currently " & lstPeople.Count & " people entered.")
- chMenuOpt = Console.ReadKey.KeyChar ' get keyboard character and store in chMenuOpt
- Select Case chMenuOpt
- Case "1"
- lstPeople.AddRange(GetPeople) ' get more people and add to lstPeople
- Case "2"
- SavePeople(lstPeople)
- Case "3"
- DisplayPeople(lstPeople)
- Case "6"
- Exit Do
- Case "4"
- SearchPeople(lstPeople)
- Case "5"
- LoadPeople()
- Case "7"
- IO.File.Create(tempDir).Dispose()
- Dim s As New IO.StreamWriter(tempDir, True)
- s.WriteLine(lstPeople.Count)
- s.Close()
- lstPeople.AddRange(TestCrapThing)
- Case "8"
- MakeFile()
- Case "9"
- ChangeLocation()
- Case "0"
- 'lstPeople.Clear()
- CLearPeople(lstPeople)
- End Select
- Loop
- ' user chose to exit program
- End Sub
- Sub CLearPeople(lst As List(Of Person))
- Console.Clear()
- Console.WriteLine("All loaded people have been cleared!")
- lst.Clear()
- Console.WriteLine("Press any key to continue . . . ")
- Console.ReadKey()
- End Sub
- Sub ChangeLocation()
- Console.Clear()
- Console.Write("The new file directory will be: ")
- sFileName = Console.ReadLine
- Console.Clear()
- Console.WriteLine("The file directory has changed to: " & sFileName)
- Console.WriteLine("Please make sure to make the file in the menu after changing the directory.")
- Console.WriteLine("Press any key to continue . . . ")
- Console.ReadKey()
- End Sub
- Sub MakeFile()
- Console.Clear()
- Console.WriteLine("Do you really want to continue [Yes/No]")
- Dim innnn As Char = Console.ReadKey.KeyChar
- If LCase(innnn) = "y" Then
- Console.Clear()
- IO.File.Create(sFileName).Dispose()
- Console.WriteLine("The file renewed!")
- Console.WriteLine("Press any key to continue . . . ")
- Console.ReadKey()
- Else
- End If
- End Sub
- Sub DisplayMenu()
- Console.Clear()
- Console.WriteLine("Please pick an option. There are loads to choose from xD")
- Console.WriteLine()
- Console.WriteLine("1) Add people")
- Console.WriteLine("2) Save people to file")
- Console.WriteLine("3) Display people")
- Console.WriteLine("4) Search in file")
- Console.WriteLine("5) Show file")
- Console.WriteLine("6) Quit")
- Console.WriteLine("7) Load from file")
- Console.WriteLine("8) Make file / Clear file")
- Console.WriteLine("9) Change file location")
- Console.WriteLine("0) Unload people")
- Console.WriteLine()
- End Sub
- Sub LoadPeople()
- Console.Clear()
- Dim s As New IO.StreamReader(sFileName, True)
- Do Until s.EndOfStream = True
- Dim tempp As String = s.ReadLine
- Console.WriteLine(tempp)
- Loop
- s.Close()
- Console.ReadKey()
- Console.Clear()
- End Sub
- Function TestCrapThing() As List(Of Person)
- Console.Clear()
- Dim count As Decimal = 0
- Dim d As New IO.StreamReader(sFileName, True)
- Do Until d.EndOfStream = True
- d.ReadLine()
- count = count + 1
- Loop
- d.Close()
- Console.Clear()
- Dim s As New IO.StreamReader(sFileName, True)
- ' builds a list of people from the user. The user types "Stop" to end the list
- ' when function ends, the list is returned
- Dim tmpPerson As Person ' Used while entering data, added at end
- Dim tmpDate As String ' stores user's DOB. This can be checked to see if valid, before storing
- Dim tmpPeopleList As New List(Of Person) ' store added names in temporary list
- Console.Clear()
- ' the loop goes around an infinite number of times. Only exits when user types 'Stop'
- Console.WriteLine("In progress...")
- Console.WriteLine("There are " & count & " lines.")
- Console.WriteLine("")
- Do Until s.EndOfStream = True
- ' ############
- ' # Get Name #
- ' ############
- Dim countt As Decimal = 0
- Dim tempstuff As String = s.ReadLine
- Dim tempsplit As String() = tempstuff.Split(New Char() {"|"})
- Dim part As String = tempsplit(1)
- For Each part In tempsplit
- countt = countt + 1
- tmpPerson.Name = tempsplit(0)
- tmpPerson.DoB = CDate(tempsplit(1))
- If countt = 2 Then
- Console.WriteLine("Added: " & tempsplit(0))
- Console.WriteLine("with date of: " & tempsplit(1))
- tmpPeopleList.Add(tmpPerson)
- countt = 0
- End If
- Next
- Loop ' loop back around to get next person
- Dim localDir As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
- Dim tmp As New IO.StreamReader(localDir & "/temp.tmp", True)
- If tmp.ReadLine = "0" Then ' If no people are loaded then it won't ask to clear it.
- Console.WriteLine("")
- Console.WriteLine("Imported new people!")
- Console.WriteLine("Press any key to continue . . . ")
- Console.ReadKey()
- tmp.Close()
- s.Close()
- Return tmpPeopleList
- Else
- Console.WriteLine("")
- Console.WriteLine("Do you want to clear all the existing people loaded into the program? [Yes/No]")
- Dim inputty As Char = Console.ReadKey.KeyChar
- If inputty = "y" Then
- 'need finish !!!!!!!!!!!!THIS IS WHERE I WANT TO CLEAR THE LIST !!!!!!
- Return tmpPeopleList ' send temporary list back for processing
- Console.WriteLine("")
- Console.WriteLine("Cleared people and imported new people!")
- Console.WriteLine("Press any key to continue . . . ")
- Else
- tmp.Close()
- s.Close()
- Return tmpPeopleList ' send temporary list back for processing
- Console.WriteLine("")
- Console.WriteLine("imported new people!")
- Console.WriteLine("Press any key to continue . . . ")
- End If
- End If
- Console.ReadKey()
- tmp.Close()
- s.Close()
- End Function
- Function GetPeople() As List(Of Person)
- ' builds a list of people from the user. The user types "Stop" to end the list
- ' when function ends, the list is returned
- Dim tmpPerson As Person ' Used while entering data, added at end
- Dim tmpDate As String ' stores user's DOB. This can be checked to see if valid, before storing
- Dim tmpPeopleList As New List(Of Person) ' store added names in temporary list
- Console.Clear()
- ' the loop goes around an infinite number of times. Only exits when user types 'Stop'
- Do
- ' ############
- ' # Get Name #
- ' ############
- Console.WriteLine("Please enter the person's name. Type 'Stop' or 'Quit' to finish entering names.")
- tmpPerson.Name = Console.ReadLine ' get name
- If LCase(tmpPerson.Name) = "stop" Or LCase(tmpPerson.Name) = "quit" Then
- Exit Do ' immediately exit, do not add to the list
- End If
- ' ###########
- ' # Get DOB #
- ' ###########
- Do ' loop infinitely until user gives a valid date.
- Console.WriteLine("Please enter " & tmpPerson.Name & "'s DOB.")
- tmpDate = Console.ReadLine ' read into temporary string to validate date
- If IsDate(tmpDate) = False Then ' user entered an invalid date
- Console.WriteLine("Sorry, that was an invalid date. Please try again.")
- Else
- tmpPerson.DoB = CDate(tmpDate) ' convert to a date and store
- Exit Do ' quit infinite loop as we have a valid date
- End If
- Loop
- tmpPeopleList.Add(tmpPerson) ' remember, a function becomes the return value.
- Loop ' loop back around to get next person
- Return tmpPeopleList ' send temporary list back for processing
- End Function
- Sub SavePeople(lst As List(Of Person))
- ' sub expects a list as a parameter
- ' it then saves all people in the list to a text file
- Dim strFile As New IO.StreamWriter(sFileName, True)
- Console.Clear()
- If lst.Count = 0 Then ' there are no people in the list
- ' no people to display
- Console.WriteLine("Please enter some people first. Press [enter] to continue.")
- Console.ReadLine()
- Exit Sub ' leave subroutine as cannot continue with no people to display
- End If
- For Each PersonElement As Person In lst
- ' iterate for every Person in the list 'lst'
- strFile.WriteLine(PersonElement.Name & "|" & PersonElement.DoB)
- Next
- strFile.Close()
- Console.WriteLine("File saved successfully. Press [enter] to continue")
- Console.ReadLine()
- End Sub
- Sub DisplayPeople(lst As List(Of Person))
- ' sub expects a list as a parameter
- ' it then displays all people in the list to the console screen
- Console.Clear()
- Dim time As DateTime = DateTime.Now
- Dim format As String = "ddd d MMM yyyy"
- Dim format2 As String = "yyyy"
- Dim snao As Date = Now
- If lst.Count = 0 Then ' there are no people in the list
- ' no people to display
- Console.WriteLine("Please enter some people first. Press [enter] to continue.")
- Console.ReadLine()
- Exit Sub ' leave subroutine as cannot continue with no people to display
- End If
- For Each PersonElement As Person In lst
- ' iterate for every Person in the list 'lst'
- Console.WriteLine(PersonElement.Name & " was born " & PersonElement.DoB.ToString(format) & " and is " & PersonElement.DoB.ToString(format2) & " years old. It is " & snao.DayOfYear - PersonElement.DoB.DayOfYear & " days till their birthday.")
- Next
- Console.WriteLine()
- Console.WriteLine("Press [enter] to return")
- Console.ReadLine()
- End Sub
- Sub SearchPeople(lst As List(Of Person))
- Console.Clear()
- Dim inputt As Char
- Console.WriteLine("Please pick an option.")
- Console.WriteLine()
- Console.WriteLine("1) Search in names")
- Console.WriteLine("2) Search in DoB")
- inputt = Console.ReadKey.KeyChar
- Select Case inputt
- Case "1"
- Console.Clear()
- Console.WriteLine("Search:")
- Dim Lookfor As String = Console.ReadLine
- For Each PersonElement As Person In lst
- If PersonElement.Name.Contains(Lookfor) Then
- Console.WriteLine("We found this for " & Lookfor & ":")
- Console.WriteLine(PersonElement.Name)
- Else
- End If
- Console.WriteLine("No matching string has been found.")
- Next
- Console.WriteLine("Press any key to continue . . . ")
- Console.ReadKey()
- Case "2"
- Console.Clear()
- Console.WriteLine("Search:")
- Dim Lookfor As String = Console.ReadLine
- Console.Clear()
- For Each PersonElement As Person In lst
- Dim temps As String = PersonElement.DoB
- If temps.Contains(Lookfor) Then
- Console.WriteLine("We found this for " & Lookfor & ":")
- Console.WriteLine(PersonElement.DoB)
- Else
- End If
- Next
- Console.WriteLine("No matching string has been found.")
- Console.WriteLine("Press any key to continue . . . ")
- Console.ReadKey()
- Case Else
- End Select
- End Sub
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement