Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Random;
- import java.util.Scanner;
- import java.util.StringTokenizer;
- import java.util.concurrent.TimeUnit;
- public class pack {
- //Program wide variables
- static Boolean debug = false; //True for debug mode
- static Boolean ranBefore = true; //Set to false if running for the first time
- static String invalidInput = "Please only input true/false or t/f";
- static String invalidLogin = "Sorry that username or password is not correct, please try again.";
- static String tooManyAttempts = "Sorry you have exceeded your 3 trys! Try again later.";
- static String loginPromptUser = "Please enter your username.";
- static String loginPromptPass = "Please enter your password.";
- static String firstName;
- static String lastName;
- static String username;
- static int quest = 4;
- static int testBankQ = 124;
- static String[] answeredQ = new String[quest];
- static Boolean[] answeredA = new Boolean[quest];
- static Boolean[] answeredC = new Boolean[quest];
- //idk what these are called
- static DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy_HH-mm");
- static Date date = new Date();
- static Scanner kb = new Scanner(System.in);
- //magic
- public static void main(String Args[]) throws IOException {
- Random rand = new Random();
- int count = 0, right = 0, wrong = 0;
- //Debug false
- if(!debug) {
- if(login(3, "NewUsers.txt")) { //Check login creds
- long startTime = System.currentTimeMillis();
- while (count < quest) {
- if(!ranBefore) {
- parse("UsersInfo.txt", "NewUsers.txt", 1);
- }
- int r = rand.nextInt(testBankQ);
- p(count + 1 + ". " + read(r, "TestBank.txt"));
- boolean ans = input();
- answeredQ[count] = read(r, "TestBank.txt");
- answeredA[count] = ans;
- answeredC[count] = parseA(read(r, "Answers.txt"));
- if(ans != parseA(read(r, "Answers.txt"))) {
- wrong++;
- }else if(ans == parseA(read(r, "Answers.txt"))) {
- right++;
- }
- count++;
- }
- //Wraping it all up
- p("You got " + wrong + " wrong and " + right + " correct.");
- long endDiff = (System.currentTimeMillis()-startTime);
- makeReport(firstName, lastName, username, printTime(endDiff), count);
- } else {
- close();
- }
- }
- //Debug true
- else {
- //Insert code here
- }
- }
- //Shortens System.out.println() to p()
- public static String p(String in) {
- System.out.println(in);
- return in;
- }
- //Close the program
- public static void close(){
- System.exit(0);
- }
- //Generates a report report(first, last, score, time, array of answers)
- public static void makeReport(String first, String last, String user, String time, int score) throws FileNotFoundException {
- double percent = (score/quest)*100;
- p("Name: " + first + " " + last);
- System.out.printf("Score: %.0f\n", percent);
- p("Elapsed time: " + time);
- String fileName = user + "_COSC_236_Quiz_" + dateFormat.format(date);
- try{
- File file = new File(fileName);
- file.createNewFile();
- } catch (IOException e) {
- System.out.println("Exception Occurred:");
- e.printStackTrace();
- }
- for(int i = 0; i < answeredQ.length; i++) {
- System.out.print(i+1 + ". " + answeredQ[i]);
- if(answeredC[i] == answeredA[i]) {
- System.out.print(" - Correct!\n\n");
- } else {
- System.out.print(" - Wrong!\nThe correct answer is " + answeredC[i] + "\n\n");
- }
- }
- }
- //fuck this method
- public static String printTime(long endDiff) {
- StringBuilder sb = new StringBuilder(64);
- long startValue = endDiff;
- long days = TimeUnit.MILLISECONDS.toDays(endDiff);
- endDiff = endDiff - TimeUnit.DAYS.toMillis(days);
- long hours = TimeUnit.MILLISECONDS.toHours(endDiff);
- endDiff = endDiff - TimeUnit.HOURS.toMillis(hours);
- long minutes = TimeUnit.MILLISECONDS.toMinutes(endDiff);
- endDiff = endDiff - TimeUnit.MINUTES.toMillis(minutes);
- long seconds = TimeUnit.MILLISECONDS.toSeconds(endDiff);
- if(startValue >= 86400000) {
- sb.append(days);
- sb.append(" Days, ");
- sb.append(hours);
- sb.append(" hours, ");
- sb.append(minutes);
- sb.append(" minutes and ");
- sb.append(seconds);
- sb.append(" seconds.");
- } else if (startValue >= 3600000) {
- sb.append(hours);
- sb.append(" hours, ");
- sb.append(minutes);
- sb.append(" minutes and ");
- sb.append(seconds);
- sb.append(" seconds.");
- } else if (startValue >= 60000) {
- sb.append(minutes);
- sb.append(" minutes and ");
- sb.append(seconds);
- sb.append(" seconds.");
- } else {
- sb.append(seconds);
- sb.append(" seconds.");
- }
- return(sb.toString());
- }
- //Gets lines in a file
- public static int getLines(String file) throws IOException {
- BufferedReader reader = new BufferedReader(new FileReader(file));
- int x = 0;
- while (reader.readLine() != null) {
- x++;
- }
- reader.close();
- return x;
- }
- //Boolean login method | login(tries allowed, source file)
- public static Boolean login(int tries, String source) throws FileNotFoundException, IOException {
- for(int x = 0; x < tries; x++) {
- p(loginPromptUser);
- String user = kb.next();
- p(loginPromptPass);
- String pass = kb.next();
- for(int i = 0; i < getLines(source); i++) {
- StringTokenizer st = null;
- st = new StringTokenizer(read(i, source));
- if(st.nextToken().equals(user)) {
- if (st.nextToken().equals(pass)) {
- username = user;
- firstName = st.nextToken();
- lastName = st.nextToken();
- return true;
- }
- }
- }
- p(invalidLogin);
- }
- p(tooManyAttempts);
- return false;
- }
- //Takes valid input and outputs it as a boolean
- public static Boolean input() {
- Boolean cont = true, ans = null;
- do {
- String out = kb.nextLine().toLowerCase();
- if(out.equals("t") || out.equals("true")) {
- ans = true;
- cont = false;
- } else if(out.equals("f") || out.equals("false")) {
- ans = false;
- cont = false;
- } else {
- p(invalidInput);
- }
- }while(cont);
- return ans;
- }
- //Method that returns a string with a question read(line#, File)
- public static String read(int line, String source) throws FileNotFoundException {
- File ansFile = new File(source);
- Scanner ans = new Scanner(ansFile);
- String out = null;
- for(int i = 0; i <= line; i++) {
- if (i == line) {
- out = ans.nextLine();
- } else {
- ans.nextLine();
- }
- }
- ans.close();
- return out;
- }
- //Parse
- public static void parse(String source, String dest, int sect) throws FileNotFoundException, IOException {
- FileWriter out = new FileWriter(dest);
- for(int x = 0; x < getLines(source); x++) {
- StringTokenizer st = new StringTokenizer(read(x, source));
- String[] make = new String[6];
- for(int i = 0; st.hasMoreTokens(); i++) {
- if(i==sect) {
- make[i] = passwd(10);
- } else {
- make[i] = st.nextToken();
- }
- }
- String strg = String.join(" ", make);
- p(strg + "\n");
- out.write(strg + "\n");
- }
- out.close();
- }
- //Generates a random password passwd(char#)
- public static String passwd(int length){
- String chars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789";
- StringBuilder strBldr = new StringBuilder(length);
- Random rand = new Random();
- for(int i = 0; i < length; i++)
- strBldr.append(chars.charAt(rand.nextInt(chars.length())));
- return strBldr.toString();
- }
- //Method that returns a boolean answer readA(line#, File)
- public static Boolean parseA(String source) {
- if(source.toLowerCase().equals("true")) {
- return true;
- } else {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement