📄 agent.java
字号:
package server;
import java.util.*;
import java.io.*;
/** * The agent object that encapsulates a series of questions and answers. */
public class Agent {
private List problems = new Vector();
private int totalAnswersCorrect;
private int totalQuestions;
private String lastAnswer;
public Agent() {
initProblems();
this.totalQuestions = problems.size();
}
/** * Loads the questions and answers. */
private void initProblems() {
Problem problem = null;
problem = new Problem(1, "Who is the first president?",
"George Washington");
problems.add(problem);
problem = new Problem(2, "Who is the second president?", "John Adams");
problems.add(problem);
problem = new Problem(3, "Who is the third president?",
"Thomas Jefferson");
problems.add(problem);
problem = new Problem(4, "Who is the fourth president?",
"James Madison");
problems.add(problem);
problem = new Problem(5, "Who is the fifth president?", "James Monroe");
problems.add(problem);
problem = new Problem(6, "Who is the sixth president?",
"John Quincy Adams");
problems.add(problem);
problem = new Problem(7, "Who is the seventh president?",
"Andrew Jackson");
problems.add(problem);
problem = new Problem(8, "Who is the eight president?",
"Martin Van Buren");
problems.add(problem);
problem = new Problem(9, "Who is the ninth president?",
"William Henry Harrison");
problems.add(problem);
problem = new Problem(10, "Who is the tenth president?", "John Tyler");
problems.add(problem);
}
/** * Gets the total number of questions */
public int getTotalNumberOfQuestions() {
return this.totalQuestions;
}
/** * Returns the next problem. */
public Problem getNextProblem() {
return (Problem) problems.get(0);
}
/** * Processes the answer to the current problem. * * @returns true if answered correctly. * @returns false if answered incorrectly. */
public boolean processAnswer(String answer) {
Problem problem = (Problem) problems.remove(0);
this.lastAnswer = problem.getAnswer();
if (answer.equals(problem.getAnswer())) {
totalAnswersCorrect++;
return true;
} else {
return false;
}
}
/** * Returns the last answer. */
public String getLastAnswer() {
return this.lastAnswer;
}
/** * Returns true if there are more problems left to solve. */
public boolean hasMoreProblems() {
return !problems.isEmpty();
}
/** * Returns the total number of questions answered correctly so far. */
public int getTotalAnswersCorrect() {
return totalAnswersCorrect;
}
/** * Returns tips and suggestions to help the student improve his score based on * the total number of questions answered correctly. */
public String getSuggestions() {
return (totalAnswersCorrect >= 8)
? "Excellent! Please view this website for further knowledge: http://www.ipl.org/ref/POTUS"
: "You need to study harder! Here is a website to help http://www.ipl.org/ref/POTUS";
}
/** * Encapsulates a problem with a solution. */
class Problem {
private int number;
private String question;
private String answer;
Problem(int number, String question, String answer) {
this.number = number;
this.question = question;
this.answer = answer;
}
/** * Returns the problem number. */
public int getNumber() {
return this.number;
}
/** * Returns the question. */
public String getQuestion() {
return this.question;
}
/** * Returns the answer to the problem. */
String getAnswer() {
return answer;
}
} ///:~ Problem
} ///:~ Agent
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -