Advertisement
Guest User

Untitled

a guest
Nov 5th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.49 KB | None | 0 0
  1. package finalProject;
  2.  
  3. import java.awt.*;
  4. import java.util.*;
  5. import java.io.*;
  6. import java.text.SimpleDateFormat;
  7. import java.util.concurrent.TimeUnit;
  8. import javax.swing.*;
  9.  
  10. public class FinalProject {
  11.  
  12.     public static void main(String[] args) throws FileNotFoundException, IOException {
  13.  
  14.         JFrame frame = new JFrame();
  15.         int failedLogins = 0;
  16.         boolean logedIn = false;
  17.        
  18.         viewIntro();
  19.         readPass(logedIn, frame, failedLogins);
  20.        
  21.  
  22.     }
  23.  
  24.     private static boolean readPass(boolean logedIn, JFrame frame, int failedLogins) throws
  25.             FileNotFoundException, IOException {
  26.  
  27.         //Puts userFile into a ArrayList for easy reading and comparing.
  28.         File userFile = new File("UsersInfo_006.txt");
  29.         while (!userFile.canRead()) {
  30.             System.out.println("UserInfo can Not found, please make sure its in"
  31.                     + " the right directory.");
  32.             break;
  33.         }
  34.         ArrayList<String> userList;
  35.         try (Scanner uFile = new Scanner(userFile)) {
  36.             userList = new ArrayList<>(); //Puts UserFile into arraylist
  37.             while (uFile.hasNext()) {
  38.                 userList.add(uFile.nextLine());
  39.             }
  40.             uFile.close();
  41.         } //Puts UserFile into arraylist
  42.         //ends adding userfile to userList-Array List
  43.         //Login Panel
  44.         JPanel loginPanel = new JPanel(new BorderLayout(5, 5));
  45.  
  46.         JPanel label = new JPanel(new GridLayout(0, 1, 2, 2));
  47.         label.add(new JLabel("Username:", SwingConstants.RIGHT));
  48.         label.add(new JLabel("Password:", SwingConstants.RIGHT));
  49.         loginPanel.add(label, BorderLayout.WEST);
  50.  
  51.         JPanel controls = new JPanel(new GridLayout(0, 1, 2, 2));
  52.         JTextField username = new JTextField();
  53.         controls.add(username);
  54.         JPasswordField password = new JPasswordField();
  55.         controls.add(password);
  56.         loginPanel.add(controls, BorderLayout.CENTER);
  57.  
  58.         JOptionPane.showConfirmDialog(frame, loginPanel, "login",
  59.                 JOptionPane.OK_CANCEL_OPTION);
  60.         //End Login Panel Creation
  61.         String userName = username.getText();
  62.         String passWord = new String(password.getPassword());
  63.         //end login panel
  64.         //begin comparing info from login panel to userinfo array
  65.         int failedLogs = 0;
  66.         for (int i = 0; i < userList.size(); i++) {
  67.  
  68.             if (userList.get(i).contains(userName) && userList.get(i).contains(passWord) && failedLogs <= 3) {
  69.                 System.out.println("I FOUND IT");
  70.                 String[] u = userList.get(i).split("\\t");
  71.                 String userNameA = u[0];
  72.                 String firstName = u[2];
  73.                 String lastName = u[3];
  74.                
  75.                 quizTime(userNameA, firstName, lastName);
  76.             }
  77.             else {
  78.                 failedLogs++;
  79.             }
  80.  
  81.         }
  82.         System.out.println(logedIn);
  83.         return false;
  84.     }
  85.  
  86.     //End Login Method
  87.     private static void quizTime(String userNameA, String firstName, String lastName)
  88.             throws FileNotFoundException, IOException {
  89.         //Quizes the User
  90.         long startime = System.currentTimeMillis();
  91.         File questFile = new File("TestBank.txt"); //Retrive Question Bank
  92.         while (!questFile.canRead()) {
  93.             JOptionPane.showMessageDialog(null, "Unable to find TestBank.txt");
  94.         }
  95.         ArrayList<String> qList;
  96.         try (Scanner qFile = new Scanner(questFile) // Read Question Bank
  97.                 ) {
  98.             qList = new ArrayList<>(); //Puts Question Bank into Array List
  99.             while (qFile.hasNext()) {
  100.                 qList.add(qFile.nextLine());
  101.             }
  102.         } //Puts Question Bank into Array List
  103.         File answrFile = new File("Answers.txt"); // Answer Bank
  104.         while (!answrFile.canRead()) {
  105.             JOptionPane.showMessageDialog(null, "Unable to find Answers.txt");
  106.         }
  107.  
  108.         ArrayList<String> aList;
  109.         try (Scanner aFile = new Scanner(answrFile)) {
  110.             aList = new ArrayList<>(); //Throws answer bank into ArrayList
  111.             while (aFile.hasNext()) {
  112.                 aList.add(aFile.nextLine());
  113.             }
  114.         } //Throws answer bank into ArrayList
  115.         Random randNumb = new Random();
  116.         int correct = 0;
  117.         String test;
  118.         for (int i = 0; i <= 10; i++) {
  119.             int x = randNumb.nextInt(124) + 1;
  120.             JOptionPane.showConfirmDialog(null, qList.get(x), "Quiz", JOptionPane.YES_NO_OPTION);
  121.  
  122.             if (JOptionPane.YES_OPTION == 0) {
  123.                 test = "TRUE";
  124.             } else {
  125.                 test = "FALSE";
  126.             }
  127.             if (aList.get(x).contains(test)) {
  128.                 JOptionPane.showMessageDialog(null, "Correcto~!");
  129.                 correct++;
  130.             } else {
  131.                 JOptionPane.showMessageDialog(null, "Incorrecto!~");
  132.             }
  133.         }
  134.         long endtime = System.currentTimeMillis(); //End Time
  135.         long dura = endtime - startime; //Get Duration in MILISEC
  136.         long duration = TimeUnit.MILLISECONDS.toSeconds(dura); //Convert MILISEC to SEC
  137.         String correcto = correct + "/10"; // Correct out of 10, used for FILE
  138.        
  139.         Date dNow = new Date(); //Date Object
  140.         SimpleDateFormat fr = new SimpleDateFormat("yyyy M/D H:M"); //Gets Date Object Converted
  141.         String date = fr.format(dNow); //Make Date Format easier for FILE Handling
  142.        
  143.        
  144.         try (PrintWriter writer = new PrintWriter(userNameA + "_COSC_236_Quiz_" + date + ".txt" ) // Makes new File with userName_COSC_236_Quiz_Date_Time format hopefully
  145.         ) {
  146.             writer.print(firstName + " " + lastName);
  147.             writer.println(correcto + " Correct");
  148.             writer.println("You took this long: " + duration + " secs");
  149.             writer.println(); //FIGURE OUT HOW TO •   User’s answers and the correct answer
  150.             writer.close();
  151.            
  152.         }
  153.        
  154.         System.out.println(firstName + " " + lastName);
  155.         System.out.println(correcto + " Correct");
  156.         System.out.println(duration + " Secs");
  157.         System.out.println("");//FIGURE OUT HOW TO •  User’s answers and the correct answer
  158.        
  159.     }
  160.  
  161.     private static void viewIntro() {
  162.         JOptionPane.showMessageDialog(null, "Welcome to the Quiz App! Please have you're username and password ready!", "QuizTime", JOptionPane.INFORMATION_MESSAGE);
  163.     }
  164.    
  165.    
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement