Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import javax.swing.JOptionPane;
  4. public class login {
  5. public static void main (String [] args) throws IOException {
  6. String usernameEntered;
  7. String passwordEntered;
  8.  
  9.  
  10. ArrayList<String> user = new ArrayList<String>();
  11. ArrayList<String> pass = new ArrayList<String>();
  12. ArrayList<String> first = new ArrayList<String>();
  13. ArrayList<String> last = new ArrayList<String>();
  14. File file = new File("UsersInfo.txt");
  15. Scanner inputFile = new Scanner(file);
  16. String fName="";
  17. String lName="";
  18.  
  19. int tries = 3;
  20. boolean usernameFlag = true;
  21. boolean passwordFlag = true;
  22. while (inputFile.hasNext())
  23. {
  24. String str = inputFile.nextLine();
  25. String usernameList = str.split("\t")[0];
  26. String passwordList = str.split("\t") [1];
  27. String firstName = str.split("\t") [2];
  28. String lastName = str.split("\t") [3];
  29. user.add(usernameList);
  30. pass.add(passwordList);
  31. first.add(firstName);
  32. last.add(lastName);
  33.  
  34. }
  35.  
  36. while(tries>0){
  37. usernameEntered = JOptionPane.showInputDialog("Enter your username");
  38. passwordEntered = JOptionPane.showInputDialog("Now enter your password");
  39. for (int i = 0; i < user.size(); i++){
  40. if (!usernameEntered.equals(user.get(i)))
  41. usernameFlag = false;}
  42.  
  43. for (int f = 0; f < pass.size(); f++){
  44. if (!passwordEntered.equals(pass.get(f)))
  45. passwordFlag = false;}
  46.  
  47. if ((passwordFlag = false) || (usernameFlag = false)){
  48. JOptionPane.showMessageDialog(null, "Error, you have " + tries + " tries left.");
  49. tries--;}
  50.  
  51. for (int b = 0; b < user.size(); b++)
  52. if(usernameEntered.equals(user.get(b)))
  53. {
  54. for(int j = 0; j < pass.size(); j++)
  55. {
  56. if(passwordEntered.equals(pass.get(j))) {
  57. JOptionPane.showMessageDialog(null, "Starting Quiz...");
  58. fName=first.get(j);
  59. lName=last.get(j);
  60. JOptionPane.showMessageDialog(null, "Welcome " + fName + " " + lName + "!");}
  61. tries = 0;
  62. }
  63. }
  64. }
  65. File quest = new File("TestBank.txt");
  66. File answ = new File("AnswBank.txt");
  67. Scanner inputFile2 = new Scanner(quest);
  68. Scanner inputFile3 = new Scanner(answ);
  69. Scanner keyboard = new Scanner(System.in);
  70. ArrayList<String> questions = new ArrayList<String>();
  71. ArrayList<String> answers = new ArrayList<String>();
  72. ArrayList<String> incorrect = new ArrayList<String>();
  73. ArrayList<String> enteredAnswer = new ArrayList<String>();
  74. while(inputFile2.hasNextLine())
  75. questions.add(inputFile2.nextLine());
  76. while(inputFile3.hasNextLine())
  77. answers.add(inputFile3.nextLine());
  78. Random rand = new Random();
  79. String userAnswer;
  80. int counter=0;
  81. wrong=0;
  82.  
  83. double begin=System.currentTimeMillis();
  84.  
  85. for(int i=0; i<10; i++){
  86. int x = rand.nextInt(questions.size());
  87. JOptionPane.showMessageDialog(null,questions.get(x));
  88. userAnswer=JOptionPane.showInputDialog("Enter your answer. (True or False)");
  89. enteredAnswer.get(x)=userAnswer;
  90. if((userAnswer.equals("t"))||(userAnswer.equals("T")))
  91. userAnswer="true";
  92. if((userAnswer.equals("f"))||(userAnswer.equals("F")))
  93. userAnswer="false";
  94. userAnswer=userAnswer.toUpperCase();
  95. if(((userAnswer.equals("TRUE"))||(userAnswer.equals("FALSE"))))
  96. {
  97. if(userAnswer.toUpperCase().equals(answers.get(x).toUpperCase())){
  98. JOptionPane.showMessageDialog(null,"Correct!");
  99. counter++;}
  100. else{
  101. JOptionPane.showMessageDialog(null,"Incorrect!");
  102. incorrect.get(wrong)=x;
  103. wrong++;
  104. }
  105. else{
  106. JOptionPane.showMessageDialog(null,"Please enter a valid answer.");
  107. JOptionPane.showMessageDialog(null,questions.get(x));
  108. userAnswer=JOptionPane.showInputDialog("Enter your answer. (True or False)");
  109. if((userAnswer.equals("t"))||(userAnswer.equals("T")))
  110. userAnswer="true";
  111. if((userAnswer.equals("f"))||(userAnswer.equals("F")))
  112. userAnswer="false";
  113. userAnswer=userAnswer.toUpperCase();
  114. if(userAnswer.toUpperCase().equals(answers.get(x).toUpperCase())){
  115. JOptionPane.showMessageDialog(null,"Correct!");
  116. counter++;}
  117. else{
  118. JOptionPane.showMessageDialog(null,"Incorrect!");
  119. incorrect.get(wrong)=x;
  120. wrong++;
  121. }
  122. }
  123. }
  124. double end=System.currentTimeMillis()-begin;
  125. end=end/1000.0;
  126. JOptionPane.showMessageDialog(null,"Name: " + fName + "" + lName + "\nIt took you " + String.format("%.0f",end) + " seconds to complete the test.\nScore: " + counter + " out of 10 correct.");
  127. JOptionPane.showMessageDialog(null,"Your Answer: " +enteredAnswers.get(incorrect.get(wrong))+"\tCorrect Answer: "+answers.get(incorrect.get(wrong));
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement