Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 13.45 KB | None | 0 0
  1. Module Module1
  2.     Structure Person
  3.         Dim Name As String
  4.         Dim DoB As Date
  5.     End Structure
  6.  
  7.  
  8.  
  9.     Dim sFileName As String = "Z:/" ' the name and file path of the saved file
  10.  
  11.     Public Sub Main()
  12.  
  13.         'Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)) ' Users area xD
  14.         Dim localDir As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
  15.         localDir = localDir & "/People.txt"
  16.         sFileName = localDir
  17.         Dim tempDir As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "/temp.tmp"
  18.         ' create a new list to store our structure data type 'Person'
  19.         Dim lstPeople As New List(Of Person)
  20.         Dim chMenuOpt As Char
  21.         Do
  22.             DisplayMenu() ' call sub and display menu options
  23.             Console.WriteLine("There are currently " & lstPeople.Count & " people entered.")
  24.  
  25.             chMenuOpt = Console.ReadKey.KeyChar ' get keyboard character and store in chMenuOpt
  26.  
  27.             Select Case chMenuOpt
  28.                 Case "1"
  29.                     lstPeople.AddRange(GetPeople) ' get more people and add to lstPeople
  30.                 Case "2"
  31.                     SavePeople(lstPeople)
  32.                 Case "3"
  33.                     DisplayPeople(lstPeople)
  34.                 Case "6"
  35.                     Exit Do
  36.                 Case "4"
  37.                     SearchPeople(lstPeople)
  38.                 Case "5"
  39.                     LoadPeople()
  40.                 Case "7"
  41.  
  42.                     IO.File.Create(tempDir).Dispose()
  43.  
  44.                     Dim s As New IO.StreamWriter(tempDir, True)
  45.                     s.WriteLine(lstPeople.Count)
  46.                     s.Close()
  47.                     lstPeople.AddRange(TestCrapThing)
  48.                 Case "8"
  49.                     MakeFile()
  50.                 Case "9"
  51.                     ChangeLocation()
  52.                 Case "0"
  53.                     'lstPeople.Clear()
  54.                     CLearPeople(lstPeople)
  55.             End Select
  56.         Loop
  57.         ' user chose to exit program
  58.     End Sub
  59.     Sub CLearPeople(lst As List(Of Person))
  60.         Console.Clear()
  61.         Console.WriteLine("All loaded people have been cleared!")
  62.         lst.Clear()
  63.         Console.WriteLine("Press any key to continue . . . ")
  64.         Console.ReadKey()
  65.     End Sub
  66.     Sub ChangeLocation()
  67.         Console.Clear()
  68.         Console.Write("The new file directory will be: ")
  69.         sFileName = Console.ReadLine
  70.         Console.Clear()
  71.         Console.WriteLine("The file directory has changed to: " & sFileName)
  72.         Console.WriteLine("Please make sure to make the file in the menu after changing the directory.")
  73.         Console.WriteLine("Press any key to continue . . . ")
  74.         Console.ReadKey()
  75.     End Sub
  76.     Sub MakeFile()
  77.         Console.Clear()
  78.         Console.WriteLine("Do you really want to continue [Yes/No]")
  79.         Dim innnn As Char = Console.ReadKey.KeyChar
  80.         If LCase(innnn) = "y" Then
  81.             Console.Clear()
  82.             IO.File.Create(sFileName).Dispose()
  83.             Console.WriteLine("The file renewed!")
  84.             Console.WriteLine("Press any key to continue . . . ")
  85.             Console.ReadKey()
  86.         Else
  87.  
  88.         End If
  89.     End Sub
  90.     Sub DisplayMenu()
  91.         Console.Clear()
  92.         Console.WriteLine("Please pick an option. There are loads to choose from xD")
  93.         Console.WriteLine()
  94.         Console.WriteLine("1) Add people")
  95.         Console.WriteLine("2) Save people to file")
  96.         Console.WriteLine("3) Display people")
  97.         Console.WriteLine("4) Search in file")
  98.         Console.WriteLine("5) Show file")
  99.         Console.WriteLine("6) Quit")
  100.         Console.WriteLine("7) Load from file")
  101.         Console.WriteLine("8) Make file / Clear file")
  102.         Console.WriteLine("9) Change file location")
  103.         Console.WriteLine("0) Unload people")
  104.         Console.WriteLine()
  105.     End Sub
  106.     Sub LoadPeople()
  107.         Console.Clear()
  108.         Dim s As New IO.StreamReader(sFileName, True)
  109.         Do Until s.EndOfStream = True
  110.             Dim tempp As String = s.ReadLine
  111.  
  112.             Console.WriteLine(tempp)
  113.         Loop
  114.         s.Close()
  115.         Console.ReadKey()
  116.         Console.Clear()
  117.  
  118.     End Sub
  119.     Function TestCrapThing() As List(Of Person)
  120.         Console.Clear()
  121.         Dim count As Decimal = 0
  122.         Dim d As New IO.StreamReader(sFileName, True)
  123.         Do Until d.EndOfStream = True
  124.             d.ReadLine()
  125.             count = count + 1
  126.         Loop
  127.         d.Close()
  128.         Console.Clear()
  129.         Dim s As New IO.StreamReader(sFileName, True)
  130.         ' builds a list of people from the user.  The user types "Stop" to end the list
  131.         ' when function ends, the list is returned
  132.  
  133.         Dim tmpPerson As Person ' Used while entering data, added at end
  134.         Dim tmpDate As String ' stores user's DOB.  This can be checked to see if valid, before storing
  135.         Dim tmpPeopleList As New List(Of Person) ' store added names in temporary list
  136.         Console.Clear()
  137.         ' the loop goes around an infinite number of times.  Only exits when user types 'Stop'
  138.         Console.WriteLine("In progress...")
  139.         Console.WriteLine("There are " & count & " lines.")
  140.         Console.WriteLine("")
  141.         Do Until s.EndOfStream = True
  142.             ' ############
  143.             ' # Get Name #
  144.             ' ############
  145.             Dim countt As Decimal = 0
  146.             Dim tempstuff As String = s.ReadLine
  147.             Dim tempsplit As String() = tempstuff.Split(New Char() {"|"})
  148.             Dim part As String = tempsplit(1)
  149.             For Each part In tempsplit
  150.                 countt = countt + 1
  151.                 tmpPerson.Name = tempsplit(0)
  152.                 tmpPerson.DoB = CDate(tempsplit(1))
  153.                 If countt = 2 Then
  154.                     Console.WriteLine("Added: " & tempsplit(0))
  155.                     Console.WriteLine("with date of: " & tempsplit(1))
  156.                     tmpPeopleList.Add(tmpPerson)
  157.                     countt = 0
  158.                 End If
  159.             Next
  160.         Loop ' loop back around to get next person
  161.         Dim localDir As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
  162.         Dim tmp As New IO.StreamReader(localDir & "/temp.tmp", True)
  163.         If tmp.ReadLine = "0" Then ' If no people are loaded then it won't ask to clear it.
  164.             Console.WriteLine("")
  165.             Console.WriteLine("Imported new people!")
  166.             Console.WriteLine("Press any key to continue . . . ")
  167.             Console.ReadKey()
  168.             tmp.Close()
  169.             s.Close()
  170.             Return tmpPeopleList
  171.         Else
  172.             Console.WriteLine("")
  173.             Console.WriteLine("Do you want to clear all the existing people loaded into the program? [Yes/No]")
  174.             Dim inputty As Char = Console.ReadKey.KeyChar
  175.             If inputty = "y" Then
  176.                 'need finish !!!!!!!!!!!!THIS IS WHERE I WANT TO CLEAR THE LIST !!!!!!
  177.                 Return tmpPeopleList ' send temporary list back for processing
  178.                 Console.WriteLine("")
  179.                 Console.WriteLine("Cleared people and imported new people!")
  180.                 Console.WriteLine("Press any key to continue . . . ")
  181.             Else
  182.                 tmp.Close()
  183.                 s.Close()
  184.                 Return tmpPeopleList ' send temporary list back for processing
  185.                 Console.WriteLine("")
  186.                 Console.WriteLine("imported new people!")
  187.                 Console.WriteLine("Press any key to continue . . . ")
  188.             End If
  189.         End If
  190.         Console.ReadKey()
  191.         tmp.Close()
  192.         s.Close()
  193.     End Function
  194.     Function GetPeople() As List(Of Person)
  195.         ' builds a list of people from the user.  The user types "Stop" to end the list
  196.         ' when function ends, the list is returned
  197.  
  198.         Dim tmpPerson As Person ' Used while entering data, added at end
  199.         Dim tmpDate As String ' stores user's DOB.  This can be checked to see if valid, before storing
  200.         Dim tmpPeopleList As New List(Of Person) ' store added names in temporary list
  201.         Console.Clear()
  202.         ' the loop goes around an infinite number of times.  Only exits when user types 'Stop'
  203.         Do
  204.             ' ############
  205.             ' # Get Name #
  206.             ' ############
  207.             Console.WriteLine("Please enter the person's name.  Type 'Stop' or 'Quit' to finish entering names.")
  208.             tmpPerson.Name = Console.ReadLine ' get name
  209.             If LCase(tmpPerson.Name) = "stop" Or LCase(tmpPerson.Name) = "quit" Then
  210.                 Exit Do ' immediately exit, do not add to the list
  211.             End If
  212.  
  213.             ' ###########
  214.             ' # Get DOB #
  215.             ' ###########
  216.             Do ' loop infinitely until user gives a valid date.
  217.                 Console.WriteLine("Please enter " & tmpPerson.Name & "'s DOB.")
  218.                 tmpDate = Console.ReadLine ' read into temporary string to validate date
  219.  
  220.                 If IsDate(tmpDate) = False Then ' user entered an invalid date
  221.                     Console.WriteLine("Sorry, that was an invalid date.  Please try again.")
  222.                 Else
  223.                     tmpPerson.DoB = CDate(tmpDate) ' convert to a date and store
  224.                     Exit Do ' quit infinite loop as we have a valid date
  225.                 End If
  226.             Loop
  227.  
  228.             tmpPeopleList.Add(tmpPerson) ' remember, a function becomes the return value.
  229.         Loop ' loop back around to get next person
  230.         Return tmpPeopleList ' send temporary list back for processing
  231.     End Function
  232.  
  233.     Sub SavePeople(lst As List(Of Person))
  234.         ' sub expects a list as a parameter
  235.         ' it then saves all people in the list to a text file
  236.         Dim strFile As New IO.StreamWriter(sFileName, True)
  237.         Console.Clear()
  238.         If lst.Count = 0 Then ' there are no people in the list
  239.             ' no people to display
  240.             Console.WriteLine("Please enter some people first.  Press [enter] to continue.")
  241.             Console.ReadLine()
  242.             Exit Sub ' leave subroutine as cannot continue with no people to display
  243.         End If
  244.  
  245.         For Each PersonElement As Person In lst
  246.             ' iterate for every Person in the list 'lst'
  247.             strFile.WriteLine(PersonElement.Name & "|" & PersonElement.DoB)
  248.         Next
  249.         strFile.Close()
  250.         Console.WriteLine("File saved successfully.  Press [enter] to continue")
  251.         Console.ReadLine()
  252.  
  253.     End Sub
  254.  
  255.     Sub DisplayPeople(lst As List(Of Person))
  256.         ' sub expects a list as a parameter
  257.         ' it then displays all people in the list to the console screen
  258.  
  259.         Console.Clear()
  260.         Dim time As DateTime = DateTime.Now
  261.         Dim format As String = "ddd d MMM yyyy"
  262.         Dim format2 As String = "yyyy"
  263.         Dim snao As Date = Now
  264.         If lst.Count = 0 Then ' there are no people in the list
  265.             ' no people to display
  266.             Console.WriteLine("Please enter some people first.  Press [enter] to continue.")
  267.             Console.ReadLine()
  268.             Exit Sub ' leave subroutine as cannot continue with no people to display
  269.         End If
  270.  
  271.         For Each PersonElement As Person In lst
  272.             ' iterate for every Person in the list 'lst'
  273.             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.")
  274.         Next
  275.         Console.WriteLine()
  276.         Console.WriteLine("Press [enter] to return")
  277.         Console.ReadLine()
  278.     End Sub
  279.     Sub SearchPeople(lst As List(Of Person))
  280.  
  281.         Console.Clear()
  282.         Dim inputt As Char
  283.         Console.WriteLine("Please pick an option.")
  284.         Console.WriteLine()
  285.         Console.WriteLine("1) Search in names")
  286.         Console.WriteLine("2) Search in DoB")
  287.         inputt = Console.ReadKey.KeyChar
  288.         Select Case inputt
  289.             Case "1"
  290.                 Console.Clear()
  291.                 Console.WriteLine("Search:")
  292.                 Dim Lookfor As String = Console.ReadLine
  293.                 For Each PersonElement As Person In lst
  294.                     If PersonElement.Name.Contains(Lookfor) Then
  295.                         Console.WriteLine("We found this for " & Lookfor & ":")
  296.                         Console.WriteLine(PersonElement.Name)
  297.                     Else
  298.  
  299.                     End If
  300.                     Console.WriteLine("No matching string has been found.")
  301.                 Next
  302.                 Console.WriteLine("Press any key to continue . . . ")
  303.                 Console.ReadKey()
  304.             Case "2"
  305.                 Console.Clear()
  306.                 Console.WriteLine("Search:")
  307.                 Dim Lookfor As String = Console.ReadLine
  308.                 Console.Clear()
  309.                 For Each PersonElement As Person In lst
  310.                     Dim temps As String = PersonElement.DoB
  311.                     If temps.Contains(Lookfor) Then
  312.                         Console.WriteLine("We found this for " & Lookfor & ":")
  313.                         Console.WriteLine(PersonElement.DoB)
  314.                     Else
  315.  
  316.                     End If
  317.  
  318.                 Next
  319.                 Console.WriteLine("No matching string has been found.")
  320.                 Console.WriteLine("Press any key to continue . . . ")
  321.                 Console.ReadKey()
  322.             Case Else
  323.         End Select
  324.  
  325.     End Sub
  326. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement