Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import javax.swing.JOptionPane;
- public class login {
- public static void main (String [] args) throws IOException {
- String usernameEntered;
- String passwordEntered;
- ArrayList<String> user = new ArrayList<String>();
- ArrayList<String> pass = new ArrayList<String>();
- ArrayList<String> first = new ArrayList<String>();
- ArrayList<String> last = new ArrayList<String>();
- File file = new File("UsersInfo.txt");
- Scanner inputFile = new Scanner(file);
- String fName="";
- String lName="";
- int tries = 3;
- boolean usernameFlag = true;
- boolean passwordFlag = true;
- while (inputFile.hasNext())
- {
- String str = inputFile.nextLine();
- String usernameList = str.split("\t")[0];
- String passwordList = str.split("\t") [1];
- String firstName = str.split("\t") [2];
- String lastName = str.split("\t") [3];
- user.add(usernameList);
- pass.add(passwordList);
- first.add(firstName);
- last.add(lastName);
- }
- while(tries>0){
- usernameEntered = JOptionPane.showInputDialog("Enter your username");
- passwordEntered = JOptionPane.showInputDialog("Now enter your password");
- for (int i = 0; i < user.size(); i++){
- if (!usernameEntered.equals(user.get(i)))
- usernameFlag = false;}
- for (int f = 0; f < pass.size(); f++){
- if (!passwordEntered.equals(pass.get(f)))
- passwordFlag = false;}
- if ((passwordFlag = false) || (usernameFlag = false)){
- JOptionPane.showMessageDialog(null, "Error, you have " + tries + " tries left.");
- tries--;}
- for (int b = 0; b < user.size(); b++)
- if(usernameEntered.equals(user.get(b)))
- {
- for(int j = 0; j < pass.size(); j++)
- {
- if(passwordEntered.equals(pass.get(j))) {
- JOptionPane.showMessageDialog(null, "Starting Quiz...");
- fName=first.get(j);
- lName=last.get(j);
- JOptionPane.showMessageDialog(null, "Welcome " + fName + " " + lName + "!");}
- tries = 0;
- }
- }
- }
- File quest = new File("TestBank.txt");
- File answ = new File("AnswBank.txt");
- Scanner inputFile2 = new Scanner(quest);
- Scanner inputFile3 = new Scanner(answ);
- Scanner keyboard = new Scanner(System.in);
- ArrayList<String> questions = new ArrayList<String>();
- ArrayList<String> answers = new ArrayList<String>();
- ArrayList<String> incorrect = new ArrayList<String>();
- ArrayList<String> enteredAnswer = new ArrayList<String>();
- while(inputFile2.hasNextLine())
- questions.add(inputFile2.nextLine());
- while(inputFile3.hasNextLine())
- answers.add(inputFile3.nextLine());
- Random rand = new Random();
- String userAnswer;
- int counter=0;
- wrong=0;
- double begin=System.currentTimeMillis();
- for(int i=0; i<10; i++){
- int x = rand.nextInt(questions.size());
- JOptionPane.showMessageDialog(null,questions.get(x));
- userAnswer=JOptionPane.showInputDialog("Enter your answer. (True or False)");
- enteredAnswer.get(x)=userAnswer;
- if((userAnswer.equals("t"))||(userAnswer.equals("T")))
- userAnswer="true";
- if((userAnswer.equals("f"))||(userAnswer.equals("F")))
- userAnswer="false";
- userAnswer=userAnswer.toUpperCase();
- if(((userAnswer.equals("TRUE"))||(userAnswer.equals("FALSE"))))
- {
- if(userAnswer.toUpperCase().equals(answers.get(x).toUpperCase())){
- JOptionPane.showMessageDialog(null,"Correct!");
- counter++;}
- else{
- JOptionPane.showMessageDialog(null,"Incorrect!");
- incorrect.get(wrong)=x;
- wrong++;
- }
- else{
- JOptionPane.showMessageDialog(null,"Please enter a valid answer.");
- JOptionPane.showMessageDialog(null,questions.get(x));
- userAnswer=JOptionPane.showInputDialog("Enter your answer. (True or False)");
- if((userAnswer.equals("t"))||(userAnswer.equals("T")))
- userAnswer="true";
- if((userAnswer.equals("f"))||(userAnswer.equals("F")))
- userAnswer="false";
- userAnswer=userAnswer.toUpperCase();
- if(userAnswer.toUpperCase().equals(answers.get(x).toUpperCase())){
- JOptionPane.showMessageDialog(null,"Correct!");
- counter++;}
- else{
- JOptionPane.showMessageDialog(null,"Incorrect!");
- incorrect.get(wrong)=x;
- wrong++;
- }
- }
- }
- double end=System.currentTimeMillis()-begin;
- end=end/1000.0;
- JOptionPane.showMessageDialog(null,"Name: " + fName + "" + lName + "\nIt took you " + String.format("%.0f",end) + " seconds to complete the test.\nScore: " + counter + " out of 10 correct.");
- JOptionPane.showMessageDialog(null,"Your Answer: " +enteredAnswers.get(incorrect.get(wrong))+"\tCorrect Answer: "+answers.get(incorrect.get(wrong));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement