Advertisement
Guest User

Untitled

a guest
Jan 30th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.40 KB | None | 0 0
  1.  
  2. Module Module1
  3. Structure teacher_login
  4. Dim username As String
  5. Dim password As String
  6. End Structure
  7. Structure student_login
  8. Dim username As String
  9. Dim password As String
  10. Dim name As String
  11. Dim year As String 'structure for seperate student + teacher logins
  12. Dim test_status As String
  13. End Structure
  14. Dim students(40) As student_login
  15. Dim teacher(2) As teacher_login 'assigns name to structure
  16. Dim Student_counter As Integer = 0
  17. Dim Login_counter As Integer = 0 ' need to make global as refereced in both login subs
  18. Dim week As Integer
  19. Dim spel(9) As String
  20. Dim definitions(9) As String
  21. Dim login_path As String = "N:\Computing\Spelling project\login.csv"
  22. Sub Main()
  23. login_file_scrape()
  24. login()
  25. End Sub
  26. Sub login_file_scrape()
  27. FileOpen(1, login_path, OpenMode.Input)
  28. Do Until EOF(1)
  29. Dim line As String = LineInput(1)
  30. Dim temp() As String = Split(line, ",") 'extracts the line and splits it with the commas
  31. If temp(2) = "S" Then
  32. students(Student_counter).username = temp(0)
  33. students(Student_counter).password = temp(1)
  34. students(Student_counter).name = temp(3)
  35. students(Student_counter).year = temp(4)
  36. students(Student_counter).test_status = temp(5) 'whether the student has completed the test for the week.
  37. Student_counter += 1
  38. Else
  39. teacher(Login_counter).username = temp(0)
  40. teacher(Login_counter).password = temp(1)
  41. Login_counter += 1
  42. End If
  43. 'if status field is S then store in student record
  44. 'else store in teacher record
  45. Loop
  46. 'counter used to track how many students and teachers so the corresponding arrays
  47. 'to be created will have an upper limit
  48. FileClose(1)
  49. End Sub
  50. Sub login()
  51. Console.Clear()
  52. Console.WriteLine("Username:")
  53. Dim Use_name As String = Console.ReadLine
  54. Console.WriteLine("Password:")
  55. Dim P_word As String = Console.ReadLine
  56. For i = 0 To Student_counter - 1
  57. If Use_name.ToLower = students(i).username.ToLower And P_word = students(i).password Then
  58. student_menu(students(i).year, students(i).name)
  59. 'passes the year and name of the student
  60. End If
  61. Next
  62. 'check for student login
  63. For i = 0 To Login_counter - 1
  64. If Use_name.ToLower = teacher(i).username.ToLower And P_word = teacher(i).password Then
  65. teacher_menu()
  66. End If
  67. 'if not loops back to the login screen
  68. Next
  69. 'only reaches the end if incorrect login
  70. Console.WriteLine("Invalid Username or Password")
  71. Console.WriteLine("You will return to login menu in 3 seconds.")
  72. System.Threading.Thread.Sleep(3000)
  73. login() 'if it reaches the bottom then we know it's not a correct Username and Password
  74. End Sub
  75. Sub teacher_menu()
  76. Console.Clear()
  77. Console.WriteLine("WELCOME TO TEACHER'S MENU")
  78. Console.WriteLine("1 - Enter new words")
  79. Console.WriteLine("2 - See Progress")
  80. Console.WriteLine("3 - Increment Week")
  81. Console.WriteLine("4 - Logout")
  82. Dim action As Integer = Console.ReadLine
  83. If action = 1 Then
  84. new_words()
  85. ElseIf action = 2 Then
  86. progress_tracker()
  87. ElseIf action = 3 Then
  88. score_check(week_tracker())
  89. week_tracker()
  90. reset_test_status()
  91. ElseIf action = 4 Then
  92. login()
  93. Else
  94. Console.WriteLine("Invalid Input")
  95. System.Threading.Thread.Sleep(2000)
  96. teacher_menu()
  97. End If
  98. End Sub
  99. Function week_tracker()
  100. Dim week_file As String = "N:\Computing\Spelling project\week.csv"
  101. week = System.IO.File.ReadAllText(week_file)
  102. 'gets week value from file
  103. System.IO.File.WriteAllText(week_file, week + 1)
  104. 'increment week in file
  105. Console.WriteLine("Week Successfully Incremented now at week:" & (week + 1).ToString)
  106. Console.ReadLine()
  107. 'success message and loop back to menu
  108. Return week
  109. End Function
  110. Sub reset_test_status()
  111. Dim login_filepath As String = "N:\Computing\Spelling project\login.csv"
  112. Dim lines() As String = System.IO.File.ReadAllLines(login_filepath)
  113. For i = 0 To lines.Length - 1
  114. Dim temp() As String = Split(lines(i), ",")
  115. If temp(2) = "S" Then
  116. 'check if line belongs to student, if so take action
  117. lines(i) = lines(i).TrimEnd()
  118. 'removes spaces from the end
  119. If lines(i).EndsWith(True) Then
  120. lines(i) = lines(i).Substring(0, lines(i).Length - 4) & False
  121. End If
  122. 'trims end and replaces with false, no action needed if end already false
  123. End If
  124. System.IO.File.WriteAllLines(login_filepath, lines)
  125. 'rewrite with corrections
  126. Next
  127. teacher_menu()
  128. End Sub
  129. Sub score_check(ByVal week) 'checks if student has done the test for the week if not set their score for the week as 0
  130. Dim score_file As String = "N:\Computing\Spelling project\score.csv"
  131. Dim lines() As String = System.IO.File.ReadAllLines(score_file)
  132. For i = 0 To lines.Length - 1
  133. Dim temp() As String = Split(lines(i), ",")
  134. If temp.Length = week Then
  135. lines(i) = lines(i) & ",0"
  136. End If
  137. 'the further along the week is the more score entries there should be
  138. 'by comparing the amount of entries we know if the student has completed the test for the week.
  139. 'if not edit their line so that the score is 0
  140. Next
  141. System.IO.File.WriteAllLines(score_file, lines)
  142. 'rewrite file with corrects (if any)
  143. End Sub
  144. Sub new_words()
  145. Console.WriteLine("For which year would you like to edit the words ? (3-6)")
  146. Dim year As Integer = Console.ReadLine
  147. 'gets year to edit
  148. Dim year_file As String = "N:\Computing\Spelling project\year" & year.ToString & "_words.csv"
  149. Dim lines(9) As String
  150. 'limited to 10 lines (10 words + definitions)
  151. For i = 0 To 9
  152. Console.Write("Enter the word: ")
  153. Dim words As String = Console.ReadLine
  154. Console.Write("Enter the corresponding definition: ")
  155. Dim definitions As String = Console.ReadLine
  156. lines(i) = "" & words.ToString & "," & definitions & ""
  157. 'prompts for word and definition and write to file seperated by ","
  158. Next
  159. System.IO.File.WriteAllLines(year_file, lines)
  160. 'update the file
  161. Console.WriteLine("New words successfully input!")
  162. Console.ReadLine()
  163. teacher_menu()
  164. 'success messagen, loops back to menu
  165. End Sub
  166. Sub progress_tracker()
  167. Dim score_file As String = "N:\Computing\Spelling project\score.csv"
  168. Dim lines() As String = System.IO.File.ReadAllLines(score_file)
  169. Console.Write("Name")
  170. For i = 1 To 6
  171. Console.Write(" Week {0}", i)
  172. Next
  173. Console.WriteLine("")
  174. For i = 0 To lines.Length - 1
  175. Dim temp() As String = Split(lines(i), ",")
  176. Console.WriteLine("" & vbCr & "" & temp(0) & "")
  177. For k = 1 To temp.Length - 1
  178. Console.Write(" " & temp(k) & "")
  179. Next
  180. Next
  181. Console.ReadLine()
  182. End Sub
  183. Sub student_menu(ByVal student_year, ByVal student_name)
  184. Console.Clear()
  185. Dim words_definitons(9, 2) As String
  186. words_definitons = get_word_definition(student_year, words_definitons)
  187. Console.WriteLine("Student Menu")
  188. Console.WriteLine("1 - Take Test")
  189. Console.WriteLine("2 - Log out")
  190. Dim choice As Integer = Console.ReadLine
  191. Select Case choice
  192. Case 1
  193. test_availibility(student_name) 'checks if students have completed the test for the week
  194. Dim score As Integer = spelling_bee(words_definitons)
  195. 'student takes test and returns the mark
  196. save_score(score, student_name)
  197. Case 2
  198. Console.WriteLine("Logging Out")
  199. System.Threading.Thread.Sleep(3000)
  200. login()
  201. Case Else
  202. Console.WriteLine("Input not valid, press enter to re-enter.")
  203. Console.ReadLine()
  204. student_menu(student_year, student_name)
  205. End Select
  206. End Sub
  207. Function get_word_definition(ByVal student_year, ByRef words_definitons)
  208. Dim words_filepath As String = "N:\Computing\Spelling project\year" & student_year & "_words.csv"
  209. Dim lines() As String = System.IO.File.ReadAllLines(words_filepath)
  210. 'reads word file and seperates lines into array
  211. For i = 0 To 9
  212. Dim temp() As String = Split(lines(i), ",")
  213. words_definitons(i, 1) = temp(0)
  214. words_definitons(i, 2) = temp(1)
  215. 'split array and assign word and definition
  216. Next
  217. Return words_definitons
  218. 'return populated array
  219. End Function
  220. Sub test_availibility(ByVal student_name) 'checks if the student has completed the test for the current week
  221. For i = 0 To Student_counter - 1
  222. If student_name = students(i).name Then
  223. 'finds the line the student is on and checks if the test has been completed for the week.
  224. If students(i).test_status = True Then
  225. Console.WriteLine("You have already done the test for this week, wait until new words are set by the teacher.")
  226. Console.ReadLine()
  227. student_menu(3, student_name)
  228. 'if the student has comleted the test we return back to the student menu
  229. 'our given parameters are arbitrary as the students will not take the test, the parameters need only to not crash the program.
  230. 'their only availible action in the menu is to log out
  231. Else
  232. Dim lines() As String = System.IO.File.ReadAllLines("N:\Computing\Spelling project\login.csv")
  233. For k = 0 To lines.Length - 1
  234. Dim temp() As String = Split(lines(k), ",")
  235. If student_name = temp(3) Then
  236. lines(k) = "" & temp(0) & "," & temp(1) & "," & temp(2) & "," & temp(3) & "," & temp(4) & "," & True & ""
  237. Exit For
  238. End If
  239. 'if the name matches then we edit the final field in the string to True
  240. Next
  241. System.IO.File.WriteAllLines("N:\Computing\Spelling project\login.csv", lines)
  242. 'rewrite file with changes.
  243. End If
  244. End If
  245. Next
  246. End Sub
  247. Sub save_score(ByVal score, ByVal student_name)
  248. Dim lines() As String = IO.File.ReadAllLines("N:\Computing\Spelling project\score.csv")
  249. 'splits text file by line sorted into 'lines' array
  250. Dim written As Boolean
  251. Console.WriteLine(Student_counter)
  252. For i = 0 To lines.Length - 1
  253. Dim temp() As String = Split(lines(i), ",")
  254. If temp(0) = student_name Then
  255. 'if student is in file, append score to the line the student was found on and rewrite file with the addition.
  256. lines(i) = lines(i) & "," & score & ""
  257. System.IO.File.WriteAllLines("N:\Computing\Spelling project\score.csv", lines)
  258. 'rewrites the lines to the file with the added score
  259. written = True
  260. Exit For
  261. End If
  262. Next
  263. If written <> True Then
  264. System.IO.File.AppendAllText("N:\Computing\Spelling project\score.csv", vbCrLf + "" & student_name & "," & score & "")
  265. End If
  266. 'if no previous entry we append to end of file on a new line
  267. End Sub
  268. Function spelling_bee(ByVal words_definitions)
  269. Dim marks As Integer = 0
  270. Dim no_error As Integer
  271. For i = 0 To 9
  272. Console.WriteLine(words_definitions(i, 2))
  273. Console.WriteLine("Enter the matching word")
  274. Dim ans As String = Console.ReadLine
  275. If ans.ToLower.ToString = words_definitions(i, 1).ToString.ToLower Then
  276. Console.WriteLine("CORRECT")
  277. marks += 2
  278. ElseIf ans.Length - words_definitions(i, 1).Length < -1 Or ans.Length - words_definitions(i, 1).Length > 1 Then
  279. Console.WriteLine("Major error")
  280. Else
  281. Dim ansArray() As Char = ans.ToCharArray
  282. Dim wordsArray() As Char = words_definitions(i, 1).ToCharArray
  283. 'before this we check if they are the same, or 2 characters off
  284. 'if it reaches this if statement than we know the word is either
  285. 'one character too big/small or the same length
  286. If ansArray.Length > wordsArray.Length Then
  287. For j = 0 To wordsArray.Length - 1
  288. If wordsArray(j).ToString.Contains(ansArray(j)) = False Then
  289. no_error += 1
  290. End If
  291. Next
  292. If ansArray.Length - wordsArray.Length <> 0 Then
  293. no_error += 1
  294. End If
  295. 'if the input answer is longer by 1 we run it correct word amount of times to prevent calling
  296. 'an index too high on the word array since the input array is bigger
  297. ElseIf wordsArray.Length > ansArray.Length Then
  298. For j = 0 To ansArray.Length - 1
  299. If wordsArray(j).ToString.Contains(ansArray(j)) = False Then
  300. no_error += 1
  301. End If
  302. Next
  303. If ansArray.Length - wordsArray.Length <> 0 Then
  304. no_error += 1
  305. End If
  306. 'if the correct word is longer by 1 we run it answer amount of time for the same reasons
  307. Else
  308. For j = 0 To wordsArray.Length - 1
  309. If wordsArray(j).ToString.Contains(ansArray(j)) = False Then
  310. no_error += 1
  311. End If
  312. Next
  313. 'if they are the same length
  314. End If
  315. End If
  316. If no_error = 1 Then
  317. marks += 1
  318. Console.WriteLine("minor error")
  319. ElseIf no_error > 1 Then
  320. Console.WriteLine("major error")
  321. 'if only 1 error than its a minor error, if this statement isnt true then
  322. 'theres at least 2 errors(major) and we don't add any marks.
  323. End If
  324. no_error = 0
  325. Next
  326. Console.WriteLine("total marks was: " & marks)
  327. Console.ReadLine()
  328. Return marks
  329. End Function
  330. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement