SHARE
TWEET

Untitled

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