
// Responser.java
// Andrew Davison, Nov. 2018, ad@fivedots.coe.psu.ac.th

/* An input line is passed to the responder and, based on the words in the line,
   the responder generates a String response.

   If none of the input words are recognized, then a default responses is chosen
   at random.

 * Internally, the reponder uses a HashMap to associate words with response
 * strings and a list of default responses. If any of the input words is found
 * in the HashMap, the corresponding response is returned. 
 * 
 * @version    1.0
 * @author     Michael Kolling and David J. Barnes
 */


import java.util.*;


public class Responder
{
  private HashMap<String, String> respMap;   // maps keywords to responses
  private ArrayList<String> defaultResps;    // used if we don't recognise a word
  private Random ranGen;


  public Responder()
  {
    initResponses();
    initDefaultResponses();
    ranGen = new Random();
  }  // end of Responder()


  private void initResponses()
  /* Enter all the known keywords and their associated responses
     into our response map. */
  {
    respMap = new HashMap<String, String>();

    respMap.put("crash", 
        "Well, it never crashes on our system. It must have something\n"
        + "to do with your system. Tell me more about your configuration.");
    respMap.put("crashes", 
        "Well, it never crashes on our system. It must have something\n"
        + "to do with your system. Tell me more about your configuration.");
    respMap.put("slow", 
        "I think this has to do with your hardware. Upgrading your processor\n"
        + "should solve all performance problems. Have you got a problem with\n"
        + "our software?");
    respMap.put("performance", 
        "Performance was quite adequate in all our tests. Are you running\n"
        + "any other processes in the background?");
    respMap.put("bug", 
        "Well, you know, all software has some bugs. But our software engineers\n"
        + "are working very hard to fix them. Can you describe the problem a bit\n"
        + "further?");
    respMap.put("buggy", 
        "Well, you know, all software has some bugs. But our software engineers\n"
        + "are working very hard to fix them. Can you describe the problem a bit\n"
        + "further?");
    respMap.put("windows", 
        "This is a known bug to do with the Windows operating system. Please\n"
        + "report it to Microsoft. There is nothing we can do about this.");
    respMap.put("macintosh", 
        "This is a known bug to do with the Mac operating system. Please\n"
        + "report it to Apple. There is nothing we can do about this.");
    respMap.put("expensive", 
        "The cost of our product is quite competitive. Have you looked around\n"
        + "and really compared our features?");
    respMap.put("installation", 
        "The installation is really quite straight forward. We have tons of\n"
        + "wizards that do all the work for you. Have you read the installation\n"
        + "instructions?");
    respMap.put("memory", 
        "If you read the system requirements carefully, you will see that the\n"
        + "specified memory requirements are 1.5 giga byte. You really should\n"
        + "upgrade your memory. Anything else you want to know?");
    respMap.put("linux", 
        "We take Linux support very seriously. But there are some problems.\n"
        + "Most have to do with incompatible glibc versions. Can you be a bit\n"
        + "more precise?");
    respMap.put("bluej", 
        "Ahhh, BlueJ, yes. We tried to buy out those guys long ago, but\n"
        + "they simply won't sell... Stubborn people they are. Nothing we can\n"
        + "do about it, I'm afraid.");
  }  // end of initResponses()


  private void initDefaultResponses()
  /* Build up a list of default responses from which we can pick one
     if we don't know what else to say. */
  {
    defaultResps = new ArrayList<String>();

    defaultResps.add("That sounds odd. Could you describe that problem in more detail?");
    defaultResps.add("No other customer has ever complained about this before. \n"
            + "What is your system configuration?");
    defaultResps.add("That sounds interesting. Tell me more...");
    defaultResps.add("I need a bit more information on that.");
    defaultResps.add("Have you checked that you do not have a dll conflict?");
    defaultResps.add("That is explained in the manual. Have you read the manual?");
    defaultResps.add("Your description is a bit wishy-washy. Have you got an expert\n"
            + "there with you who could describe this more precisely?");
    defaultResps.add("That's not a bug, it's a feature!");
    defaultResps.add("Could you elaborate on that?");
  }  // end of initDefaultResponses()




  public String genResponse(String inputLine)
  // Generate a response from a given input line
  {
    String[] words = inputLine.split(" "); // split at spaces
    String response = null;
    for(int i=0; i < words.length; i++) {
      response = respMap.get( words[i] );
      if (response != null)
        return response;
    }

    /* None of the words from the input line were recognized,
       so randomly select a default responses to return. */
    int i = ranGen.nextInt( defaultResps.size() );    //  0 to size-1
    return defaultResps.get(i);
  }  // end of genResponse()


}  // end of Responder class 
