Using Java, you will process shipping records received from a warehouse. These records are stored in a text file. Each
Posted: Thu May 05, 2022 1:58 pm
Using Java, you will process shipping records received from a
warehouse. These records are stored in a text file.
Each line in the text file represents a record, and each record
will have six fields. The pipe symbol will be used to delimit
the fields within the record. The structure of a record is as
follow:
Last name|first name|address|state|item description|qty|
Your code will read the file, and for each record, create a
ShippingRecord that contains the data. Then, add this
ShippingRecord to an ArrayList.
NOTE: The class diagram for ShippingRecord is missing the
setter for the state field (st). This setter will be public
like the others.
ShippingRecord
This class will represent a record from the file. As seen
in the class diagram, there are only setter methods. There’s
no need to implement the getters. Also, the class overrides
the toString() method in order to produce a formatted string.
See the section on toString() in Chapter 11. All you have to
do is create a toString() method in the ShippingRecord class and
return a formatted string. The string should be formatted as
shown at the bottom.
For this assignment, you do not have to implement the Comparable
interface.
Parser
The parser class has one method, parse(File). This method
will open the file, and create a ShippingRecord for each record in
the file. After processing all of the records, it will return
an ArrayList of ShippingRecords.
If the file passed in as input does not exist, this method will
throw a RecordException.
The parse(File) method should be able to handle a record field
with bad data. In fact, the data file you will use has one
bad record in it which will cause an InputMismatchException.
Your code should catch this exception, print an error message, and
continue to the next record. We do not want a bad record to
stop processing of the others.
A basic algorithm for the parse method is:
While the file is not empty
Create a shipping record
Set the fields based upon the record from the file.
Add record to list
End while
Return list
Use the Scanner class and the file parsing technique shown in
the book to read a line
RecordException
This exception should be a checked exception (see text for
meaning). It a very simple Exception class. There should be a
constructor that accepts a String representing a error message, and
this constructor forwards this String to it's superclass as shown
in listing 12.10. That's it for this class.
In our exercise, there is a possibility of at least two
exceptions. The Parser class will catch the
InputMismatchException and continue processing. But there is
also a chance of getting a FileNotFoundException when the parse()
method tries to open the file. If this exception occurs (and
it will, when you run the junit tests), your code must catch this
exception and rethrow it as a REcordException. Use the error
message "File not found!". (The junit test case will expect
this string).
TestParser
I will provide a JUnit test class, as well as a data file.
Note that the data file contains one record that will cause a
InputMismatchException. The expected output is shown below
for a (using the included data file):
Here is the data which should be in a text file the program
pulls from:
warehouse. These records are stored in a text file.
Each line in the text file represents a record, and each record
will have six fields. The pipe symbol will be used to delimit
the fields within the record. The structure of a record is as
follow:
Last name|first name|address|state|item description|qty|
Your code will read the file, and for each record, create a
ShippingRecord that contains the data. Then, add this
ShippingRecord to an ArrayList.
NOTE: The class diagram for ShippingRecord is missing the
setter for the state field (st). This setter will be public
like the others.
ShippingRecord
This class will represent a record from the file. As seen
in the class diagram, there are only setter methods. There’s
no need to implement the getters. Also, the class overrides
the toString() method in order to produce a formatted string.
See the section on toString() in Chapter 11. All you have to
do is create a toString() method in the ShippingRecord class and
return a formatted string. The string should be formatted as
shown at the bottom.
For this assignment, you do not have to implement the Comparable
interface.
Parser
The parser class has one method, parse(File). This method
will open the file, and create a ShippingRecord for each record in
the file. After processing all of the records, it will return
an ArrayList of ShippingRecords.
If the file passed in as input does not exist, this method will
throw a RecordException.
The parse(File) method should be able to handle a record field
with bad data. In fact, the data file you will use has one
bad record in it which will cause an InputMismatchException.
Your code should catch this exception, print an error message, and
continue to the next record. We do not want a bad record to
stop processing of the others.
A basic algorithm for the parse method is:
While the file is not empty
Create a shipping record
Set the fields based upon the record from the file.
Add record to list
End while
Return list
Use the Scanner class and the file parsing technique shown in
the book to read a line
RecordException
This exception should be a checked exception (see text for
meaning). It a very simple Exception class. There should be a
constructor that accepts a String representing a error message, and
this constructor forwards this String to it's superclass as shown
in listing 12.10. That's it for this class.
In our exercise, there is a possibility of at least two
exceptions. The Parser class will catch the
InputMismatchException and continue processing. But there is
also a chance of getting a FileNotFoundException when the parse()
method tries to open the file. If this exception occurs (and
it will, when you run the junit tests), your code must catch this
exception and rethrow it as a REcordException. Use the error
message "File not found!". (The junit test case will expect
this string).
TestParser
I will provide a JUnit test class, as well as a data file.
Note that the data file contains one record that will cause a
InputMismatchException. The expected output is shown below
for a (using the included data file):
Here is the data which should be in a text file the program
pulls from: