TheRightGuy

Advice?

Feb 17th, 2022
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. public class App {
  2.     public static void main(String[] args) {
  3.         QuizMaker quizMaker = new QuizMaker();
  4.         quizMaker.quiz();
  5.     }
  6. }
  7.  
  8. enum QuizRoles{
  9.     QUESTIONS("questions"),
  10.     ANSWER("answers");
  11.  
  12.     private final String role;
  13.  
  14.     QuizRoles(String role) {
  15.         this.role = role;
  16.     }
  17.  
  18.     String getRole() {
  19.         return role;
  20.     }
  21. }
  22.  
  23. public class QuizMaker {
  24.     private final ArrayList<String> questions = new ArrayList<>();
  25.     private final ArrayList<String> answers = new ArrayList<>();
  26.     private final Scanner input = new Scanner(System.in);
  27.     private int count = 0;
  28.  
  29.     public void quiz() {
  30.         retrieveQuizData(QuizRoles.QUESTIONS);
  31.         retrieveQuizData(QuizRoles.ANSWER);
  32.         quizGame();
  33.     }
  34.  
  35.     private void quizGame() {
  36.         for (int i = 0; i < 10; i++) {
  37.             int randomIndex = (int) (Math.random() * questions.size());
  38.             getQuestion(randomIndex);
  39.             checkInputMatch(randomIndex);
  40.         }
  41.         questionGrading();
  42.     }
  43.  
  44.     private void questionGrading() {
  45.         if (count < 5) {
  46.             System.out.println("You got " + count + "0% answered. Better luck next time.");
  47.         } else {
  48.             System.out.println("You got " + count + "0% answered. Good Job buddy.");
  49.         }
  50.     }
  51.  
  52.     private void checkInputMatch(int randomQuestionIndex) {
  53.         String answer = input.nextLine().toLowerCase();
  54.         if (answer.equals(getAnswer(randomQuestionIndex))) {
  55.             System.out.println("Correct answer");
  56.             count++;
  57.         }
  58.     }
  59.  
  60.     private void getQuestion(int randomQuestionIndex) {
  61.         System.out.println(questions.get(randomQuestionIndex));
  62.     }
  63.  
  64.     private String getAnswer(int questionIndex) {
  65.         return answers.get(questionIndex);
  66.     }
  67.  
  68.     private void retrieveQuizData(QuizRoles location) {
  69.         try {
  70.             BufferedReader bufferedReader = getBufferedReader(location.getRole());
  71.             if (location.equals(QuizRoles.QUESTIONS)) {
  72.                 addQuizValues(bufferedReader,questions);
  73.             } else {
  74.                 addQuizValues(bufferedReader,answers);
  75.             }
  76.         } catch (IOException e) {
  77.             throw new FileSystemNotFoundException("The file could not be found, check the file or adjust the location");
  78.         }
  79.     }
  80.  
  81.     private BufferedReader getBufferedReader(String location) throws FileNotFoundException {
  82.         File file = new File("src/main/resources/" + location + ".txt");
  83.         FileReader fileReader = new FileReader(file);
  84.         return new BufferedReader(fileReader);
  85.     }
  86.  
  87.     private void addQuizValues(BufferedReader bufferedReader, ArrayList<String> arrayList) throws IOException {
  88.         String line;
  89.         int MAX_LINE = 100;
  90.         for (int i = 0; i < MAX_LINE; i++) {
  91.             line = bufferedReader.readLine().toLowerCase(Locale.ROOT);
  92.             arrayList.add(line);
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment