Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import javax.swing.JOptionPane;
  4. public class MCQ
  5. {
  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. readFile = readFilesIntoArrayLists();
  14. if(!readFile)
  15. System.out.println("One or more files do not exist");
  16. else
  17. {
  18. quizImplementation();
  19. }
  20. }
  21.  
  22. public static boolean readFilesIntoArrayLists() throws IOException
  23. {
  24. String filename1 = "topics.txt";
  25. String filename2 = "questions.txt";
  26.  
  27. File topicFile = new File(filename1);
  28. File questionFile = new File(filename2);
  29. String fileElements[];
  30.  
  31. topics = new ArrayList<ArrayList<String>>();
  32. topics.add(new ArrayList<String>());
  33. topics.add(new ArrayList<String>());
  34.  
  35. questions = new ArrayList<ArrayList<String>>();
  36. questions.add(new ArrayList<String>());
  37. questions.add(new ArrayList<String>());
  38. questions.add(new ArrayList<String>());
  39. questions.add(new ArrayList<String>());
  40. questions.add(new ArrayList<String>());
  41. questions.add(new ArrayList<String>());
  42. questions.add(new ArrayList<String>());
  43. questions.add(new ArrayList<String>());
  44. questions.add(new ArrayList<String>());
  45.  
  46. if(topicFile.exists() && questionFile.exists())
  47. {
  48. Scanner in = new Scanner(topicFile);
  49. while(in.hasNext())
  50. {
  51. fileElements = (in.nextLine()).split(",");
  52. topics.get(0).add(fileElements[0]);
  53. topics.get(1).add(fileElements[1]);
  54. }
  55. in.close();
  56. in = new Scanner(questionFile);
  57. while(in.hasNext())
  58. {
  59. fileElements = (in.nextLine()).split(",");
  60. questions.get(0).add(fileElements[0]);
  61. questions.get(1).add(fileElements[1]);
  62. questions.get(2).add(fileElements[2]);
  63. questions.get(3).add(fileElements[3]);
  64. questions.get(4).add(fileElements[4]);
  65. questions.get(5).add(fileElements[5]);
  66. questions.get(6).add(fileElements[6]);
  67. questions.get(7).add(fileElements[7]);
  68. questions.get(8).add(fileElements[8]);
  69. }
  70. in.close();
  71. return true;
  72. }else
  73. return false;
  74.  
  75. }
  76.  
  77. /*
  78. John Long - 12132306
  79. This method dynamically gets the topic selection from the user
  80. and returns this as a String
  81. */
  82.  
  83. public static String getTopicOption()
  84. {
  85. String topicOptionNums = "";
  86. String topicOptionText = "";
  87. String topicOptions = "";
  88. String selection = "";
  89. int topicNumListSize = topics.get(0).size(); // **
  90. int topicTextListSize = topics.get(1).size(); // **
  91.  
  92. // get topic numbers and question text from topics file
  93. for(int i=0; i<topicNumListSize && i<topicTextListSize; i++)
  94. {
  95. topicOptionNums = topics.get(0).get(i); // **
  96. topicOptionText = topics.get(1).get(i); // **
  97. topicOptions += topicOptionNums + " " + topicOptionText + "n";
  98. }
  99.  
  100. String menuMessage = "Select which topics test you would like to take";
  101. String errorMessage = "Invalid menu selection.nnValid options are 0 to " + topicNumListSize + " inclusive.";
  102. errorMessage += "nSelect OK to retry or cancel to exit.";
  103. String errorHeader = "Error in user input";
  104. boolean validInput = false;
  105. String menuChoicePattern = "[1-4]{1}"; // **need to modify to dynamically perform**
  106.  
  107. while (!(validInput))
  108. {
  109. selection = JOptionPane.showInputDialog(null, topicOptions, menuMessage, 3);
  110. if (selection == null || selection.matches(menuChoicePattern))
  111. validInput = true;
  112. else
  113. JOptionPane.showMessageDialog(null, errorMessage, errorHeader, 2);
  114. }
  115.  
  116. return selection;
  117. }
  118.  
  119. /*
  120. John Long - 12132306
  121. This method runs the quiz for the user
  122. */
  123. public static void quizImplementation()
  124. {
  125. int selection = Integer.parseInt(getTopicOption());
  126. String fileElements[];
  127. String qOptions = "";
  128. String question = "";
  129. String topicHeader = "";
  130. String msg1 = "Enter the number of the chosen answer.";
  131.  
  132.  
  133. for(int i=0; i<questions.get(0).size(); i++)
  134. {
  135. if(Integer.parseInt(questions.get(i).get(0)) == selection)
  136. {
  137. topicHeader += topics.get(selection).get(0);
  138. qOptions = questions.get(2).get(i) + "n";
  139. qOptions += "1. " + questions.get(3).get(i) + "n";
  140. qOptions += "2. " + questions.get(4).get(i) + "n";
  141. qOptions += "3. " + questions.get(5).get(i) + "n";
  142. qOptions += "4. " + questions.get(6).get(i) + "n";
  143. }
  144. question = JOptionPane.showInputDialog(null,qOptions,topicHeader,3);
  145. }
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement