Java Reference
In-Depth Information
Code 5.3
continued
The
Responder
source code with
random responses
* @author Michael Kölling and David J. Barnes
* @version 0.2 (2011.07.31)
*/
public
class
Responder
{
private
Random randomGenerator;
private
ArrayList<String> responses;
/**
* Create a responder.
*/
public
Responder()
{
randomGenerator =
new
Random();
responses =
new
ArrayList<String>();
fillResponses();
}
/**
* Generate a response.
* @return A string that should be displayed as the
* response
*/
public
String generateResponse()
{
// Pick a random number for the index in the default response
// list. The number will be between 0 (inclusive) and the size
// of the list (exclusive).
int
index = randomGenerator.nextInt(responses.size());
return
responses.get(index);
}
/**
* Build up a list of default responses from which we can
* pick one if we don't know what else to say.
*/
private
void
fillResponses()
{
responses.add(
"That sounds odd. Could you describe \n"
+
"that problem in more detail?"
);
responses.add(
"No other customer has ever \n"
+
"complained about this before. \n"
+
"What is your system configuration?"
);
responses.add(
"That's a known problem with Vista."
+
"Windows 7 is much better."
);
responses.add(
"I need a bit more information on that."
);
responses.add(
"Have you checked that you do not \n"
+
"have a dll conflict?"
);
Search WWH ::
Custom Search