Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

smartResponder1

By: ace on Jan 27th, 2010  |  syntax: None  |  size: 2.92 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3. import java.util.HashMap;
  4.  
  5. /**
  6.  * The responder class represents a response generator object. It is
  7.  * used to generate an automatic response.
  8.  * This is the second version of this class. This time, we generate
  9.  * some random behavior by randomly selecting a phrase from a predefined
  10.  * list of responses.
  11.  *
  12.  * @author     Michael Kolling and David J. Barnes
  13.  * @version    0.2
  14.  */
  15. public class Responder
  16. {
  17.     private Random randomGenerator;
  18.     private ArrayList<String> responses;
  19.     public HashMap<String,String> smartResponder;
  20.     /**
  21.      * Construct a Responder
  22.      */
  23.     public Responder()
  24.     {
  25.         randomGenerator = new Random();
  26.         responses = new ArrayList<String>();
  27.         fillResponses();
  28.         smartResponder = new HashMap <String,String>();
  29.         fillSmartResponder();
  30.     }
  31.  
  32.     /**
  33.      * Generate a response.
  34.      *
  35.      * @return  A string that should be displayed as the response
  36.      */
  37.     public String generateResponse()
  38.     {
  39.         // Pick a random number for the index in the default response
  40.         // list. The number will be between 0 (inclusive) and the size
  41.         // of the list (exclusive).
  42.         int index = randomGenerator.nextInt(responses.size());
  43.         return responses.get(index);
  44.     }
  45.    
  46.     private void fillSmartResponder()
  47. {
  48. smartResponder.put("slow",
  49. "I think this has to do with your hardware. \n" +
  50. "Upgrading your processor should solve all \n" +
  51. "performance problems. Have you got a problem with \n" +
  52. "our software?");
  53. smartResponder.put("bug",
  54. "Well, you know, all software has some bugs. But \n" +
  55. "our software engineers are working very hard to \n" +
  56. "fix them. Can you describe the problem a bit \n" +
  57. "further?");
  58. smartResponder.put("expensive",
  59. "The cost of our product is quite competitive. \n" +
  60. "Have you looked around and really compared \n" +
  61. "our features?");
  62. }
  63.  
  64.     /**
  65.      * Build up a list of default responses from which we can pick one
  66.      * if we don't know what else to say.
  67.      */
  68.     private void fillResponses()
  69.     {
  70.         responses.add("That sounds odd. Could you describe that problem in more detail?");
  71.         responses.add("No other customer has ever complained about this before. \n" +
  72.                       "What is your system configuration?");
  73.         responses.add("That sounds interesting. Tell me more...");
  74.         responses.add("I need a bit more information on that.");
  75.         responses.add("Have you checked that you do not have a dll conflict?");
  76.         responses.add("That is explained in the manual. Have you read the manual?");
  77.         responses.add("Your description is a bit wishy-washy. Have you got an expert\n" +
  78.                       "there with you who could describe this more precisely?");
  79.         responses.add("That's not a bug, it's a feature!");
  80.         responses.add("Could you elaborate on that?");
  81.     }
  82. }