class UserInterface
extends java.lang.Object
This class provides helper methods to interact with the user.
Note that all methods in this class are static because we don't need to make
several UserInterface
objects and have them maintain their own
data. Instead, we simply need a collection of useful methods to create
windows that pop up one at a time, are used, and then are discarded.
Constructor and Description |
---|
UserInterface() |
Modifier and Type | Method and Description |
---|---|
private static java.lang.String |
convertToOutputFormat(NewsStory newsStory,
java.lang.String mediaType)
This method converts an individual story to the desired display format.
|
static java.lang.String |
createListOfNewsStoriesForNewsmaker(NewsMaker newsMaker,
java.lang.String mediaType,
java.lang.String[] sortCriteria)
This method takes a news maker and turns its list of news stories into a
String formated for display to the user or saving to a text
file. |
static void |
displayPieChartForNewsMaker(NewsMaker newsMaker,
java.lang.String mediaType,
java.lang.String content,
java.lang.String measure)
This method displays a pie chart for a given news maker.
|
static void |
presentNewspaperStories(java.lang.String listOfStories,
java.lang.String newsMakerName)
This method displays the list of news stories to the user at the console.
|
static boolean |
queryBoolean(java.lang.String message)
This method prompts the user (at the console) with whatever message is
passed in through the parameter
message , followed by the
String " (y/n)?". |
static java.lang.String |
queryFileName(java.lang.String message,
boolean mustExist)
This method prompts the user (at the console) with whatever message is
passed in through the parameter
message . |
static java.lang.String |
queryMultipleChoices(java.lang.String message,
java.lang.String[] options)
This method asks the user (at the console) whatever message is passed in
as a parameter.
|
static java.lang.String |
queryNewsMakerName(java.lang.String matchType)
This method asks the user for the name of a news maker using console
input and returns it.
|
static java.lang.String |
querySingleChoice(java.lang.String message,
java.lang.String[] options)
This method asks the user (at the console) whatever message is passed in
as a parameter.
|
static java.lang.String |
querySortCriterion(java.lang.String[] sortCriteria)
This method asks the user (at the console) for a sort criterion.
|
static void |
reportNewsmakerNotFound(NewsMaker queriedNewsMaker)
This method reports at the console that there were no relevant stories
found.
|
public static java.lang.String querySingleChoice(java.lang.String message, java.lang.String[] options) throws java.io.IOException
message
- The message to show to the user.options
- An array of the valid options (all Strings).java.io.IOException
- If something goes wrong reading a line from the console.public static java.lang.String queryMultipleChoices(java.lang.String message, java.lang.String[] options) throws java.io.IOException
message
- The message to show to the user.options
- An array of the valid options (all Strings).java.io.IOException
- If something goes wrong reading a line from the console.public static java.lang.String queryNewsMakerName(java.lang.String matchType) throws java.io.IOException
matchType
, which has "e" and "p"
as its two valid options.matchType
- Whether the name match will be exact (e) or partial (p).java.io.IOException
- If something goes wrong reading a line from the console.public static java.lang.String querySortCriterion(java.lang.String[] sortCriteria) throws java.io.IOException
String
objects specifying the
criteria already specified. If the first array object is an empty
String
(""), it will ask about the primary sort criterion.
If the first array object is a non-empty String
but the
second array object is an empty String
, it will ask for a
secondary sort criterion that isn't the sort criterion specified in the
first array object. If the first two array objects are non-empty
String
but the third array object is an empty
String
, it will ask for a tertiary sort criterion that isn't
one of the sort criteria specified by either of the first two array
objects. If the sort criteria array provided as a parameter is invalid,
it will throw an IllegalArgumentException
. The method loops
until a valid answer in given.sortCriteria
- The array of sort criteria already specified by the user (if
any).java.io.IOException
- If something goes wrong reading a line from the console.public static java.lang.String queryFileName(java.lang.String message, boolean mustExist) throws java.io.IOException
message
. This message should
ask for a filename.
If mustExist
is true, this method checks whether a file with
the given name exists and is readable. If so, it returns it. If not, it
alerts the user to the problem and asks again.
If mustExist
is false, any file name is allowed. This is
intended for writing to new files or existing files
If the user hits enter without entering any filename, it will cause the program to exit. This is intended for the case where the user doesn't have a needed file after all and just wants to exit.
message
- The message with which to prompt the user.mustExist
- A boolean specifying whether the file must exist for the name
chosen to be valid.java.io.IOException
- If something goes wrong reading a line from the console.public static boolean queryBoolean(java.lang.String message) throws java.io.IOException
message
, followed by the
String " (y/n)?". This message is intended to ask questions to which the
answer should be yes (y) or no (n) and it returns a boolean value of
true
or false
, respectively. The method loops
until a valid answer in given.message
- The message with which to prompt the user.java.io.IOException
- If something goes wrong reading a line from the console.public static java.lang.String createListOfNewsStoriesForNewsmaker(NewsMaker newsMaker, java.lang.String mediaType, java.lang.String[] sortCriteria)
String
formated for display to the user or saving to a text
file. At the end it includes a line summarizing the number of stories
found, the number of different news sources in which these stories were
published, the total length of these stories, and the number of different
topics found.
The summary line will have a slightly different format depending on media type specified.
newsMaker
- The news maker for which to create the story list as a string.mediaType
- The type of media (newspaper, TV news, online, or mixed, given
as one or more of n, t, or o in any order) that should be
included in the list.sortCriteria
- The primary, secondary, tertiary, and quaternary sort criteria
to use (source, topic, length, or date/time, given as s, t, l,
or d), in an array where element 0 corresponds to primary, 1
to secondary, 2 to tertiary, and 3 to quaternary.private static java.lang.String convertToOutputFormat(NewsStory newsStory, java.lang.String mediaType)
The returned line will have a slightly different format depending on media type specified and story type.
newsStory
- The story to convert to the display format.mediaType
- The type of media (newspaper, TV news, online, or mixed, given
as one or more of n, t, or o in any order) that should be
included in the list.public static void presentNewspaperStories(java.lang.String listOfStories, java.lang.String newsMakerName)
listOfStories
- The list of stories to display, all as one (potentially very
large) String.newsMakerName
- The name of the news maker (which is put into the title of the
JOptionPane).public static void reportNewsmakerNotFound(NewsMaker queriedNewsMaker)
"There are no relevant stories about news maker in this database."
queriedNewsMaker
- The news maker the user searched for.public static void displayPieChartForNewsMaker(NewsMaker newsMaker, java.lang.String mediaType, java.lang.String content, java.lang.String measure)
newsMaker
- The news maker whose stories are to be charted.mediaType
- The type(s) of media to chart.content
- The content field to chart.measure
- The measure to use in determining how to weight each story in
the chart.