Advertisement
Guest User

Untitled

a guest
Oct 25th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.90 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.text.DateFormat;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import java.util.Random;
  11. import java.util.Scanner;
  12. import java.util.StringTokenizer;
  13. import java.util.concurrent.TimeUnit;
  14.  
  15. import javax.swing.JLabel;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPasswordField;
  18. import javax.swing.JTextField;
  19.  
  20. public class pack {
  21.  
  22. // Program wide variables
  23. static Boolean debug = false; // True for debug mode
  24. static String invalidInput = "Please only input true/false or t/f";
  25. static String invalidLogin = "Sorry that username or password is not correct, please try again.";
  26. static String tooManyAttempts = "Sorry you have exceeded your 3 trys! Try again later.";
  27. static String loginPromptUser = "Please enter your username.";
  28. static String loginPromptUserDone = "Please enter your username or done to end the program.";
  29. static String loginPromptPass = "Please enter your password.";
  30. static String startParse = "Parsing your users file.";
  31. static String enterDone = "Enter done to quit.";
  32. static String testBankFile = "TestBank.txt";
  33. static String originalUsersFile = "UsersInfo.txt";
  34. static String newUsersFile = "NewUsers.txt";
  35. static String answersFile = "Answers.txt";
  36. static String firstName;
  37. static String lastName;
  38. static String username;
  39. static int quest = 4;
  40. static int testBankQ = 124;
  41. static int timesRan = 0;
  42. static String[] answeredQ = new String[quest];
  43. static Boolean[] answeredA = new Boolean[quest];
  44. static Boolean[] answeredC = new Boolean[quest];
  45.  
  46. // idk what these are called
  47. static DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy_HH-mm");
  48. static Date date = new Date();
  49. static Scanner kb = new Scanner(System.in);
  50.  
  51. // magic
  52. public static void main(String Args[]) throws IOException {
  53.  
  54. Random rand = new Random();
  55. Boolean run = true;
  56. int count = 0, right = 0, wrong = 0;
  57.  
  58. // Debug false
  59. if (!debug) {
  60. while (run) {
  61. parse(originalUsersFile, newUsersFile, 1);
  62. if (login(3, newUsersFile)) { // Check login creds
  63. long startTime = System.currentTimeMillis();
  64.  
  65. while (count < quest) {
  66. int r = rand.nextInt(testBankQ);
  67. int ansInt;
  68. boolean ans;
  69.  
  70. while(1>0) {
  71. ansInt = promptQ(read(r, testBankFile), count+1);
  72. if (ansInt == 0) {
  73. ans = true;
  74. break;
  75. } else if(ansInt == 1) {
  76. ans = false;
  77. }
  78. }
  79.  
  80.  
  81.  
  82. answeredQ[count] = read(r, testBankFile);
  83. answeredA[count] = ans;
  84. answeredC[count] = parseA(read(r, answersFile));
  85.  
  86. if (ans != parseA(read(r, answersFile))) {
  87. wrong++;
  88. } else if (ans == parseA(read(r, answersFile))) {
  89. right++;
  90. }
  91. count++;
  92. }
  93.  
  94. // Wrapping it all up
  95. JOptionPane.showMessageDialog(null, "You got " + wrong + " wrong and " + right + " correct.");
  96. long endDiff = (System.currentTimeMillis() - startTime);
  97.  
  98. makeReport(firstName, lastName, username, printTime(endDiff), right);
  99. timesRan++;
  100. } else {
  101. close();
  102. }
  103.  
  104. while (count == quest) {
  105. if (login(3, newUsersFile)) {
  106. count = 0;
  107. } else {
  108. close();
  109. }
  110. }
  111. }
  112. // Debug true
  113. } else {
  114. // Insert code here
  115. int ans2 = promptQ(read(2, testBankFile), count+1);
  116. if(ans2 == 2) {
  117. p("null");
  118. } else if (ans2 == 1) {
  119. p("false");
  120. } else {
  121. p("true");
  122. }
  123. }
  124. }
  125.  
  126. public static int promptQ(String question, int number) {
  127. Object[] options = { "True", "False" };
  128. int n = JOptionPane.showOptionDialog(null, question, "Question " + Integer.toString(number),
  129. JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, // do not use a custom Icon
  130. options,
  131. options[0]);
  132. if(n == 0) {
  133. return 0;
  134. } else if (n == 1) {
  135. return 1;
  136. }
  137. return 2;
  138. }
  139.  
  140. // Shortens System.out.println() to p()
  141. public static String p(String in) {
  142. System.out.println(in);
  143. return in;
  144. }
  145.  
  146. // Close the program
  147. public static void close() {
  148. System.exit(0);
  149. }
  150.  
  151. // Generates a report report(first, last, score, time, array of answers)
  152. public static void makeReport(String first, String last, String user, String time, int score) throws IOException {
  153. String fileName = user + "_COSC236_Quiz_" + dateFormat.format(date) + ".txt";
  154. try {
  155. File file = new File(fileName);
  156. file.createNewFile();
  157. } catch (IOException e) {
  158. System.out.println("Ur wrong:");
  159. e.printStackTrace();
  160. }
  161.  
  162. FileWriter out = new FileWriter(fileName);
  163.  
  164. double percent = (((double) score) / ((double) quest) * 100);
  165. out.write("Name: " + first + " " + last + "\n");
  166. out.write("Score: " + percent + "%\n");
  167. out.write("Elapsed time: " + time + "\n");
  168.  
  169. for (int i = 0; i < answeredQ.length; i++) {
  170. out.write(i + 1 + ". " + answeredQ[i]);
  171. if (answeredC[i] == answeredA[i]) {
  172. out.write(" - Correct!\n\n");
  173. } else {
  174. out.write(" - Wrong!\nThe correct answer is " + answeredC[i] + "\n\n");
  175. }
  176. }
  177. out.close();
  178. }
  179.  
  180. // fuck this method
  181. public static String printTime(long endDiff) {
  182. StringBuilder sb = new StringBuilder(64);
  183. long startValue = endDiff;
  184. long days = TimeUnit.MILLISECONDS.toDays(endDiff);
  185. endDiff = endDiff - TimeUnit.DAYS.toMillis(days);
  186. long hours = TimeUnit.MILLISECONDS.toHours(endDiff);
  187. endDiff = endDiff - TimeUnit.HOURS.toMillis(hours);
  188. long minutes = TimeUnit.MILLISECONDS.toMinutes(endDiff);
  189. endDiff = endDiff - TimeUnit.MINUTES.toMillis(minutes);
  190. long seconds = TimeUnit.MILLISECONDS.toSeconds(endDiff);
  191. if (startValue >= 86400000) {
  192. sb.append(days);
  193. sb.append(" Days, ");
  194. sb.append(hours);
  195. sb.append(" hours, ");
  196. sb.append(minutes);
  197. sb.append(" minutes and ");
  198. sb.append(seconds);
  199. sb.append(" seconds.");
  200. } else if (startValue >= 3600000) {
  201. sb.append(hours);
  202. sb.append(" hours, ");
  203. sb.append(minutes);
  204. sb.append(" minutes and ");
  205. sb.append(seconds);
  206. sb.append(" seconds.");
  207. } else if (startValue >= 60000) {
  208. sb.append(minutes);
  209. sb.append(" minutes and ");
  210. sb.append(seconds);
  211. sb.append(" seconds.");
  212. } else {
  213. sb.append(seconds);
  214. sb.append(" seconds.");
  215. }
  216. return (sb.toString());
  217. }
  218.  
  219. // Gets lines in a file
  220. public static int getLines(String file) throws IOException {
  221. BufferedReader reader = new BufferedReader(new FileReader(file));
  222. int x = 0;
  223. while (reader.readLine() != null) {
  224. x++;
  225. }
  226. reader.close();
  227.  
  228. return x;
  229. }
  230.  
  231. // Boolean login method | login(tries allowed, source file)
  232. public static Boolean login(int tries, String source) throws FileNotFoundException, IOException {
  233.  
  234. String[] loginInfo;
  235.  
  236. if (timesRan == 0) {
  237. for (int x = 0; x < tries; x++) {
  238. loginInfo = pack.promptLogin();
  239. for (int i = 0; i < getLines(source); i++) {
  240. StringTokenizer st = null;
  241. st = new StringTokenizer(read(i, source));
  242. if (st.nextToken().equals(loginInfo[0])) {
  243. if (st.nextToken().equals(loginInfo[1])) {
  244. username = loginInfo[0];
  245. firstName = st.nextToken();
  246. lastName = st.nextToken();
  247. return true;
  248. }
  249. }
  250. }
  251. JOptionPane.showMessageDialog(null, invalidLogin);
  252. }
  253. JOptionPane.showMessageDialog(null, tooManyAttempts);
  254. return false;
  255. } else {
  256. for (int x = 0; x < tries; x++) {
  257. loginInfo = pack.promptLogin();
  258. if (loginInfo[0].toLowerCase().equals("done")) {
  259. return false;
  260. }
  261. for (int i = 0; i < getLines(source); i++) {
  262. StringTokenizer st = null;
  263. st = new StringTokenizer(read(i, source));
  264. if (st.nextToken().equals(loginInfo[0])) {
  265. if (st.nextToken().equals(loginInfo[1])) {
  266. username = loginInfo[0];
  267. firstName = st.nextToken();
  268. lastName = st.nextToken();
  269. return true;
  270. }
  271. }
  272. }
  273. JOptionPane.showMessageDialog(null, invalidLogin);
  274. }
  275. JOptionPane.showMessageDialog(null, tooManyAttempts);
  276. return false;
  277. }
  278. }
  279.  
  280. public static String[] promptLogin() {
  281. JLabel jUserName = new JLabel("User Name");
  282. JTextField userName = new JTextField();
  283. JLabel jPassword = new JLabel("Password");
  284. JTextField password = new JPasswordField();
  285. String[] output = new String[2];
  286. Object[] ob = { jUserName, userName, jPassword, password };
  287. int result = JOptionPane.showConfirmDialog(null, ob,
  288. "Please input username and password.", JOptionPane.OK_CANCEL_OPTION);
  289.  
  290. if (result != JOptionPane.CANCEL_OPTION || result != JOptionPane.CLOSED_OPTION) {
  291. String userNameValue = userName.getText();
  292. String passwordValue = password.getText();
  293.  
  294. output[0] = userNameValue;
  295. output[1] = passwordValue;
  296. }
  297. return output;
  298. }
  299.  
  300. // Takes valid input and outputs it as a boolean
  301. public static Boolean input() {
  302. Boolean cont = true, ans = null;
  303.  
  304. do {
  305. String out = kb.nextLine().toLowerCase();
  306.  
  307. if (out.equals("t") || out.equals("true")) {
  308. ans = true;
  309. cont = false;
  310. } else if (out.equals("f") || out.equals("false")) {
  311. ans = false;
  312. cont = false;
  313. } else {
  314. p(invalidInput);
  315. }
  316. } while (cont);
  317.  
  318. return ans;
  319. }
  320.  
  321. // Method that returns a string with a question read(line#, File)
  322. public static String read(int line, String source) throws FileNotFoundException {
  323. File ansFile = new File(source);
  324. Scanner ans = new Scanner(ansFile);
  325. String out = null;
  326.  
  327. for (int i = 0; i <= line; i++) {
  328. if (i == line) {
  329. out = ans.nextLine();
  330. } else {
  331. ans.nextLine();
  332. }
  333. }
  334. ans.close();
  335. return out;
  336. }
  337.  
  338. // Parse
  339. public static void parse(String source, String dest, int sect) throws FileNotFoundException, IOException {
  340.  
  341. File f = new File(dest);
  342. if (!f.exists()) {
  343. FileWriter out = new FileWriter(dest);
  344. System.out.println(startParse);
  345. for (int x = 0; x < getLines(source); x++) {
  346. StringTokenizer st = new StringTokenizer(read(x, source));
  347. String[] make = new String[6];
  348. for (int i = 0; st.hasMoreTokens(); i++) {
  349. if (i == sect) {
  350. make[i] = passwd(10);
  351. } else {
  352. make[i] = st.nextToken();
  353. }
  354. }
  355. String strBuild = String.join(" ", make);
  356. out.write(strBuild + "\n");
  357. }
  358. out.close();
  359. }
  360. }
  361.  
  362. // Generates a random password passwd(char#)
  363. public static String passwd(int length) {
  364. String chars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789";
  365. StringBuilder strBldr = new StringBuilder(length);
  366. Random rand = new Random();
  367.  
  368. for (int i = 0; i < length; i++)
  369. strBldr.append(chars.charAt(rand.nextInt(chars.length())));
  370. return strBldr.toString();
  371. }
  372.  
  373. // Method that returns a boolean answer readA(line#, File)
  374. public static Boolean parseA(String source) {
  375. if (source.toLowerCase().equals("true")) {
  376. return true;
  377. } else {
  378. return false;
  379. }
  380. }
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement