Guest User

Untitled

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