Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. Imports System.IO
  2. Module Module1
  3. Sub Main()
  4. Dim choice As String
  5. Do 'Validates choice so the user can only choose 1 or 2 repeating if neither are chose
  6. Console.Write("1. Sign in " & vbCrLf & "2. Register " & vbCrLf & "Choose: ") : choice = Console.ReadLine 'vbcrlf puts it on a new line
  7. Loop While choice <> "1" And choice <> "2"
  8. If choice = "1" Then
  9. If signIn() = True Then 'Calls the function sign in
  10. Console.WriteLine("Welcome!")
  11. End If
  12. Else
  13. Register() 'Calls the sub procedure register
  14. Console.WriteLine("Registered!")
  15. End If
  16. Console.ReadLine()
  17. End Sub
  18.  
  19. Function signIn()
  20. Dim username, password, readPass As String 'Read in password from a individual file
  21. Dim finish As Boolean = False 'End states for each loop
  22. Do While finish = False
  23. Do While readPass = Nothing 'Loops until username is acceptable
  24. Console.Write("Enter your username: ") : username = Console.ReadLine
  25. readPass = ReadFrom(username) 'Uses function to read in the file with the username as its path
  26. If readPass = Nothing Then
  27. Console.WriteLine("Please enter a valid username")
  28. End If
  29. Loop
  30. Console.Write("Enter your password: ") : password = Console.ReadLine
  31. If readPass = password Then 'Checks if password in file equals users inputted password
  32. finish = True
  33. Else
  34. finish = False
  35. Console.WriteLine("Please enter a valid password")
  36. End If
  37. Loop
  38. Return finish
  39. End Function
  40.  
  41. Sub Register()
  42. Dim Iage As Integer
  43. Dim name, age, username, password As String
  44. Do
  45. Console.Write("Please enter your first name: ") : name = Console.ReadLine
  46. Loop While name.Length < 4 'Loops until there name is more than 4 characters
  47. Try
  48. Console.Write("please enter your age: ") : Iage = Console.ReadLine 'Gets there age as Iage so it can be checked if its an integer if it doesnt work it calls sub main and program restarts
  49. Catch ex As Exception
  50. Console.WriteLine("Not an age!") : Console.ReadLine()
  51. Main()
  52. End Try
  53. age = Iage 'Sets iage to a string
  54. username = name.Substring(0, 3) & age 'gets first 3 characters of name and adds age
  55. Console.WriteLine("Your username is: " & username)
  56. Console.Write("Please input a password: ") : password = Console.ReadLine
  57. Create(username) 'Calls the create procedure
  58. WriteTo(username, password) 'calls the writeto procedure
  59. End Sub
  60.  
  61. Sub WriteTo(ByRef FilePath As String, Message As String)
  62. Using Writet As New StreamWriter(FilePath) 'Writes to file in bin with username as title
  63. Writet.WriteLine(Message)
  64. End Using
  65. End Sub
  66. Function ReadFrom(ByVal FilePath As String)
  67. Dim message As String
  68. Try
  69. Using ReadFr As New StreamReader(FilePath) 'trys to read the message from the file
  70. message = ReadFr.ReadLine
  71. End Using
  72. Catch ex As Exception
  73. message = Nothing ' if there is no message an error will happen so it sets message = to nothing and allows the program to carry on
  74. End Try
  75. Return message 'returns the message
  76. End Function
  77. Sub Create(ByRef username As String) 'Creates text document in bin with username as title
  78. Dim filepath As String = username
  79. If Not System.IO.File.Exists(filepath) Then 'Checks if document already exists
  80. System.IO.File.Create(filepath).Dispose() 'Creates a new file
  81. End If
  82. End Sub
  83. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement