first commit
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
#BlueJ class context
|
||||
comment0.target=InputReader()
|
||||
comment0.text=\nCreate\ a\ new\ InputReader\ that\ reads\ text\ from\ the\ text\ terminal.\n\n
|
||||
comment1.target=HashSet<String>\ getInput()
|
||||
comment1.text=\nRead\ a\ line\ of\ text\ from\ standard\ input\ (the\ text\ terminal),\nand\ return\ it\ as\ a\ set\ of\ words.\n\n@return\ \ A\ set\ of\ Strings,\ where\ each\ String\ is\ one\ of\ the\ \nwords\ typed\ by\ the\ user\n\n
|
||||
numComments=2
|
@@ -0,0 +1,45 @@
|
||||
import java.util.HashSet;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* InputReader reads typed text input from the standard text terminal.
|
||||
* The text typed by a user is then chopped into words, and a set of words
|
||||
* is provided.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling.
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class InputReader
|
||||
{
|
||||
private Scanner reader;
|
||||
|
||||
/**
|
||||
* Create a new InputReader that reads text from the text terminal.
|
||||
*/
|
||||
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(" "); // split at spaces
|
||||
|
||||
// add words from array into hashset
|
||||
HashSet<String> words = new HashSet<>();
|
||||
for(String word : wordArray) {
|
||||
words.add(word);
|
||||
}
|
||||
return words;
|
||||
}
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
TechSupport - the DodgySoft Technical support system.
|
||||
|
||||
This project is part of the material for the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
This project is discussed in chapters 5 and 12.
|
||||
|
||||
This project implements a technical support system for customers of the
|
||||
DodgySoft software company. Users can describe their software problems and
|
||||
get advice instantly!
|
||||
|
||||
The idea is based on Eliza - a famous program described by Joseph Weizenbaum
|
||||
in 1966. (Do a web search for "Eliza" and "Weizenbaum" if you want to know
|
||||
more about this.)
|
||||
|
||||
In fact, it is much more primitive than Eliza. But that's enough to match the
|
||||
quality of many software companies' technical support advice... ;-)
|
||||
|
||||
To start this program, create a SupportSystem object and execute the "start"
|
||||
method.
|
||||
|
||||
Then start describing your problem by typing in the terminal window.
|
||||
|
||||
The purpose of this project is to demonstrate and study library classes, such
|
||||
as ArrayList, HashMap, HashSet, and Random.
|
@@ -0,0 +1,17 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=Responder()
|
||||
comment0.text=\r\n\ Construct\ a\ Responder\r\n
|
||||
comment1.params=words
|
||||
comment1.target=java.lang.String\ generateResponse(java.util.HashSet)
|
||||
comment1.text=\r\n\ Generate\ a\ response\ from\ a\ given\ set\ of\ input\ words.\r\n\ \r\n\ @param\ words\ \ A\ set\ of\ words\ entered\ by\ the\ user\r\n\ @return\ \ \ \ \ \ \ A\ string\ that\ should\ be\ displayed\ as\ the\ response\r\n
|
||||
comment2.params=
|
||||
comment2.target=void\ fillResponseMap()
|
||||
comment2.text=\r\n\ Enter\ all\ the\ known\ keywords\ and\ their\ associated\ responses\r\n\ into\ our\ response\ map.\r\n
|
||||
comment3.params=
|
||||
comment3.target=void\ fillDefaultResponses()
|
||||
comment3.text=\r\n\ Build\ up\ a\ list\ of\ default\ responses\ from\ which\ we\ can\ pick\r\n\ if\ we\ don't\ know\ what\ else\ to\ say.\r\n
|
||||
comment4.params=
|
||||
comment4.target=java.lang.String\ pickDefaultResponse()
|
||||
comment4.text=\r\n\ Randomly\ select\ and\ return\ one\ of\ the\ default\ responses.\r\n\ @return\ \ \ \ \ A\ random\ default\ response\r\n
|
||||
numComments=5
|
@@ -0,0 +1,157 @@
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* The responder class represents a response generator object.
|
||||
* It is used to generate an automatic response, based on specified input.
|
||||
* Input is presented to the responder as a set of words, and based on those
|
||||
* words the responder will generate a String that represents the response.
|
||||
*
|
||||
* Internally, the reponder uses a HashMap to associate words with response
|
||||
* strings and a list of default responses. If any of the input words is found
|
||||
* in the HashMap, the corresponding response is returned. If none of the input
|
||||
* words is recognized, one of the default responses is randomly chosen.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling.
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Responder
|
||||
{
|
||||
// Used to map key words to responses.
|
||||
private HashMap<String, String> responseMap;
|
||||
// Default responses to use if we don't recognise a word.
|
||||
private ArrayList<String> defaultResponses;
|
||||
// The name of the file containing the default responses.
|
||||
private static final String FILE_OF_DEFAULT_RESPONSES = "default.txt";
|
||||
private Random randomGenerator;
|
||||
|
||||
/**
|
||||
* Construct a Responder
|
||||
*/
|
||||
public Responder()
|
||||
{
|
||||
responseMap = new HashMap<>();
|
||||
defaultResponses = new ArrayList<>();
|
||||
fillResponseMap();
|
||||
fillDefaultResponses();
|
||||
randomGenerator = new Random();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a response from a given set of input words.
|
||||
*
|
||||
* @param words A set of words entered by the user
|
||||
* @return A string that should be displayed as the response
|
||||
*/
|
||||
public String generateResponse(HashSet<String> words)
|
||||
{
|
||||
Iterator<String> it = words.iterator();
|
||||
while(it.hasNext()) {
|
||||
String word = it.next();
|
||||
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 (what we say when
|
||||
// we cannot think of anything else to say...)
|
||||
return pickDefaultResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enter all the known keywords and their associated responses
|
||||
* into our response map.
|
||||
*/
|
||||
private void fillResponseMap()
|
||||
{
|
||||
responseMap.put("crash",
|
||||
"Well, it never crashes on our system. It must have something\n" +
|
||||
"to do with your system. Tell me more about your configuration.");
|
||||
responseMap.put("crashes",
|
||||
"Well, it never crashes on our system. It must have something\n" +
|
||||
"to do with your system. Tell me more about your configuration.");
|
||||
responseMap.put("slow",
|
||||
"I think this has to do with your hardware. Upgrading your processor\n" +
|
||||
"should solve all performance problems. Have you got a problem with\n" +
|
||||
"our software?");
|
||||
responseMap.put("performance",
|
||||
"Performance was quite adequate in all our tests. Are you running\n" +
|
||||
"any other processes in the background?");
|
||||
responseMap.put("bug",
|
||||
"Well, you know, all software has some bugs. But our software engineers\n" +
|
||||
"are working very hard to fix them. Can you describe the problem a bit\n" +
|
||||
"further?");
|
||||
responseMap.put("buggy",
|
||||
"Well, you know, all software has some bugs. But our software engineers\n" +
|
||||
"are working very hard to fix them. Can you describe the problem a bit\n" +
|
||||
"further?");
|
||||
responseMap.put("windows",
|
||||
"This is a known bug to do with the Windows operating system. Please\n" +
|
||||
"report it to Microsoft. There is nothing we can do about this.");
|
||||
responseMap.put("macintosh",
|
||||
"This is a known bug to do with the Mac operating system. Please\n" +
|
||||
"report it to Apple. There is nothing we can do about this.");
|
||||
responseMap.put("expensive",
|
||||
"The cost of our product is quite competitive. Have you looked around\n" +
|
||||
"and really compared our features?");
|
||||
responseMap.put("installation",
|
||||
"The installation is really quite straight forward. We have tons of\n" +
|
||||
"wizards that do all the work for you. Have you read the installation\n" +
|
||||
"instructions?");
|
||||
responseMap.put("memory",
|
||||
"If you read the system requirements carefully, you will see that the\n" +
|
||||
"specified memory requirements are 1.5 giga byte. You really should\n" +
|
||||
"upgrade your memory. Anything else you want to know?");
|
||||
responseMap.put("linux",
|
||||
"We take Linux support very seriously. But there are some problems.\n" +
|
||||
"Most have to do with incompatible glibc versions. Can you be a bit\n" +
|
||||
"more precise?");
|
||||
responseMap.put("bluej",
|
||||
"Ahhh, BlueJ, yes. We tried to buy out those guys long ago, but\n" +
|
||||
"they simply won't sell... Stubborn people they are. Nothing we can\n" +
|
||||
"do about it, I'm afraid.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Build up a list of default responses from which we can pick
|
||||
* if we don't know what else to say.
|
||||
*/
|
||||
private void fillDefaultResponses()
|
||||
{
|
||||
Charset charset = Charset.forName("US-ASCII");
|
||||
Path path = Paths.get(FILE_OF_DEFAULT_RESPONSES);
|
||||
try (BufferedReader reader = Files.newBufferedReader(path, charset)) {
|
||||
String response = reader.readLine();
|
||||
while(response != null) {
|
||||
defaultResponses.add(response);
|
||||
response = reader.readLine();
|
||||
}
|
||||
}
|
||||
catch(FileNotFoundException e) {
|
||||
System.err.println("Unable to open " + FILE_OF_DEFAULT_RESPONSES);
|
||||
}
|
||||
catch(IOException e) {
|
||||
System.err.println("A problem was encountered reading " +
|
||||
FILE_OF_DEFAULT_RESPONSES);
|
||||
}
|
||||
// Make sure we have at least one response.
|
||||
if(defaultResponses.size() == 0) {
|
||||
defaultResponses.add("Could you elaborate on that?");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
#BlueJ class context
|
||||
comment0.target=SupportSystem()
|
||||
comment0.text=\nCreates\ a\ technical\ support\ system.\n\n
|
||||
comment1.target=void\ start()
|
||||
comment1.text=\nStart\ the\ technical\ support\ system.\ This\ will\ print\ a\ welcome\ message\ and\ enter\ninto\ a\ dialog\ with\ the\ user,\ until\ the\ user\ ends\ the\ dialog.\n\n
|
||||
comment2.target=void\ printWelcome()
|
||||
comment2.text=\nPrint\ a\ welcome\ message\ to\ the\ screen.\n\n
|
||||
comment3.target=void\ printGoodbye()
|
||||
comment3.text=\nPrint\ a\ good-bye\ message\ to\ the\ screen.\n\n
|
||||
numComments=4
|
@@ -0,0 +1,74 @@
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* 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 generate responses.
|
||||
* It contains a loop that repeatedly reads input and generates
|
||||
* output until the users wants to leave.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling.
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
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 DodgySoft 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 a good-bye message to the screen.
|
||||
*/
|
||||
private void printGoodbye()
|
||||
{
|
||||
System.out.println("Nice talking to you. Bye...");
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
That sounds odd. Could you describe that problem in more detail?
|
||||
No other customer has ever complained about this before. What is your system configuration?
|
||||
That sounds interesting. Tell me more...
|
||||
I need a bit more information on that.
|
||||
Have you checked that you do not have a dll conflict?
|
||||
That is explained in the manual. Have you read the manual?
|
||||
Your description is a bit wishy-washy. Have you got an expert there with you who could describe this more precisely?
|
||||
That's not a bug, it's a feature!
|
||||
Could you elaborate on that?
|
@@ -0,0 +1,61 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=SupportSystem
|
||||
dependency1.to=InputReader
|
||||
dependency1.type=UsesDependency
|
||||
dependency2.from=SupportSystem
|
||||
dependency2.to=Responder
|
||||
dependency2.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=842
|
||||
package.editor.height=405
|
||||
package.editor.width=734
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=2
|
||||
package.numTargets=3
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=567
|
||||
readme.editor.width=834
|
||||
readme.editor.x=41
|
||||
readme.editor.y=23
|
||||
target1.editor.height=730
|
||||
target1.editor.width=875
|
||||
target1.editor.x=53
|
||||
target1.editor.y=60
|
||||
target1.height=60
|
||||
target1.name=InputReader
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=130
|
||||
target1.x=90
|
||||
target1.y=210
|
||||
target2.editor.height=725
|
||||
target2.editor.width=1005
|
||||
target2.editor.x=94
|
||||
target2.editor.y=23
|
||||
target2.height=60
|
||||
target2.name=Responder
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=120
|
||||
target2.x=350
|
||||
target2.y=210
|
||||
target3.editor.height=730
|
||||
target3.editor.width=872
|
||||
target3.editor.x=53
|
||||
target3.editor.y=60
|
||||
target3.height=60
|
||||
target3.name=SupportSystem
|
||||
target3.naviview.expanded=true
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=130
|
||||
target3.x=220
|
||||
target3.y=90
|
Reference in New Issue
Block a user