Tugas 5 - TechSupport System
Pada tugas kelima ini kami diberi tugas untuk membuat Technical Support System. Technical Support System ialah sebuah program yang menyediakan technical support untuk usernya, berupa bot yang akan merespon secara otomatis sebuah input yang diberikan. Program berikut akan berhenti ketika menginput suatu key.
Berikut merupakan dokumentasi dari TechSupport System yang telah dibuat,
Source Code
1. Class SupportSystem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
/** | |
* This class implements a technical support system. It is the top-level class in this project. | |
* The support system communicates via text input/output in the text terminal. This class uses | |
* an object of class InputReader to read input from the user and an object of class Responder | |
* to generates output until the user wants to leave. | |
* | |
* @author Fidhia Ainun Khofifah | |
* @version 0.1 10/11/2020 | |
*/ | |
public class SupportSystem | |
{ | |
private InputReader reader; | |
private Responder responder; | |
/** | |
* Creates a technical support system. | |
*/ | |
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 a welcome message to the screen. | |
*/ | |
private void printWelcome() | |
{ | |
System.out.println("Welcome to the Mangorenji Technical Support System!"); | |
System.out.println("Please tell us about your problem."); | |
System.out.println("Please type 'bye' to exit our system."); | |
System.out.println(); | |
} | |
/** | |
* Print a good-bye message to the screen. | |
*/ | |
private void printGoodbye() | |
{ | |
System.out.println("Nice talking to you. Have a good day!"); | |
} | |
} |
2. Class InputReader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
/** | |
* The InputReader class is used for read the input entry. | |
* | |
* @author Fidhia Ainun Khofifah | |
* @version 0.1 10/11/2020 | |
*/ | |
public class InputReader | |
{ | |
private Scanner reader; | |
public InputReader() | |
{ | |
reader = new Scanner(System.in); | |
} | |
/** | |
* Read user input | |
*/ | |
public HashSet<String> getInput() | |
{ | |
System.out.print(">> "); | |
String inputLine = reader.nextLine().trim().toLowerCase(); | |
String[] wordArray = inputLine.split(" "); | |
HashSet<String> words = new HashSet<String>(); | |
for(String word : wordArray) | |
{ | |
words.add(word); | |
} | |
return words; | |
} | |
} |
3. Class Responder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
/** | |
* The responder class represents a response-generator object. | |
* It is used to generate an automatic response to an input string. | |
* | |
* @author Fidhia Ainun Khofifah | |
* @version 0.2 10/11/2020 | |
*/ | |
public class Responder | |
{ | |
private Random randomGenerator; | |
private ArrayList<String> defaultResponses; | |
private HashMap<String, String> responseMap; | |
/** | |
* Create a responder. | |
*/ | |
public Responder() | |
{ | |
defaultResponses = new ArrayList<String>(); | |
responseMap = new HashMap<String, String>(); | |
fillResponses(); | |
fillDefaultResponses(); | |
randomGenerator = new Random(); | |
} | |
/** | |
* Generate a response. | |
* @return A string that should be displayed as the response. | |
*/ | |
public String generateResponse(HashSet<String> words) | |
{ | |
for(String word : words) | |
{ | |
String response = responseMap.get(word); | |
if(response != null) | |
{ | |
return response; | |
} | |
} | |
return pickDefaultResponse(); | |
} | |
/** | |
* Build up a list of default responses from which we can pick one | |
* if we don't know what else to say. | |
*/ | |
private void fillResponses() | |
{ | |
responseMap.put("hi", "Hi, How's your day?"); | |
responseMap.put("hello", "Hello there! " + "How's your day?"); | |
responseMap.put("bad", "Is there something bothering you? :("); | |
responseMap.put("sad", "Hope that you can get better. " + "It's Okay not to be Okay."); | |
responseMap.put("happy", "So glad to hear that!"); | |
responseMap.put("pretty", "you are :)"); | |
responseMap.put("happy", "So glad to hear that!"); | |
responseMap.put("nothing", "That's okay, i still love you:)"); | |
responseMap.put("you", "I'm mangorenji bot, your online diary <3"); | |
responseMap.put("thanks", "It's my pleasure!"); | |
} | |
private void fillDefaultResponses() | |
{ | |
defaultResponses.add("Sorry I can't understand."); | |
defaultResponses.add("Please try again."); | |
} | |
private String pickDefaultResponse() | |
{ | |
int index = randomGenerator.nextInt(defaultResponses.size()); | |
return defaultResponses.get(index); | |
} | |
} |
Run Code
Berikut merupakan tahapan run code chatbot,
1. Klik kanan pada class SupportSystem(), lalu klik new SupportSystem().
2. Ketik nama dan tekan OK.
4. Berikut merupakan implementasi dari TechSupport System.
Comments
Post a Comment