Technical Support System
Technical Support System merupakan suatu layanan yang berfungsi untuk membantu user atau mengatasi masalah yang dialami user. Program di bawah ini akan menjawab pertanyaan dari user. Jadi, ketika user menginputkan sesuatu, maka sistem akan merespon user.
Program Technical Support System terdiri dari beberapa kelas, diantaranya sebagai berikut :
1. SupportSystem : Kelas utama dari program.
2. InputReader : Kelas ini akan menerima input dari user, yaitu berupa pertanyaan
3. Responder : Kelas ini akan memberi respon atas input user, yaitu berupa jawaban yang sesuai dengan pertanyaan user
1. Class SupportSystem
Kelas ini merupakan utama dari program. Kelas ini menggunakan objek dari kelas InputReader untuk membaca input user dan juga objek dari kelas Responder untuk merespon user.
- import java.util.*;
- /**
- * @author Sabrina Lydia S
- * @version 15/11/20
- */
- public class SupportSystem
- {
- private InputReader reader;
- private Responder responder;
- /**
- * Creates a technical SupportSystem
- */
- public SupportSystem()
- {
- reader = new InputReader();
- responder = new Responder();
- }
- /**
- * Start the technical support system. This will print a
- * welcome message and enter into a dialog with the user,
- * until the user ends the dialog.
- */
- public void start()
- {
- boolean finished = false;
- printWelcome();
- while(!finished) {
- HashSet<String> input = reader.getInput();
- if(input.contains("bye")) {
- finished = true;
- }
- else {
- String response = responder.generateResponse(input);
- System.out.println(response);
- }
- }
- printGoodbye();
- }
- //Print welcome message to the screen
- private void printWelcome()
- {
- System.out.println("Welcome to PBO Technical Support System.");
- System.out.println();
- System.out.println("Please tell us about your problem.");
- System.out.println("We will assist you with any problem you might have.");
- System.out.println("Please type 'bye' to exit our system.");
- }
- //Print goodbye message to the screen
- private void printGoodbye()
- {
- System.out.println("It was nice talking to you, See you again");
- }
- }
2. Class InputReader
Kelas ini akan menerima input dari user, yaitu berupa teks, dan membaca teks tersebut.
- import java.util.Scanner;
- import java.util.HashSet;
- /**
- * @author Sabrina Lydia S
- * @version 15/11/20
- */
- public class InputReader
- {
- private Scanner reader;
- /**
- * Constructor for objects of class InputReader
- * to read input text from user public InputReader()
- */
- public InputReader()
- {
- reader = new Scanner(System.in);
- }
- /**
- * Read a line of text from standard input (the text
- * terminal), and return it as a set of words.
- *
- * @return A set of Strings, where each String is one of the
- * words typed by the user
- */
- public HashSet<String> getInput()
- {
- System.out.print("> "); // print prompt
- String inputLine = reader.nextLine().trim().toLowerCase();
- String[] wordArray = inputLine.split(" ");
- // add words from array into hashset
- HashSet<String> words = new HashSet<>();
- for(String word : wordArray) {
- words.add(word);
- }
- return words;
- }
- }
3. Class Responder
Kelas ini akan memberi respon atas input user, yaitu berupa jawaban yang sesuai dengan pertanyaan user.
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.Random;
- /**
- * @author Sabrina Lydia S
- * @version 15/11/20
- */
- public class Responder
- {
- //Default response used for unrecognized input
- private ArrayList<String> defaultResponses;
- private Random randomGenerator;
- // Mapping index/key to response
- private HashMap<String, String> responseMap;
- /**
- * Constructor for objects of class Responder
- */
- public Responder()
- {
- responseMap = new HashMap<String, String>();
- defaultResponses = new ArrayList<String>();
- fillResponseMap();
- fillDefaultResponses();
- randomGenerator = new Random();
- }
- /**
- * Generate a response from one set of input words given.
- *
- * @param words A set of words given by the user
- * @return String that should be displayed as a response
- */
- public String generateResponse(HashSet<String> words)
- {
- for(String word : words)
- {
- String response = responseMap.get(word);
- if(response != null)
- {
- return response;
- }
- }
- // If we get here, none of the words from the input line was
- // recognized. In this case, we pick one of our default responses.
- return pickDefaultResponse();
- }
- /**
- * Enter all the known keywords and their associated responses
- * into our response map.
- */
- private void fillResponseMap()
- {
- responseMap.put("hello", "hello.");
- responseMap.put("hi", "hi.");
- responseMap.put("problem",
- "Could you describe \n" +
- "that problem in more detail?");
- responseMap.put("slow",
- "I think this has to do with your hardware. \n" +
- "Upgrading your processor should solve all " +
- "performance problems. \n" +
- "Have you got a problem with our software?");
- responseMap.put("bug",
- "Well, you know, all software has some bugs. \n" +
- "But our software engineers are working very " +
- "hard to fix them. \n" +
- "Can you describe the problem a bit further?");
- responseMap.put("expensive",
- "The cost of our product is quite competitive. \n" +
- "Have you looked around and " +
- "really compared our features?");
- responseMap.put("windows",
- "That can be happened whether you use unoriginal windows \n" +
- "or you need to do further Update");
- responseMap.put("how are you", "I'm doing fine!");
- responseMap.put ("ok", "Anything else you wish to ask?");
- responseMap.put("",
- "I don't quite understand about what you said. \n" +
- "Please try to ask me for other entry.");
- }
- /**
- * Build up a list of default responses from which we can pick one
- * if we don't know what else to say.
- */
- private void fillDefaultResponses()
- {
- defaultResponses.add("That sounds odd. Please be more specific");
- defaultResponses.add("I'm sorry, I didn't understand...");
- }
- /**
- * Randomly select and return one of the default responses.
- * @return A random default response
- */
- private String pickDefaultResponse()
- {
- // Pick a random number for the index in the default response list.
- // The number will be between 0 (inclusive) and the size of the list (exclusive).
- int index = randomGenerator.nextInt(defaultResponses.size());
- return defaultResponses.get(index);
- }
- }
Implementasi
Komentar
Posting Komentar