The Program shall:
Note: Cases are ignored. Lowercase letters are acceptable (a, b, c).
Create a try-catch structure to handle three (3) exceptions. These are when the user inputs the following:
Prompt the user that he can answer again if any of the three (3) exceptions is thrown
Display the score.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Questionnaire {
public static void main(String[] args) {
Question question1 = new Question("Question 1 correct answer B");
question1.addAnswer('A', "Answer A", false);
question1.addAnswer('B', "Answer B", true);
question1.addAnswer('C', "Answer C", false);
Question question2 = new Question("Question 2 correct answer B");
question2.addAnswer('A', "Answer A", false);
question2.addAnswer('B', "Answer B", true);
question2.addAnswer('C', "Answer C", false);
Question question3 = new Question("Question 3 correct answer B");
question3.addAnswer('A', "Answer A", false);
question3.addAnswer('B', "Answer B", true);
question3.addAnswer('C', "Answer C", false);
Question question4 = new Question("Question 4 correct answer B");
question4.addAnswer('A', "Answer A", false);
question4.addAnswer('B', "Answer B", true);
question4.addAnswer('C', "Answer C", false);
Question question5 = new Question("Question 5 correct answer B");
question5.addAnswer('A', "Answer A", false);
question5.addAnswer('B', "Answer B", true);
question5.addAnswer('C', "Answer C", false);
Question question6 = new Question("Question 6 correct answer B");
question6.addAnswer('A', "Answer A", false);
question6.addAnswer('B', "Answer B", true);
question6.addAnswer('C', "Answer C", false);
Question question7 = new Question("Question 7 correct answer B");
question7.addAnswer('A', "Answer A", false);
question7.addAnswer('B', "Answer B", true);
question7.addAnswer('C', "Answer C", false);
Question question8 = new Question("Question 8 correct answer B");
question8.addAnswer('A', "Answer A", false);
question8.addAnswer('B', "Answer B", true);
question8.addAnswer('C', "Answer C", false);
Question question9 = new Question("Question 9 correct answer B");
question9.addAnswer('A', "Answer A", false);
question9.addAnswer('B', "Answer B", true);
question9.addAnswer('C', "Answer C", false);
Question question10 = new Question("Question 10 correct answer B");
question10.addAnswer('A', "Answer A", false);
question10.addAnswer('B', "Answer B", true);
question10.addAnswer('C', "Answer C", false);
Question[] questions = {
question1,
question2,
question3,
question4,
question5,
question6,
question7,
question8,
question9,
question10
};
int score = 0;
int i = 0;
Scanner scanner = new Scanner(System.in);
while (i < 10) {
System.out.print(i+1 + ". ");
System.out.println(questions[i]);
System.out.print("Enter your answer -> ");
String userAnswer = scanner.nextLine();
try {
validateBlank(userAnswer);
validateNumberOrAnySpecialCharacter(userAnswer);
validateIsNotABC(userAnswer);
char answer = userAnswer.toUpperCase().charAt(0);
if (questions[i].isCorrectAnswer(answer)) {
score++;
}
i++;
} catch (UserAnswerIsBlankException | UserAnswerIsNumberOrAnySpecialCharacterException | UserAnswerIsNotABCCharacterException e) {
System.out.println("Answer is not allowed. \n");
}
}
System.out.println("Your score is : " + score);
}
private static void validateBlank(String userAnswer) {
if (userAnswer.isBlank()) {
throw new UserAnswerIsBlankException();
}
}
private static void validateNumberOrAnySpecialCharacter(String userAnswer) {
Character character = userAnswer.charAt(0);
if ((character < 'A' || character > 'Z') && (character < 'a' || character > 'z')) {
throw new UserAnswerIsNumberOrAnySpecialCharacterException();
}
}
private static void validateIsNotABC(String userAnswer) {
Character character = userAnswer.toUpperCase().charAt(0);
if ((character < 'A' || character > 'C')) {
throw new UserAnswerIsNotABCCharacterException();
}
}
}
class Question {
private String question;
private Map<Character, Answer> answers = new HashMap<>();
public Question(String question) {
this.question = question;
}
public void addAnswer(Character character, String answer, boolean isCorrect) {
answers.put(character, new Answer(answer, isCorrect));
}
public boolean isCorrectAnswer(Character character) {
Answer answer = answers.get(character);
return answer.isCorrect();
}
@Override
public String toString() {
return question
+ " \n A:" + answers.get('A')
+ " \n B:" + answers.get('B')
+ " \n C:" + answers.get('C');
}
}
class Answer {
private String answer;
private boolean isCorrect;
public Answer(String answer, boolean isCorrect) {
this.answer = answer;
this.isCorrect = isCorrect;
}
public boolean isCorrect() {
return isCorrect;
}
@Override
public String toString() {
return answer;
}
}
class UserAnswerIsBlankException extends RuntimeException {
}
class UserAnswerIsNotABCCharacterException extends RuntimeException {
}
class UserAnswerIsNumberOrAnySpecialCharacterException extends RuntimeException {
}
Comments
Leave a comment