Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package finalProject;
- import java.awt.*;
- import java.util.*;
- import java.io.*;
- import java.text.SimpleDateFormat;
- import java.util.concurrent.TimeUnit;
- import javax.swing.*;
- public class FinalProject {
- public static void main(String[] args) throws FileNotFoundException, IOException {
- JFrame frame = new JFrame();
- int failedLogins = 0;
- boolean logedIn = false;
- viewIntro();
- readPass(logedIn, frame, failedLogins);
- }
- private static boolean readPass(boolean logedIn, JFrame frame, int failedLogins) throws
- FileNotFoundException, IOException {
- //Puts userFile into a ArrayList for easy reading and comparing.
- File userFile = new File("UsersInfo_006.txt");
- while (!userFile.canRead()) {
- System.out.println("UserInfo can Not found, please make sure its in"
- + " the right directory.");
- break;
- }
- ArrayList<String> userList;
- try (Scanner uFile = new Scanner(userFile)) {
- userList = new ArrayList<>(); //Puts UserFile into arraylist
- while (uFile.hasNext()) {
- userList.add(uFile.nextLine());
- }
- uFile.close();
- } //Puts UserFile into arraylist
- //ends adding userfile to userList-Array List
- //Login Panel
- JPanel loginPanel = new JPanel(new BorderLayout(5, 5));
- JPanel label = new JPanel(new GridLayout(0, 1, 2, 2));
- label.add(new JLabel("Username:", SwingConstants.RIGHT));
- label.add(new JLabel("Password:", SwingConstants.RIGHT));
- loginPanel.add(label, BorderLayout.WEST);
- JPanel controls = new JPanel(new GridLayout(0, 1, 2, 2));
- JTextField username = new JTextField();
- controls.add(username);
- JPasswordField password = new JPasswordField();
- controls.add(password);
- loginPanel.add(controls, BorderLayout.CENTER);
- JOptionPane.showConfirmDialog(frame, loginPanel, "login",
- JOptionPane.OK_CANCEL_OPTION);
- //End Login Panel Creation
- String userName = username.getText();
- String passWord = new String(password.getPassword());
- //end login panel
- //begin comparing info from login panel to userinfo array
- int failedLogs = 0;
- for (int i = 0; i < userList.size(); i++) {
- if (userList.get(i).contains(userName) && userList.get(i).contains(passWord) && failedLogs <= 3) {
- System.out.println("I FOUND IT");
- String[] u = userList.get(i).split("\\t");
- String userNameA = u[0];
- String firstName = u[2];
- String lastName = u[3];
- quizTime(userNameA, firstName, lastName);
- }
- else {
- failedLogs++;
- }
- }
- System.out.println(logedIn);
- return false;
- }
- //End Login Method
- private static void quizTime(String userNameA, String firstName, String lastName)
- throws FileNotFoundException, IOException {
- //Quizes the User
- long startime = System.currentTimeMillis();
- File questFile = new File("TestBank.txt"); //Retrive Question Bank
- while (!questFile.canRead()) {
- JOptionPane.showMessageDialog(null, "Unable to find TestBank.txt");
- }
- ArrayList<String> qList;
- try (Scanner qFile = new Scanner(questFile) // Read Question Bank
- ) {
- qList = new ArrayList<>(); //Puts Question Bank into Array List
- while (qFile.hasNext()) {
- qList.add(qFile.nextLine());
- }
- } //Puts Question Bank into Array List
- File answrFile = new File("Answers.txt"); // Answer Bank
- while (!answrFile.canRead()) {
- JOptionPane.showMessageDialog(null, "Unable to find Answers.txt");
- }
- ArrayList<String> aList;
- try (Scanner aFile = new Scanner(answrFile)) {
- aList = new ArrayList<>(); //Throws answer bank into ArrayList
- while (aFile.hasNext()) {
- aList.add(aFile.nextLine());
- }
- } //Throws answer bank into ArrayList
- Random randNumb = new Random();
- int correct = 0;
- String test;
- for (int i = 0; i <= 10; i++) {
- int x = randNumb.nextInt(124) + 1;
- JOptionPane.showConfirmDialog(null, qList.get(x), "Quiz", JOptionPane.YES_NO_OPTION);
- if (JOptionPane.YES_OPTION == 0) {
- test = "TRUE";
- } else {
- test = "FALSE";
- }
- if (aList.get(x).contains(test)) {
- JOptionPane.showMessageDialog(null, "Correcto~!");
- correct++;
- } else {
- JOptionPane.showMessageDialog(null, "Incorrecto!~");
- }
- }
- long endtime = System.currentTimeMillis(); //End Time
- long dura = endtime - startime; //Get Duration in MILISEC
- long duration = TimeUnit.MILLISECONDS.toSeconds(dura); //Convert MILISEC to SEC
- String correcto = correct + "/10"; // Correct out of 10, used for FILE
- Date dNow = new Date(); //Date Object
- SimpleDateFormat fr = new SimpleDateFormat("yyyy M/D H:M"); //Gets Date Object Converted
- String date = fr.format(dNow); //Make Date Format easier for FILE Handling
- try (PrintWriter writer = new PrintWriter(userNameA + "_COSC_236_Quiz_" + date + ".txt" ) // Makes new File with userName_COSC_236_Quiz_Date_Time format hopefully
- ) {
- writer.print(firstName + " " + lastName);
- writer.println(correcto + " Correct");
- writer.println("You took this long: " + duration + " secs");
- writer.println(); //FIGURE OUT HOW TO • User’s answers and the correct answer
- writer.close();
- }
- System.out.println(firstName + " " + lastName);
- System.out.println(correcto + " Correct");
- System.out.println(duration + " Secs");
- System.out.println("");//FIGURE OUT HOW TO • User’s answers and the correct answer
- }
- private static void viewIntro() {
- JOptionPane.showMessageDialog(null, "Welcome to the Quiz App! Please have you're username and password ready!", "QuizTime", JOptionPane.INFORMATION_MESSAGE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement