kuchuz

PBO-C 5 : Responder()

Nov 26th, 2020 (edited)
110
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2.  
  3. public class Responder
  4. {
  5.     private Random randomGenerator;
  6.     private ArrayList<String> responses;
  7.     private HashMap<String, String> responseMap;
  8.      
  9.     public Responder()
  10.     {
  11.         randomGenerator = new Random();
  12.         responses = new ArrayList<String>();
  13.         responseMap = new HashMap<String, String>();
  14.         fillResponses();
  15.         fillResponseMap();
  16.     }
  17.      
  18.     public String generateResponse(HashSet<String> words)
  19.     {
  20.         for(String word:words)
  21.         {
  22.             String response=responseMap.get(word);
  23.             if(response!=null)
  24.             {
  25.                 return response;
  26.             }
  27.         }
  28.  
  29.         int index = randomGenerator.nextInt(responses.size());
  30.         return responses.get(index);
  31.     }
  32.      
  33.     private void fillResponses()
  34.     {
  35.         responses.add("Sorry, i dont understand you");
  36.         responses.add("Please try another word");
  37.     }
  38.      
  39.     private void fillResponseMap()
  40.     {
  41.         responseMap.put("problem", "Can you describe more about the problem?");
  42.         responseMap.put("bug", "What kind of bug?");
  43.         responseMap.put("error", "What kind of error?");
  44.         responseMap.put("stop", "You can restart the program");
  45.         responseMap.put("thanks", "You're Welcome");
  46.     }
  47. }
RAW Paste Data