import java.util.Random;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
/**
* The responder class represents a response-generator object.
* It is used to generate an automatic response by randomly
* selecting a phrase from a predefined list of responses.
*
* @author Muhammad Naufaldillah
* @version 16 November 2020
*/
public class Responder
{
private Random randomGenerator;
private ArrayList<String> responses;
private HashMap<String, String> responseMap;
/**
* Construct a Responder
*/
public Responder()
{
randomGenerator = new Random();
responses = new ArrayList<String>();
responseMap = new HashMap<String, String>();
fillResponses();
fillResponseMap();
}
/**
* Generate a response
* @return A string that should be displayed as the response
*/
public String generateResponse(HashSet<String> words)
{
for(String word : words)
{
String response = responseMap.get(word);
if(response != null)
{
return response;
}
}
// 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).
return pickDefaultResponse();
}
/**
* 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?");
responses.add("That is explained in the manual. \\n" +
"Have you read the manual?");
responses.add("Your description is a bit \\n" +
"wishy-washy. Have you got an expert \\n" +
"there with you who could describe \\n" +
"this more precisely?");
responses.add("That’s not a bug, it’s a feature!");
responses.add("Could you elaborate on that?");
}
/**
* Enter all the known keywords and their associated
* responses into our response map.
*/
private void fillResponseMap()
{
responseMap.put("slow",
"I think this has to do with your hardware. \\n" +
"Upgrading your processor should solve all " +
"performance problems. \\n" +
"Have you got a problem with our software?");
responseMap.put("bug",
"Well, you know, all software has some bugs. \\n" +
"But our software engineers are working very " +
"hard to fix them. \\n" +
"Can you describe the problem a bit further?");
responseMap.put("expensive",
"The cost of our product is quite competitive. \\n" +
"Have you looked around and " +
"really compared our features?");
}
private String pickDefaultResponse()
{
int index = randomGenerator.nextInt(responses.size());
return responses.get(index);
}
}