/**
* Response output to the System
*
* @author (prabu)
* @version (2020/11/9)
*/
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Random;
import java.time.LocalTime;
public class Responder
{
private HashMap<String, String> responseMap;
private Random randomGenerator;
private ArrayList<String> responses, faq, greetings;
/**
* Constructor for the Responder based on the user input
*
*/
public Responder()
{
randomGenerator = new Random();
LocalTime time = LocalTime.now();
greetings = new ArrayList<String>();
faq = new ArrayList<String>();
responses = new ArrayList<String>();
responseMap = new HashMap<String, String>();
fillGreetings(time.getHour());
fillFAQ();
fillResponseMap();
fillResponses();
}
/**
* Enter all the greetings into the greetings ArrayList
*/
private void fillGreetings(int h)
{
if(h >= 3 && h < 11)
{
greetings.add("Good Morning. ");
greetings.add("Morning! ");
}
else if(h >= 11 && h < 15)
{
greetings.add("Good Afternoon. ");
greetings.add("Salutations in the afternoon, good man/woman! ");
greetings.add("Afternoon, Sir/Mam! ");
}
else if(h >= 15 && h < 19)
{
greetings.add("Good Evening, Costumer. ");
greetings.add("Evening. ");
}
else
{
greetings.add("Greetings. ");
greetings.add("Hello and Welcome! ");
greetings.add("Nights has fallen, but we still will try our best! ");
}
}
/**
* Enter all the respones for the FAQ
*/
private void fillFAQ()
{
faq.add("Try upgrading your program to the latest stable version "+
"or/and try upgrading your processor too.");
faq.add("Try end task by using task manager or restart your "+
"computer.");
faq.add("Try upgrading your processor.");
}
/**
* Enter all the default response into the response ArrayList
*/
private void fillResponses()
{
responses.add("Interesting. I need a bit more information than that.");
responses.add("No other costumer has ever complained about this before.\\n"+
"Can you provide more information about your problems?");
responses.add("That sounds odd. Could you elaborate on that?");
responses.add("Have you checked that you do not have a dll "+
"conflict?");
responses.add("Your description is a bit wishy-washy.\\n"+
"Have you got an expert there with you that could describe this "+
"more precisely?");
responses.add("Allright. Have you considered upgrading your processor?\\n"+
"It might solve all your problems.");
}
/**
* 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 your performance problems.\\n"+
"Have you got problem with our software?");
responseMap.put("bug",
"All software has their own bugs.\\n" +
"Our software engineers are working very had to solve them.\\n"+
"Can you describe the problem a bit further?");
responseMap.put("expensive",
"The cost of each product is based on the features and what they offers\\n"+
"Have you really looked around and compared our features?");
}
/**
* Generate a response based on the user input
*
* @param HashSet of words from the user input
* @return Strings of responses based on the user input
*/
public String generateResponse(HashSet<String>words)
{
for(String word: words)
{
String response = responseMap.get(word);
responseMap.get(word);
if(response != null)
{
return response;
}
}
// If we get here, none of the words from the input line was
// recognize. In this case, we pick one of our default responses.
return generateResponse();
}
/**
* Generate a default response when there\'s no more valid response that
* corelate with the responseMap
*
* @return String of response
*/
private String generateResponse()
{
// Pick a random number for the index in the default response list.
// The number will be between 0 (includes) and the size of the
// list (exclusives).
int index = randomGenerator.nextInt(responses.size());
return responses.get(index);
}
/**
* Return the FAQ response corresponding to the user input option
*
* @param The inputOption that determines the correct response
* @return Strings of response based on the inputOption
*/
public String getFAQ(int opt)
{
return faq.get(opt-1);
}
/**
* Generate greetings response depends on the hour of the day
*
* @return Strings of greetings.
*/
public String generateGreeting()
{
int index = randomGenerator.nextInt(greetings.size());
return greetings.get(index);
}
}