PBO - Tugas 5 - Aplikasi Support System

 

TUGAS 5 - support system
Nama : Kevin Ashil
NRP : 05111740000178
Kelas : PBO - B

Pada minggu ke-5 mahasiswa ditugaskan untuk membuat program Technical Support yang dapat menjawab input user dan menjawabnya seperti pada TechSupport biasanya. Berikut merupakan program tersebut:

SupportSystem:
DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1.  
  2. /**
  3.  * This class implements a technical support system. It is
  4.  * the top-level class in this project. The support system
  5.  * communicates via text input/output in the text terminal.
  6.  * This class uses an object of class InputReader to read
  7.  * input from the user and an object of class Responder to
  8.  * generate responses.
  9.  * It contains a loop that repeatedly reads input and
  10.  * generates output until the user wants to leave.
  11.  *
  12.  * @author Kevin Ashil
  13.  * @version 0.1 (24 November 2020)
  14.  */
  15.  
  16. import java.util.*;
  17. public class SupportSystem
  18. {
  19.     private InputReader reader;
  20.     private Responder responder;
  21.     /**
  22.      * Creates a technical support system.
  23.      */
  24.     public SupportSystem()
  25.     {
  26.         reader = new InputReader();
  27.         responder = new Responder();
  28.     }
  29.     /**
  30.      * Start the technical support system. This will print a
  31.      * welcome message and enter into a dialog with the user,
  32.      * until the user ends the dialog.
  33.      */
  34.     public void start()
  35.     {
  36.         boolean finished = false;
  37.         printWelcome();
  38.         while(!finished) {
  39.             HashSet<String> input = reader.getInput();
  40.             if(input.contains("bye")) {
  41.                 finished = true;
  42.             }else {
  43.                 String response = responder.generateResponse(input);
  44.                 System.out.println(response);
  45.             }
  46.         }
  47.         printGoodbye();
  48.     }
  49.     /**
  50.      * Print a welcome message to the screen.
  51.      */
  52.     private void printWelcome()
  53.     {
  54.         System.out.println(
  55.         "Welcome to the Technical Support System.");
  56.         System.out.println();
  57.         System.out.println("Please tell us about your problem.");
  58.         System.out.println(
  59.         "We will assist you with any problem you might have.");
  60.         System.out.println(
  61.         "Please type 'bye' to exit our system.");
  62.     }
  63.     /**
  64.      * Print a good-bye message to the screen.
  65.      */
  66.     private void printGoodbye()
  67.     {
  68.         System.out.println("Nice talking to you. Goodbye");
  69.     }
  70. }
InputReader:
DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1.  
  2. /**
  3.  * The InputReader class is use for reading the input entry
  4.  *
  5.  * @author Kevin Ashil
  6.  * @version 0.1 (24 November 2020)
  7.  */
  8.  
  9. import java.util.*;
  10. public class InputReader
  11. {
  12.     private Scanner reader;
  13.     public InputReader()
  14.     {
  15.         reader = new Scanner(System.in);
  16.     }
  17.     /**
  18.      * Read user input
  19.      */
  20.     public HashSet<String> getInput()
  21.     {
  22.         System.out.print(">> ");
  23.         String inputLine = reader.nextLine().trim().toLowerCase();
  24.        
  25.         String[] wordArray = inputLine.split(" ");
  26.            
  27.         HashSet<String> words = new HashSet<String>();
  28.         for(String word : wordArray)
  29.         {
  30.             words.add(word);
  31.         }
  32.         return words;
  33.     }
  34. }
  35.  
Responder:
DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. /**
  2.  * The responder class represents a response generator object.
  3.  * It is used to generate an automatic response to an input string.
  4.  *
  5.  * @author Kevin Ashil
  6.  * @version 0.1 (24 November 2020)
  7.  */
  8.  
  9. import java.util.*;
  10. public class Responder
  11. {
  12.     private Random randomGenerator;
  13.     private ArrayList<String> defaultResponses;
  14.     private HashMap<String, String>responseMap;
  15.     /**
  16.      * Construct a Responder - nothing to do
  17.      */
  18.     public Responder()
  19.     {
  20.         randomGenerator = new Random();
  21.         defaultResponses = new ArrayList<String>();
  22.         responseMap = new HashMap<String, String>();
  23.         fillResponses();
  24.         fillDefaultResponses();
  25.     }
  26.     /**
  27.      * Generate a response.
  28.      * @return A string that should be displayed as the
  29.      * response
  30.      */
  31.     public String generateResponse(HashSet<String> words)
  32.     {
  33.         for(String word:words)
  34.         {
  35.             String response = responseMap.get(word);
  36.             if(response != null)
  37.                 {
  38.                     return response;
  39.                 }
  40.         }
  41.         return pickDefaultResponse();
  42.     }
  43.     /**
  44.      * Build up a list of default responses from which we can pick one
  45.      * if we don't know what else to say.
  46.      */
  47.     private void fillResponses()
  48.     {
  49.         responseMap.put("hey""Hello, How can i help you?");
  50.         responseMap.put("hello""Hi there! " + "How can i help?");
  51.         responseMap.put("error""I am sorry to hear that"
  52.         + ", We are trying to fix the problem as soon as possible");
  53.         responseMap.put("nice""Thank you for your compliment");
  54.         responseMap.put("issue""I am sorry for your inconvenience"
  55.         + ", What is the issue?");
  56.         responseMap.put("thanks""It's my pleasure!");
  57.     }
  58.     private void fillDefaultResponses()
  59.     {
  60.         defaultResponses.add("Sorry I can't understand.");
  61.         defaultResponses.add("Please try again.");
  62.     }
  63.     private String pickDefaultResponse()
  64.     {
  65.         int index = randomGenerator.nextInt(defaultResponses.size());
  66.         return defaultResponses.get(index);
  67.     }
  68. }
  69.  

Berikut program yang dijalankan dengan beberapa Test Case:



Komentar

Postingan populer dari blog ini

TUGAS - 2 - PBKK