Advertisement
Guest User

Untitled

a guest
May 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.77 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. public class QuestionAndAnswers
  4. {
  5. public static ArrayList<ArrayList<String>> users;
  6. public static ArrayList<ArrayList<String>> topics;
  7. public static ArrayList<ArrayList<String>> questions;
  8.  
  9.  
  10. public static void main(String [] args) throws IOException
  11. {
  12. boolean readFile;
  13. boolean validUser = false;
  14. int topic = 0;
  15. int currentScore = 0;
  16. int attemptedAnswers = 0;
  17. int totalScore = 0;
  18. Scanner input = new Scanner(System.in);
  19. readFile = readToArrayLists();
  20.  
  21. if(!readFile)
  22. System.out.println("One or more file do not exist.");
  23. else{
  24. validUser = usernamePassword(validUser);
  25. if(validUser)
  26. {
  27. System.out.println("Valid login \n");
  28. while(validUser)
  29. {
  30. topic = getTopic(topic);
  31. if(topic > 0 && topic <= topics.get(0).size())
  32. {
  33. System.out.println("Displaying questions on " + topics.get(1).get(topic-1) + ":\n");
  34. currentScore = askQuestions(currentScore, topic, attemptedAnswers);
  35. totalScore = getScore(totalScore, currentScore, attemptedAnswers);
  36. System.out.println("Your overall score is: " + totalScore + "\n");
  37. }
  38. }
  39. }
  40. else
  41. System.out.print("Out of attempts, please try again later.");
  42. }
  43. }
  44.  
  45. public static boolean readToArrayLists() throws IOException
  46. {
  47. /* Takes the information in the files and puts them into different
  48. array lists. If all the files are present it returns true to the main method. */
  49.  
  50.  
  51. String filename1 = "Users.txt";
  52. String filename2 = "Topics.txt";
  53. String filename3 = "Questions.txt";
  54.  
  55. String fileElements[];
  56. File inputFile1 = new File(filename1);
  57. File inputFile2 = new File(filename2);
  58. File inputFile3 = new File(filename3);
  59.  
  60. users = new ArrayList<ArrayList<String>>();
  61. users.add(new ArrayList<String>());
  62. users.add(new ArrayList<String>());
  63. users.add(new ArrayList<String>());
  64.  
  65. topics = new ArrayList<ArrayList<String>>();
  66. topics.add(new ArrayList<String>());
  67. topics.add(new ArrayList<String>());
  68.  
  69. questions = new ArrayList<ArrayList<String>>();
  70. questions.add(new ArrayList<String>());
  71. questions.add(new ArrayList<String>());
  72. questions.add(new ArrayList<String>());
  73. questions.add(new ArrayList<String>());
  74. questions.add(new ArrayList<String>());
  75. questions.add(new ArrayList<String>());
  76. questions.add(new ArrayList<String>());
  77. questions.add(new ArrayList<String>());
  78.  
  79.  
  80. if(inputFile1.exists() && inputFile2.exists() && inputFile3.exists())
  81. {
  82. Scanner in;
  83. in = new Scanner(inputFile1);
  84. while(in.hasNext())
  85. {
  86. fileElements = (in.nextLine()).split(",");
  87. users.get(0).add(fileElements[0]);
  88. users.get(1).add(fileElements[1]);
  89. users.get(2).add(fileElements[2]);
  90.  
  91. }
  92. in.close();
  93. in = new Scanner(inputFile2);
  94. while(in.hasNext())
  95. {
  96. fileElements = (in.nextLine()).split(",");
  97. topics.get(0).add(fileElements[0]);
  98. topics.get(1).add(fileElements[1]);
  99.  
  100. }
  101. in.close();
  102. in = new Scanner(inputFile3);
  103. while(in.hasNext())
  104. {
  105. fileElements = (in.nextLine()).split(",");
  106. questions.get(0).add(fileElements[0]);
  107. questions.get(1).add(fileElements[1]);
  108. questions.get(2).add(fileElements[2]);
  109. questions.get(3).add(fileElements[3]);
  110. questions.get(4).add(fileElements[4]);
  111. questions.get(5).add(fileElements[5]);
  112. questions.get(6).add(fileElements[6]);
  113. questions.get(7).add(fileElements[7]);
  114.  
  115.  
  116. }
  117. in.close();
  118. return true;
  119. }
  120. else
  121. return false;
  122.  
  123. }
  124.  
  125. public static boolean usernamePassword(boolean validUser)
  126. {
  127. /* Accepts an input for the username, checks if the input matches the username in the array list,
  128. if it matches it takes in input for the password and does the same,
  129. if both the username and password are present and they are indexed in the same row
  130. the method returns true, else it returns false. */
  131.  
  132. validUser = false;
  133. boolean validUsername = false, validPassword = false;
  134. int number, attempts = 0;
  135. boolean userIndex, passwordIndex;
  136. int userNumber, passwordNumber;
  137. String userInput, passwordInput;
  138. while(attempts <3 && validUser == false)
  139. {
  140. validUsername = false;
  141. validPassword = false;
  142. validUser = false;
  143. userNumber = 0;
  144. passwordNumber = 0;
  145. userIndex = false;
  146. passwordIndex = false;
  147. userInput = "";
  148. passwordInput = "";
  149. Scanner in = new Scanner(System.in);
  150. Scanner inn = new Scanner(System.in);
  151.  
  152. System.out.print("Please enter your username: ");
  153. userInput = in.nextLine();
  154.  
  155. for(int i = 0; i<= users.get(0).size()-1 && validUsername == false; i++)
  156. {
  157. userIndex = users.get(0).get(i).matches(userInput);
  158. if(userIndex)
  159. { userNumber = i;
  160. validUsername = true;
  161. System.out.print("Please enter your password: ");
  162. passwordInput = inn.nextLine();
  163. for(int j = 0; j<= users.get(1).size()-1 && validPassword == false; j++)
  164. {
  165. passwordIndex = users.get(1).get(j).matches(passwordInput);
  166. if(passwordIndex)
  167. {passwordNumber = j;
  168. validPassword = true;}
  169.  
  170. }
  171.  
  172.  
  173.  
  174. }
  175. else
  176. validUsername = false;
  177. }
  178.  
  179.  
  180.  
  181.  
  182. if(validUsername == true && validPassword == true && userIndex == passwordIndex && userNumber == passwordNumber)
  183. {
  184. validUser = true;
  185. return validUser;
  186. }
  187. else
  188. {
  189. validUser = false;
  190. }
  191.  
  192.  
  193. if(validUser == false)
  194. System.out.println("Invalid username or password");
  195. attempts++;
  196. }
  197. return validUser;
  198.  
  199. }
  200.  
  201. public static int getTopic(int topic)
  202. { /* Puts the topic and question numbers into arrays.
  203. Using a nested for loop it checks that the topic has at least one
  204. question available before displaying it.
  205. It then takes an input for the topic number, checks that the input
  206. is in the right range and is numeric and returns the topic number to the main method */
  207.  
  208. int q[] = new int[questions.get(0).size()];
  209. int t[] = new int[topics.get(0).size()];
  210. boolean displayed = false;
  211. for(int x = 0; x <= questions.get(0).size()-1; x++)
  212. {
  213. q[x] = Integer.parseInt(questions.get(0).get(x));
  214. }
  215.  
  216. for(int y = 0; y <= topics.get(0).size()-1; y++)
  217. {
  218. t[y] = Integer.parseInt(topics.get(0).get(y));
  219. }
  220.  
  221. for(int i = 0; i<= topics.get(0).size()-1; i++)
  222. {
  223. displayed = false;
  224. for(int j = 0; j <= questions.get(0).size()-1 && displayed == false; j++ )
  225. {
  226. if(t[i] == q[j])
  227. {
  228. System.out.println("\t" + topics.get(0).get(i) + " " + topics.get(1).get(i));
  229. displayed = true;
  230. }
  231.  
  232. }
  233. }
  234. try
  235. {
  236. Scanner in = new Scanner(System.in);
  237. System.out.print("\nPlease enter the topic number: ");
  238. topic = in.nextInt();
  239. }
  240. catch(InputMismatchException anError)
  241. {
  242. System.out.println("\nNumeric input only");
  243.  
  244. }
  245. if(topic > 0 && topic <= topics.get(0).size())
  246. {
  247. return topic;
  248. }
  249. else
  250. System.out.println("\nInvalid input");
  251. return topic;
  252. }
  253.  
  254. public static int askQuestions(int currentScore, int topic, int attemptedAnswers)
  255. {
  256. /* Puts all the questions relating to the chosen topic in an array list, if there are more than 10 questions
  257. it will randomly choose 10, if there are 10 or less it will use all available questions.
  258. It then displays the questions one at a time and allows input for answers. If the answer is correct
  259. it will move on, if the answer is wrong it will display the right answer and an explanation.
  260. It also keeps count of the amount you got correct and your attempts which are then displayed.
  261. */
  262. int answer = 0;
  263.  
  264. ArrayList<ArrayList<String>> currentQuestions;
  265.  
  266. currentQuestions = new ArrayList<ArrayList<String>>();
  267. currentQuestions.add(new ArrayList<String>());
  268. currentQuestions.add(new ArrayList<String>());
  269. currentQuestions.add(new ArrayList<String>());
  270. currentQuestions.add(new ArrayList<String>());
  271. currentQuestions.add(new ArrayList<String>());
  272. currentQuestions.add(new ArrayList<String>());
  273. currentQuestions.add(new ArrayList<String>());
  274.  
  275.  
  276. for(int i = 0; i <= questions.get(0).size()-1; i++)
  277. {
  278. if(topic == Integer.parseInt(questions.get(0).get(i)))
  279. {
  280. int row = 0;
  281.  
  282.  
  283. for(int j=0; j<=6; j++)
  284. {
  285. currentQuestions.get(j).add(row, questions.get(j+1).get(i));
  286.  
  287. }
  288. row++;
  289.  
  290.  
  291. }
  292. }
  293.  
  294.  
  295.  
  296.  
  297. if(currentQuestions.get(0).size() <=10)
  298. {
  299. for(int g = 0; g < 10 && g<= currentQuestions.get(0).size()-1; g++)
  300. {
  301. try
  302. {
  303. Scanner in = new Scanner(System.in);
  304.  
  305. System.out.println(currentQuestions.get(0).get(g));
  306. for(int h = 1; h <= 4; h++)
  307. {
  308. System.out.println("" + h + "." + currentQuestions.get(h).get(g));
  309. }
  310. answer = in.nextInt();
  311. }
  312. catch(InputMismatchException anError)
  313. {
  314. System.out.println("\nNumeric input only");
  315.  
  316. }
  317.  
  318.  
  319.  
  320. if(answer == Integer.parseInt(currentQuestions.get(5).get(g)))
  321. {
  322. System.out.println("\nCorrect answer \n");
  323. currentScore++;
  324. attemptedAnswers++;
  325.  
  326.  
  327. }
  328. else
  329. {
  330. System.out.println("Incorrect answer\n \nCorrect answer is: " + currentQuestions.get(Integer.parseInt(currentQuestions.get(5).get(g))).get(g) + "\nReason: " + currentQuestions.get(6).get(g) + "\n");
  331. attemptedAnswers++;
  332. }
  333.  
  334. }
  335. }
  336. else
  337. {
  338. int count[] = new int[10];
  339. boolean duplicate = false;
  340. for(int j = 0; j<= count.length-1; j++ )
  341. {
  342. duplicate = false;
  343. count[j] = (int)(Math.random()*(currentQuestions.get(0).size()-1));
  344.  
  345. for(int k = j+1; k <= count.length-1 && duplicate == false; k++)
  346. {
  347. if(count[j]==count[k])
  348. {
  349. duplicate = true;
  350.  
  351. }
  352.  
  353. }
  354.  
  355. if(duplicate == true)
  356. j--;
  357.  
  358. }
  359.  
  360. for(int g = 0; g <= count.length-1; g++)
  361. {
  362. try
  363. {
  364. Scanner in = new Scanner(System.in);
  365.  
  366. System.out.println(currentQuestions.get(0).get(count[g]));
  367. for(int h = 1; h <= 4; h++)
  368. {
  369. System.out.println("" + h + "." + currentQuestions.get(h).get(count[g]));
  370. }
  371. answer = in.nextInt();
  372. }
  373. catch(InputMismatchException anError)
  374. {
  375. System.out.println("\nNumeric input only");
  376.  
  377. }
  378.  
  379.  
  380.  
  381. if(answer == Integer.parseInt(currentQuestions.get(5).get(count[g])))
  382. {
  383. System.out.println("\nCorrect answer \n");
  384. currentScore++;
  385. attemptedAnswers++;
  386.  
  387.  
  388. }
  389. else
  390. {
  391. System.out.println("Incorrect answer\n \nCorrect answer is: " + currentQuestions.get(Integer.parseInt(currentQuestions.get(5).get(count[g]))).get(count[g]) + "\nReason: " + currentQuestions.get(6).get(count[g]) + "\n");
  392. attemptedAnswers++;
  393. }
  394.  
  395. }
  396.  
  397.  
  398.  
  399. }
  400.  
  401. System.out.println("Your score for this session is: " + currentScore +"/"+ attemptedAnswers + "\n");
  402.  
  403. return currentScore;
  404.  
  405. }
  406.  
  407. public static int getScore(int totalScore, int currentScore, int attemptedAnswers)
  408. {
  409. // Adds the current score to the total score //
  410.  
  411. totalScore += currentScore;
  412. users.get(0).set(1, Integer.toString(totalScore));
  413. return totalScore;
  414. }
  415. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement