Advertisement
Guest User

Untitled

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