class NoozFileProcessor
extends java.lang.Object
This class provides helper methods to read a Nooz-style data file and turn it
into NewspaperStory
and NewsMaker
objects.
Note that the field newsMakers
and all methods in this class are
static because we don't need to make several NoozFileProcessor
objects and have them maintain their own lists of news makers. Instead, we
simply need a collection of useful methods to create a list and return it to
the calling method that will keep track of it.
Modifier and Type | Field and Description |
---|---|
private static NewsMakerList |
newsMakers
The list of news makers.
|
Constructor and Description |
---|
NoozFileProcessor() |
Modifier and Type | Method and Description |
---|---|
private static java.time.LocalDate |
decodeDate(java.lang.String dateString)
This method decodes the date, which is encoded in the file as YYYYMMDD.
|
private static java.lang.String |
decodeNewsmakerName(java.lang.String[] parts,
int startingIndex)
This method decodes a news maker name from one or more the parts of the
input line.
|
private static java.lang.String |
decodeNewspaper(java.lang.String newspaperCode)
This method decodes the newspaper name, which is represented by a numeral
in the data file.
|
private static java.lang.String |
decodeTopic(java.lang.String topicCode)
This method decodes the newspaper story topic, which is represented by a
numeral in the data file.
|
private static int |
decodeWordCount(java.lang.String wordCountString)
This method decodes the word count, which is encoded in the file as
numeral string.
|
private static void |
processLine(java.lang.String line)
The method for turning each line from a Nooz-style file into data and
saving that data.
|
static NewsMakerList |
readNoozFile(java.lang.String fileName)
The primary method for reading the Nooz-style data file.
|
private static NewsMakerList newsMakers
public static NewsMakerList readNoozFile(java.lang.String fileName) throws java.io.IOException
It opens the file, reads through it line by line until it reaches the
end, and closes the file. Each line is passed to processLine
to parse and turn into data that is actually added to the list.
Note that this program doesn't attempt to deal with I/O errors. This is allowable at this point to keep this project relatively simple and because we haven't covered this topic yet. However, this is something to be refined in the future.
fileName
- The name of the Nooz style data file to read.java.io.IOException
- If there is an I/O problem reading the data file.private static void processLine(java.lang.String line)
This method breaks the line into parts, passes the parts to appropriate helper methods that decode them and return data of appropriate types for the components of newspaper stories and news makers, puts these components together into a newspaper story and two news makers, and adds these into lists of newspaper stories and news makers, respectively.
line
- The line to process.private static java.time.LocalDate decodeDate(java.lang.String dateString)
This method catches NumberFormatExceptions
and deals with
them locally by printing messages to the error stream. In later
assignments, we'll cover better alternatives for handling exceptions.
dateString
- The encoded date.null
if the date cannot be
decoded.private static java.lang.String decodeNewspaper(java.lang.String newspaperCode)
Note that hard-coding the names is generally bad practice. It would be better to keep the mapping from numeral to newspaper name in a separate file that could be read and used at runtime, so that the list of numerals and corresponding names could be updated without modifying the code. However, to keep this project relatively simple, we went with this "quick and dirty" approach.
newspaperCode
- The numeral specifying the newspaper.private static int decodeWordCount(java.lang.String wordCountString)
This method catches NumberFormatExceptions
and deals with
them locally by printing messages to the error stream. In later
assignments, we'll cover better alternatives for handling exceptions.
wordCountString
- The word count as a String.private static java.lang.String decodeTopic(java.lang.String topicCode)
Note that hard-coding the topics is generally bad practice. It would be better to keep the mapping from numeral to topic in a separate file that could be read and used at runtime, so that the list of numerals and corresponding topics could be updated without modifying the code. However, to keep this project relatively simple, we went with this "quick and dirty" approach.
topicCode
- The numeral specifying the topic.private static java.lang.String decodeNewsmakerName(java.lang.String[] parts, int startingIndex)
Note that there are three distinctly different cases that need to be dealt with by this method.
parts
- The array of all of the parts of the line, which was separated
based on commas.startingIndex
- The starting index for the news maker name to decode. This can
vary for the second news maker based on whether the first news
maker name contained a comma.