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;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
- public class pack {
- // Program wide variables
- static Boolean debug = false; // True for debug mode
- 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 loginPromptUserDone = "Please enter your username or done to end the program.";
- static String loginPromptPass = "Please enter your password.";
- static String startParse = "Parsing your users file.";
- static String enterDone = "Enter done to quit.";
- static String testBankFile = "TestBank.txt";
- static String originalUsersFile = "UsersInfo.txt";
- static String newUsersFile = "NewUsers.txt";
- static String answersFile = "Answers.txt";
- static String firstName;
- static String lastName;
- static String username;
- static int quest = 4;
- static int testBankQ = 124;
- static int timesRan = 0;
- 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();
- Boolean run = true;
- int count = 0, right = 0, wrong = 0;
- // Debug false
- if (!debug) {
- while (run) {
- parse(originalUsersFile, newUsersFile, 1);
- if (login(3, newUsersFile)) { // Check login creds
- long startTime = System.currentTimeMillis();
- while (count < quest) {
- int r = rand.nextInt(testBankQ);
- int ansInt;
- boolean ans;
- while(1>0) {
- ansInt = promptQ(read(r, testBankFile), count+1);
- if (ansInt == 0) {
- ans = true;
- break;
- } else if(ansInt == 1) {
- ans = false;
- }
- }
- answeredQ[count] = read(r, testBankFile);
- answeredA[count] = ans;
- answeredC[count] = parseA(read(r, answersFile));
- if (ans != parseA(read(r, answersFile))) {
- wrong++;
- } else if (ans == parseA(read(r, answersFile))) {
- right++;
- }
- count++;
- }
- // Wrapping it all up
- JOptionPane.showMessageDialog(null, "You got " + wrong + " wrong and " + right + " correct.");
- long endDiff = (System.currentTimeMillis() - startTime);
- makeReport(firstName, lastName, username, printTime(endDiff), right);
- timesRan++;
- } else {
- close();
- }
- while (count == quest) {
- if (login(3, newUsersFile)) {
- count = 0;
- } else {
- close();
- }
- }
- }
- // Debug true
- } else {
- // Insert code here
- int ans2 = promptQ(read(2, testBankFile), count+1);
- if(ans2 == 2) {
- p("null");
- } else if (ans2 == 1) {
- p("false");
- } else {
- p("true");
- }
- }
- }
- public static int promptQ(String question, int number) {
- Object[] options = { "True", "False" };
- int n = JOptionPane.showOptionDialog(null, question, "Question " + Integer.toString(number),
- JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, // do not use a custom Icon
- options,
- options[0]);
- if(n == 0) {
- return 0;
- } else if (n == 1) {
- return 1;
- }
- return 2;
- }
- // 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 IOException {
- String fileName = user + "_COSC236_Quiz_" + dateFormat.format(date) + ".txt";
- try {
- File file = new File(fileName);
- file.createNewFile();
- } catch (IOException e) {
- System.out.println("Ur wrong:");
- e.printStackTrace();
- }
- FileWriter out = new FileWriter(fileName);
- double percent = (((double) score) / ((double) quest) * 100);
- out.write("Name: " + first + " " + last + "\n");
- out.write("Score: " + percent + "%\n");
- out.write("Elapsed time: " + time + "\n");
- for (int i = 0; i < answeredQ.length; i++) {
- out.write(i + 1 + ". " + answeredQ[i]);
- if (answeredC[i] == answeredA[i]) {
- out.write(" - Correct!\n\n");
- } else {
- out.write(" - Wrong!\nThe correct answer is " + answeredC[i] + "\n\n");
- }
- }
- out.close();
- }
- // 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 {
- String[] loginInfo;
- if (timesRan == 0) {
- for (int x = 0; x < tries; x++) {
- loginInfo = pack.promptLogin();
- for (int i = 0; i < getLines(source); i++) {
- StringTokenizer st = null;
- st = new StringTokenizer(read(i, source));
- if (st.nextToken().equals(loginInfo[0])) {
- if (st.nextToken().equals(loginInfo[1])) {
- username = loginInfo[0];
- firstName = st.nextToken();
- lastName = st.nextToken();
- return true;
- }
- }
- }
- JOptionPane.showMessageDialog(null, invalidLogin);
- }
- JOptionPane.showMessageDialog(null, tooManyAttempts);
- return false;
- } else {
- for (int x = 0; x < tries; x++) {
- loginInfo = pack.promptLogin();
- if (loginInfo[0].toLowerCase().equals("done")) {
- return false;
- }
- for (int i = 0; i < getLines(source); i++) {
- StringTokenizer st = null;
- st = new StringTokenizer(read(i, source));
- if (st.nextToken().equals(loginInfo[0])) {
- if (st.nextToken().equals(loginInfo[1])) {
- username = loginInfo[0];
- firstName = st.nextToken();
- lastName = st.nextToken();
- return true;
- }
- }
- }
- JOptionPane.showMessageDialog(null, invalidLogin);
- }
- JOptionPane.showMessageDialog(null, tooManyAttempts);
- return false;
- }
- }
- public static String[] promptLogin() {
- JLabel jUserName = new JLabel("User Name");
- JTextField userName = new JTextField();
- JLabel jPassword = new JLabel("Password");
- JTextField password = new JPasswordField();
- String[] output = new String[2];
- Object[] ob = { jUserName, userName, jPassword, password };
- int result = JOptionPane.showConfirmDialog(null, ob,
- "Please input username and password.", JOptionPane.OK_CANCEL_OPTION);
- if (result != JOptionPane.CANCEL_OPTION || result != JOptionPane.CLOSED_OPTION) {
- String userNameValue = userName.getText();
- String passwordValue = password.getText();
- output[0] = userNameValue;
- output[1] = passwordValue;
- }
- return output;
- }
- // 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 {
- File f = new File(dest);
- if (!f.exists()) {
- FileWriter out = new FileWriter(dest);
- System.out.println(startParse);
- 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 strBuild = String.join(" ", make);
- out.write(strBuild + "\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