Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Module Module1
- Sub Main()
- Dim choice As String
- Do 'Validates choice so the user can only choose 1 or 2 repeating if neither are chose
- Console.Write("1. Sign in " & vbCrLf & "2. Register " & vbCrLf & "Choose: ") : choice = Console.ReadLine 'vbcrlf puts it on a new line
- Loop While choice <> "1" And choice <> "2"
- If choice = "1" Then
- If signIn() = True Then 'Calls the function sign in
- Console.WriteLine("Welcome!")
- End If
- Else
- Register() 'Calls the sub procedure register
- Console.WriteLine("Registered!")
- End If
- Console.ReadLine()
- End Sub
- Function signIn()
- Dim username, password, readPass As String 'Read in password from a individual file
- Dim finish As Boolean = False 'End states for each loop
- Do While finish = False
- Do While readPass = Nothing 'Loops until username is acceptable
- Console.Write("Enter your username: ") : username = Console.ReadLine
- readPass = ReadFrom(username) 'Uses function to read in the file with the username as its path
- If readPass = Nothing Then
- Console.WriteLine("Please enter a valid username")
- End If
- Loop
- Console.Write("Enter your password: ") : password = Console.ReadLine
- If readPass = password Then 'Checks if password in file equals users inputted password
- finish = True
- Else
- finish = False
- Console.WriteLine("Please enter a valid password")
- End If
- Loop
- Return finish
- End Function
- Sub Register()
- Dim Iage As Integer
- Dim name, age, username, password As String
- Do
- Console.Write("Please enter your first name: ") : name = Console.ReadLine
- Loop While name.Length < 4 'Loops until there name is more than 4 characters
- Try
- 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
- Catch ex As Exception
- Console.WriteLine("Not an age!") : Console.ReadLine()
- Main()
- End Try
- age = Iage 'Sets iage to a string
- username = name.Substring(0, 3) & age 'gets first 3 characters of name and adds age
- Console.WriteLine("Your username is: " & username)
- Console.Write("Please input a password: ") : password = Console.ReadLine
- Create(username) 'Calls the create procedure
- WriteTo(username, password) 'calls the writeto procedure
- End Sub
- Sub WriteTo(ByRef FilePath As String, Message As String)
- Using Writet As New StreamWriter(FilePath) 'Writes to file in bin with username as title
- Writet.WriteLine(Message)
- End Using
- End Sub
- Function ReadFrom(ByVal FilePath As String)
- Dim message As String
- Try
- Using ReadFr As New StreamReader(FilePath) 'trys to read the message from the file
- message = ReadFr.ReadLine
- End Using
- Catch ex As Exception
- message = Nothing ' if there is no message an error will happen so it sets message = to nothing and allows the program to carry on
- End Try
- Return message 'returns the message
- End Function
- Sub Create(ByRef username As String) 'Creates text document in bin with username as title
- Dim filepath As String = username
- If Not System.IO.File.Exists(filepath) Then 'Checks if document already exists
- System.IO.File.Create(filepath).Dispose() 'Creates a new file
- End If
- End Sub
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement