You are on page 1of 6

Harvey James Coding Challenge 2-Speed Tracker

Harvey James Coding Challenge 2-Speed Tracker


Code:
Imports System.IO
' required for the file system
Module Module1
Function GetInterval(endr As Integer, start As Integer) As Integer
'Function to get the time it takes for the car to go between cameras
Return (endr - start)
End Function
Function GetMph(time)
'Function to get Mph
Dim Mph As Integer = ((1 / time) * 3600)
' '* 3600' becuase the time is in seconds, it would be '* 60' if the time was in
minuites
Return Mph
End Function
Function CheckPlate(plate As String) As String
'This function is to check if the number plate is valid
Dim length, i As Integer
length = plate.Length
Dim AskChara(length - 1) As Integer
Dim Chara(length - 1), valid As String
valid = "Invalid"
Select Case length
Case 7
'The length of the plate should always be 7, else is invalid
For i = 0 To 6
' Very helpfull video: https://www.youtube.com/watch?v=dQw4w9WgXcQ
AskChara(i) = Asc(GetChar(plate, (i + 1)))
Next
For i = 0 To 1
Select Case AskChara(i)
Case 65 To 90 : valid = "Valid"
' first 2 characters should be between 65 and 90 becuase they are characters
Case < 65 : Return "Invalid"
Case > 90 : Return "Invalid"
End Select
Next
For i = 2 To 6
Select Case AskChara(i)

Harvey James Coding Challenge 2-Speed Tracker


Case 48 To 57 : valid = "Valid"
' last 5 charactes should be betwnn 48 and 57 becuase they are integers
Case > 57 : Return "Invalid"
Case < 48 : Return "Invalid"
End Select
Next
Case <= 6 : Return "Invalid"
Case >= 8 : Return "Invalid"
End Select
Select Case valid
Case "Valid" : Return "Valid"
End Select
End Function
Sub Main()
Dim start_time(2), end_time(2), interval(2), i1, i2, time, Mph As Integer
' (0) is the hour, (1) is the minute, (2) is the second
Dim writeSelect = New String(2) {"hour", "minuite", "second"}
Dim FirSec = New String(1) {"first", "second"}
Dim plate As String
Console.WriteLine("Enter the number plate of the car (use capitals)")
plate = Console.ReadLine()
Console.WriteLine("The plate is " & CheckPlate(plate))
For i2 = 0 To 1
For i1 = 0 To 2
Select Case i1
Case = 0 : Console.WriteLine("Enter the " & writeSelect(i1) & " of
the time that the car goes past the " & FirSec(i2) & " speed camera")
Case = 1 : Console.WriteLine("Enter the " & writeSelect(i1) & " of
the time that the car goes past the " & FirSec(i2) & " speed camera")
Case = 2 : Console.WriteLine("Enter the " & writeSelect(i1) & " of
the time that the car goes past the " & FirSec(i2) & " speed camera")
End Select
Select Case i2
Case = 0 : start_time(i1) = Console.ReadLine()
Case = 1 : end_time(i1) = Console.ReadLine()
End Select
Next
Next
For i1 = 0 To 2
interval(i1) = GetInterval(end_time(i1), start_time(i1))
Next
time = (interval(0) * 3600) + (interval(1) * 60) + (interval(2))
' in seconds between cameras
Console.WriteLine("The car with number plate " & plate & " took " & time & "
seconds to drive one mile")
Console.WriteLine("Its average speed is " & GetMph(time) & "mph")
Mph = GetMph(time)
Select Case Mph
Case 1 To 30 : Console.WriteLine("Car is driving too slow, Deploying
Tomahawk Cruise Missile to intercept.")
Case 31 To 70 : Console.WriteLine("The car is driving within the speed
limit")
Case > 70 : Console.WriteLine("The car is speeding, Deploying Tomahawk
Cruise Missile to intercept.")
End Select
Call CarFile(plate, Mph)

End Sub
Sub CarFile(plate As String, speed As Integer)
'function for writing to a file and reading from it
Dim path As String = "vehicles.txt"
'search to create files:

Harvey James Coding Challenge 2-Speed Tracker


https://docs.microsoft.com/en-us/dotnet/api/system.io.file.create?view=netframework-
4.7.1
Dim RandomPlates(3) As String
'search to open files:
https://docs.microsoft.com/en-us/dotnet/api/system.io.file.open?view=netframework-
4.7.1
Dim i, RandomSpeeds(3) As Integer
For i = 0 To 3
RandomPlates(i) = (RandomPlate1() & RandomPlate2() & " ")
Next
For i = 0 To 3
RandomSpeeds(i) = (RandomSpeed())
Next
If (File.Exists(path)) Then
File.Delete(path)
'if file exists it overwrites it
Using sw As StreamWriter = File.CreateText(path)
sw.WriteLine(plate & " " & speed & "Mph")
sw.WriteLine(RandomPlates(0) & RandomSpeeds(0) & "Mph")
sw.WriteLine(RandomPlates(1) & RandomSpeeds(1) & "Mph")
sw.WriteLine(RandomPlates(2) & RandomSpeeds(2) & "Mph")
sw.WriteLine(RandomPlates(3) & RandomSpeeds(3) & "Mph")
End Using
ElseIf Not File.Exists(path) Then
Using sw As StreamWriter = File.CreateText(path)
' if not then Create a file to write to.
sw.WriteLine(plate & " " & speed & "Mph")
sw.WriteLine(RandomPlates(0) & RandomSpeeds(0) & "Mph")
sw.WriteLine(RandomPlates(1) & RandomSpeeds(1) & "Mph")
sw.WriteLine(RandomPlates(2) & RandomSpeeds(2) & "Mph")
sw.WriteLine(RandomPlates(3) & RandomSpeeds(3) & "Mph")
End Using
End If
Dim objReader As New System.IO.StreamReader("vehicles.txt")
Console.WriteLine("Plates and Speeds stored in file: ")
Console.WriteLine(objReader.ReadToEnd)
objReader.Close()
For i = 0 To 3
Select Case RandomSpeeds(i)
Case > 70 : Console.WriteLine("The car with Plate " & RandomPlates(i)
& " is speeding over the limit (70)")
End Select
Next
Select Case speed
Case > 70 : Console.WriteLine("The car with Plate " & plate & " is
speeding over the limit (70)")
End Select
Console.ReadLine()
End Sub
Function RandomPlate1()
'to get first 2 random characters
Randomize()
Dim Fir1(1), end1 As String
Dim AscFir1(1), i As Integer
For i = 0 To 1
AscFir1(i) = CInt(Math.Floor((90 - 65 + 1) * Rnd())) + 65
' randomValue = CInt(Math.Floor((upperbound - lowerbound + 1) * Rnd())) + lowerbound
Fir1(i) = Chr(AscFir1(i))
Next
end1 = Fir1(0) + Fir1(1)
Return end1
End Function

Harvey James Coding Challenge 2-Speed Tracker


Function RandomPlate2()
'to get last 5 random numbers
Randomize()
Dim Fir2(4), end2 As String
Dim AscFir2(4), i As Integer
For i = 0 To 4
AscFir2(i) = CInt(Math.Floor((57 - 48 + 1) * Rnd())) + 48
' randomValue = CInt(Math.Floor((upperbound - lowerbound + 1) * Rnd())) + lowerbound
Fir2(i) = Chr(AscFir2(i))
Next
end2 = Fir2(0) & Fir2(1) & Fir2(2) & Fir2(3) & Fir2(4)
Return end2
End Function
Function RandomSpeed()
' gets a random number between 200 and 30
Randomize()
Dim speed As Integer
speed = CInt(Math.Floor((200 - 30 + 1) * Rnd())) + 30
Return speed
End Function
End Module

'stri"LLLL,10:10:10,10:11:13"
'Dim arr() as string = stri.split(",")
'split the array into 3 parts:
'plate, start, end

Harvey James Coding Challenge 2-Speed Tracker

You might also like