Advertisement
mnaufaldillah

Responder Tugas 5

Nov 16th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.65 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5.  
  6. /**
  7.  * The responder class represents a response-generator object.
  8.  * It is used to generate an automatic response by randomly
  9.  * selecting a phrase from a predefined list of responses.
  10.  *
  11.  * @author Muhammad Naufaldillah
  12.  * @version 16 November 2020
  13.  */
  14. public class Responder
  15. {
  16.     private Random randomGenerator;
  17.     private ArrayList<String> responses;
  18.     private HashMap<String, String> responseMap;
  19.    
  20.     /**
  21.      * Construct a Responder
  22.      */
  23.     public Responder()
  24.     {
  25.         randomGenerator = new Random();
  26.         responses = new ArrayList<String>();
  27.         responseMap = new HashMap<String, String>();
  28.         fillResponses();
  29.         fillResponseMap();
  30.     }
  31.    
  32.     /**
  33.      * Generate a response
  34.      * @return  A string that should be displayed as the response
  35.      */
  36.     public String generateResponse(HashSet<String> words)
  37.     {
  38.         for(String word : words)
  39.         {
  40.             String response = responseMap.get(word);
  41.             if(response != null)
  42.             {
  43.                 return response;
  44.             }
  45.         }
  46.         // Pick a random number for the index in the default response
  47.         // list. The number will be between 0 (inclusive) and the size
  48.         // of the list (exclusive).
  49.         return pickDefaultResponse();
  50.     }
  51.    
  52.     /**
  53.      * Build up a list of default responses from which we can
  54.      * pick one if we don't know what else to say.
  55.      */
  56.     private void fillResponses()
  57.     {
  58.         responses.add("That sounds odd. Could you describe \n" +
  59.                       "that problem in more detail?");
  60.         responses.add("No other customer has ever \n" +
  61.                       "complained about this before. \n" +
  62.                       "What is your system configuration?");
  63.         responses.add("That’s a known problem with Vista." +
  64.                       "Windows 7 is much better.");
  65.         responses.add("I need a bit more information on that.");
  66.         responses.add("Have you checked that you do not \n" +
  67.                       "have a dll conflict?");
  68.         responses.add("That is explained in the manual. \n" +
  69.                       "Have you read the manual?");
  70.         responses.add("Your description is a bit \n" +
  71.                       "wishy-washy. Have you got an expert \n" +
  72.                       "there with you who could describe \n" +
  73.                       "this more precisely?");
  74.         responses.add("That’s not a bug, it’s a feature!");
  75.         responses.add("Could you elaborate on that?");
  76.     }
  77.    
  78.     /**
  79.      * Enter all the known keywords and their associated
  80.      * responses into our response map.
  81.      */
  82.     private void fillResponseMap()
  83.     {
  84.         responseMap.put("slow",
  85.             "I think this has to do with your hardware. \n" +
  86.             "Upgrading your processor should solve all " +
  87.             "performance problems. \n" +
  88.             "Have you got a problem with our software?");
  89.         responseMap.put("bug",
  90.             "Well, you know, all software has some bugs. \n" +
  91.             "But our software engineers are working very " +
  92.             "hard to fix them. \n" +
  93.             "Can you describe the problem a bit further?");
  94.         responseMap.put("expensive",
  95.             "The cost of our product is quite competitive. \n" +
  96.             "Have you looked around and " +
  97.             "really compared our features?");
  98.     }
  99.    
  100.     private String pickDefaultResponse()
  101.     {
  102.         int index = randomGenerator.nextInt(responses.size());
  103.         return responses.get(index);
  104.     }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement