Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class QuestionAndAnswers
- {
- public static ArrayList<ArrayList<String>> users;
- public static ArrayList<ArrayList<String>> topics;
- public static ArrayList<ArrayList<String>> questions;
- public static void main(String [] args) throws IOException
- {
- boolean readFile;
- boolean validUser = false;
- int topic = 0;
- int currentScore = 0;
- int attemptedAnswers = 0;
- int totalScore = 0;
- Scanner input = new Scanner(System.in);
- readFile = readToArrayLists();
- if(!readFile)
- System.out.println("One or more file do not exist.");
- else{
- validUser = usernamePassword(validUser);
- if(validUser)
- {
- System.out.println("Valid login \n");
- while(validUser)
- {
- topic = getTopic(topic);
- if(topic > 0 && topic <= topics.get(0).size())
- {
- System.out.println("Displaying questions on " + topics.get(1).get(topic-1) + ":\n");
- currentScore = askQuestions(currentScore, topic, attemptedAnswers);
- totalScore = getScore(totalScore, currentScore, attemptedAnswers);
- System.out.println("Your overall score is: " + totalScore + "\n");
- }
- }
- }
- else
- System.out.print("Out of attempts, please try again later.");
- }
- }
- public static boolean readToArrayLists() throws IOException
- {
- /* Takes the information in the files and puts them into different
- array lists. If all the files are present it returns true to the main method. */
- String filename1 = "Users.txt";
- String filename2 = "Topics.txt";
- String filename3 = "Questions.txt";
- String fileElements[];
- File inputFile1 = new File(filename1);
- File inputFile2 = new File(filename2);
- File inputFile3 = new File(filename3);
- users = new ArrayList<ArrayList<String>>();
- users.add(new ArrayList<String>());
- users.add(new ArrayList<String>());
- users.add(new ArrayList<String>());
- topics = new ArrayList<ArrayList<String>>();
- topics.add(new ArrayList<String>());
- topics.add(new ArrayList<String>());
- questions = new ArrayList<ArrayList<String>>();
- questions.add(new ArrayList<String>());
- questions.add(new ArrayList<String>());
- questions.add(new ArrayList<String>());
- questions.add(new ArrayList<String>());
- questions.add(new ArrayList<String>());
- questions.add(new ArrayList<String>());
- questions.add(new ArrayList<String>());
- questions.add(new ArrayList<String>());
- if(inputFile1.exists() && inputFile2.exists() && inputFile3.exists())
- {
- Scanner in;
- in = new Scanner(inputFile1);
- while(in.hasNext())
- {
- fileElements = (in.nextLine()).split(",");
- users.get(0).add(fileElements[0]);
- users.get(1).add(fileElements[1]);
- users.get(2).add(fileElements[2]);
- }
- in.close();
- in = new Scanner(inputFile2);
- while(in.hasNext())
- {
- fileElements = (in.nextLine()).split(",");
- topics.get(0).add(fileElements[0]);
- topics.get(1).add(fileElements[1]);
- }
- in.close();
- in = new Scanner(inputFile3);
- while(in.hasNext())
- {
- fileElements = (in.nextLine()).split(",");
- questions.get(0).add(fileElements[0]);
- questions.get(1).add(fileElements[1]);
- questions.get(2).add(fileElements[2]);
- questions.get(3).add(fileElements[3]);
- questions.get(4).add(fileElements[4]);
- questions.get(5).add(fileElements[5]);
- questions.get(6).add(fileElements[6]);
- questions.get(7).add(fileElements[7]);
- }
- in.close();
- return true;
- }
- else
- return false;
- }
- public static boolean usernamePassword(boolean validUser)
- {
- /* Accepts an input for the username, checks if the input matches the username in the array list,
- if it matches it takes in input for the password and does the same,
- if both the username and password are present and they are indexed in the same row
- the method returns true, else it returns false. */
- validUser = false;
- boolean validUsername = false, validPassword = false;
- int number, attempts = 0;
- boolean userIndex, passwordIndex;
- int userNumber, passwordNumber;
- String userInput, passwordInput;
- while(attempts <3 && validUser == false)
- {
- validUsername = false;
- validPassword = false;
- validUser = false;
- userNumber = 0;
- passwordNumber = 0;
- userIndex = false;
- passwordIndex = false;
- userInput = "";
- passwordInput = "";
- Scanner in = new Scanner(System.in);
- Scanner inn = new Scanner(System.in);
- System.out.print("Please enter your username: ");
- userInput = in.nextLine();
- for(int i = 0; i<= users.get(0).size()-1 && validUsername == false; i++)
- {
- userIndex = users.get(0).get(i).matches(userInput);
- if(userIndex)
- { userNumber = i;
- validUsername = true;
- System.out.print("Please enter your password: ");
- passwordInput = inn.nextLine();
- for(int j = 0; j<= users.get(1).size()-1 && validPassword == false; j++)
- {
- passwordIndex = users.get(1).get(j).matches(passwordInput);
- if(passwordIndex)
- {passwordNumber = j;
- validPassword = true;}
- }
- }
- else
- validUsername = false;
- }
- if(validUsername == true && validPassword == true && userIndex == passwordIndex && userNumber == passwordNumber)
- {
- validUser = true;
- return validUser;
- }
- else
- {
- validUser = false;
- }
- if(validUser == false)
- System.out.println("Invalid username or password");
- attempts++;
- }
- return validUser;
- }
- public static int getTopic(int topic)
- { /* Puts the topic and question numbers into arrays.
- Using a nested for loop it checks that the topic has at least one
- question available before displaying it.
- It then takes an input for the topic number, checks that the input
- is in the right range and is numeric and returns the topic number to the main method */
- int q[] = new int[questions.get(0).size()];
- int t[] = new int[topics.get(0).size()];
- boolean displayed = false;
- for(int x = 0; x <= questions.get(0).size()-1; x++)
- {
- q[x] = Integer.parseInt(questions.get(0).get(x));
- }
- for(int y = 0; y <= topics.get(0).size()-1; y++)
- {
- t[y] = Integer.parseInt(topics.get(0).get(y));
- }
- for(int i = 0; i<= topics.get(0).size()-1; i++)
- {
- displayed = false;
- for(int j = 0; j <= questions.get(0).size()-1 && displayed == false; j++ )
- {
- if(t[i] == q[j])
- {
- System.out.println("\t" + topics.get(0).get(i) + " " + topics.get(1).get(i));
- displayed = true;
- }
- }
- }
- try
- {
- Scanner in = new Scanner(System.in);
- System.out.print("\nPlease enter the topic number: ");
- topic = in.nextInt();
- }
- catch(InputMismatchException anError)
- {
- System.out.println("\nNumeric input only");
- }
- if(topic > 0 && topic <= topics.get(0).size())
- {
- return topic;
- }
- else
- System.out.println("\nInvalid input");
- return topic;
- }
- public static int askQuestions(int currentScore, int topic, int attemptedAnswers)
- {
- /* Puts all the questions relating to the chosen topic in an array list, if there are more than 10 questions
- it will randomly choose 10, if there are 10 or less it will use all available questions.
- It then displays the questions one at a time and allows input for answers. If the answer is correct
- it will move on, if the answer is wrong it will display the right answer and an explanation.
- It also keeps count of the amount you got correct and your attempts which are then displayed.
- */
- int answer = 0;
- ArrayList<ArrayList<String>> currentQuestions;
- currentQuestions = new ArrayList<ArrayList<String>>();
- currentQuestions.add(new ArrayList<String>());
- currentQuestions.add(new ArrayList<String>());
- currentQuestions.add(new ArrayList<String>());
- currentQuestions.add(new ArrayList<String>());
- currentQuestions.add(new ArrayList<String>());
- currentQuestions.add(new ArrayList<String>());
- currentQuestions.add(new ArrayList<String>());
- for(int i = 0; i <= questions.get(0).size()-1; i++)
- {
- if(topic == Integer.parseInt(questions.get(0).get(i)))
- {
- int row = 0;
- for(int j=0; j<=6; j++)
- {
- currentQuestions.get(j).add(row, questions.get(j+1).get(i));
- }
- row++;
- }
- }
- if(currentQuestions.get(0).size() <=10)
- {
- for(int g = 0; g < 10 && g<= currentQuestions.get(0).size()-1; g++)
- {
- try
- {
- Scanner in = new Scanner(System.in);
- System.out.println(currentQuestions.get(0).get(g));
- for(int h = 1; h <= 4; h++)
- {
- System.out.println("" + h + "." + currentQuestions.get(h).get(g));
- }
- answer = in.nextInt();
- }
- catch(InputMismatchException anError)
- {
- System.out.println("\nNumeric input only");
- }
- if(answer == Integer.parseInt(currentQuestions.get(5).get(g)))
- {
- System.out.println("\nCorrect answer \n");
- currentScore++;
- attemptedAnswers++;
- }
- else
- {
- System.out.println("Incorrect answer\n \nCorrect answer is: " + currentQuestions.get(Integer.parseInt(currentQuestions.get(5).get(g))).get(g) + "\nReason: " + currentQuestions.get(6).get(g) + "\n");
- attemptedAnswers++;
- }
- }
- }
- else
- {
- int count[] = new int[10];
- boolean duplicate = false;
- for(int j = 0; j<= count.length-1; j++ )
- {
- duplicate = false;
- count[j] = (int)(Math.random()*(currentQuestions.get(0).size()-1));
- for(int k = j+1; k <= count.length-1 && duplicate == false; k++)
- {
- if(count[j]==count[k])
- {
- duplicate = true;
- }
- }
- if(duplicate == true)
- j--;
- }
- for(int g = 0; g <= count.length-1; g++)
- {
- try
- {
- Scanner in = new Scanner(System.in);
- System.out.println(currentQuestions.get(0).get(count[g]));
- for(int h = 1; h <= 4; h++)
- {
- System.out.println("" + h + "." + currentQuestions.get(h).get(count[g]));
- }
- answer = in.nextInt();
- }
- catch(InputMismatchException anError)
- {
- System.out.println("\nNumeric input only");
- }
- if(answer == Integer.parseInt(currentQuestions.get(5).get(count[g])))
- {
- System.out.println("\nCorrect answer \n");
- currentScore++;
- attemptedAnswers++;
- }
- else
- {
- System.out.println("Incorrect answer\n \nCorrect answer is: " + currentQuestions.get(Integer.parseInt(currentQuestions.get(5).get(count[g]))).get(count[g]) + "\nReason: " + currentQuestions.get(6).get(count[g]) + "\n");
- attemptedAnswers++;
- }
- }
- }
- System.out.println("Your score for this session is: " + currentScore +"/"+ attemptedAnswers + "\n");
- return currentScore;
- }
- public static int getScore(int totalScore, int currentScore, int attemptedAnswers)
- {
- // Adds the current score to the total score //
- totalScore += currentScore;
- users.get(0).set(1, Integer.toString(totalScore));
- return totalScore;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement