first commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=InputReader()
|
||||
comment0.text=\n\ Create\ a\ new\ InputReader\ that\ reads\ text\ from\ the\ text\ terminal.\n
|
||||
comment1.params=
|
||||
comment1.target=java.util.HashSet\ getInput()
|
||||
comment1.text=\n\ Read\ a\ line\ of\ text\ from\ standard\ input\ (the\ text\ terminal),\n\ and\ return\ it\ as\ a\ set\ of\ words.\n\n\ @return\ \ A\ set\ of\ Strings,\ where\ each\ String\ is\ one\ of\ the\ \n\ \ \ \ \ \ \ \ \ \ words\ typed\ by\ the\ user\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 Michael Kölling and David J. Barnes
|
||||
* @version 1.0 (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,30 @@
|
||||
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 chapter 6.
|
||||
|
||||
|
||||
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,16 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=Responder()
|
||||
comment0.text=\n\ Construct\ a\ Responder\n
|
||||
comment1.params=words
|
||||
comment1.target=java.lang.String\ generateResponse(java.util.HashSet)
|
||||
comment2.params=
|
||||
comment2.target=void\ fillResponseMap()
|
||||
comment2.text=\n\ Enter\ all\ the\ known\ keywords\ and\ their\ associated\ responses\n\ into\ our\ response\ map.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ fillDefaultResponses()
|
||||
comment3.text=\n\ Build\ up\ a\ list\ of\ default\ responses\ from\ which\ we\ can\ pick\ one\n\ if\ we\ don't\ know\ what\ else\ to\ say.\n
|
||||
comment4.params=
|
||||
comment4.target=java.lang.String\ pickDefaultResponse()
|
||||
comment4.text=\n\ Randomly\ select\ and\ return\ one\ of\ the\ default\ responses.\n\ @return\ \ \ \ \ A\ random\ default\ response\n
|
||||
numComments=5
|
||||
@@ -0,0 +1,142 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* 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 Michael Kölling and David J. Barnes
|
||||
* @version 1.0 (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;
|
||||
private Random randomGenerator;
|
||||
|
||||
/**
|
||||
* Construct a Responder
|
||||
*/
|
||||
public Responder()
|
||||
{
|
||||
responseMap = new HashMap<>();
|
||||
defaultResponses = new ArrayList<>();
|
||||
fillResponseMap();
|
||||
fillDefaultResponses();
|
||||
randomGenerator = new Random();
|
||||
}
|
||||
|
||||
|
||||
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 (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("mac",
|
||||
"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 one
|
||||
* if we don't know what else to say.
|
||||
*/
|
||||
private void fillDefaultResponses()
|
||||
{
|
||||
defaultResponses.add("That sounds odd. Could you describe that problem in more detail?");
|
||||
defaultResponses.add("No other customer has ever complained about this before. \n" +
|
||||
"What is your system configuration?");
|
||||
defaultResponses.add("That sounds interesting. Tell me more...");
|
||||
defaultResponses.add("I need a bit more information on that.");
|
||||
defaultResponses.add("Have you checked that you do not have a dll conflict?");
|
||||
defaultResponses.add("That is explained in the manual. Have you read the manual?");
|
||||
defaultResponses.add("Your description is a bit wishy-washy. Have you got an expert\n" +
|
||||
"there with you who could describe this more precisely?");
|
||||
defaultResponses.add("That's not a bug, it's a feature!");
|
||||
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,13 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=SupportSystem()
|
||||
comment0.text=\n\ Creates\ a\ technical\ support\ system.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ start()
|
||||
comment2.params=
|
||||
comment2.target=void\ printWelcome()
|
||||
comment2.text=\n\ Print\ a\ welcome\ message\ to\ the\ screen.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ printGoodbye()
|
||||
comment3.text=\n\ Print\ a\ good-bye\ message\ to\ the\ screen.\n
|
||||
numComments=4
|
||||
@@ -0,0 +1,70 @@
|
||||
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 Michael Kölling and David J. Barnes
|
||||
* @version 1.0 (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();
|
||||
}
|
||||
|
||||
|
||||
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,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=756
|
||||
package.editor.height=407
|
||||
package.editor.width=648
|
||||
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=651
|
||||
readme.editor.width=882
|
||||
readme.editor.x=53
|
||||
readme.editor.y=23
|
||||
target1.editor.height=777
|
||||
target1.editor.width=1189
|
||||
target1.editor.x=53
|
||||
target1.editor.y=23
|
||||
target1.height=60
|
||||
target1.name=InputReader
|
||||
target1.naviview.expanded=false
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=110
|
||||
target1.x=90
|
||||
target1.y=210
|
||||
target2.editor.height=1049
|
||||
target2.editor.width=1250
|
||||
target2.editor.x=811
|
||||
target2.editor.y=24
|
||||
target2.height=60
|
||||
target2.name=Responder
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=100
|
||||
target2.x=400
|
||||
target2.y=210
|
||||
target3.editor.height=777
|
||||
target3.editor.width=1189
|
||||
target3.editor.x=66
|
||||
target3.editor.y=125
|
||||
target3.height=60
|
||||
target3.name=SupportSystem
|
||||
target3.naviview.expanded=true
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=140
|
||||
target3.x=230
|
||||
target3.y=90
|
||||
Reference in New Issue
Block a user