Description
Specifications – Use arrays in this project; ArrayLists are not allowed!
Overview: Marketing Campaign – Part 3 is the third of a three-part software project that involves the
cost and reporting for marketing campaigns. The completed class hierarchy is shown in the UML class
diagram above.
Part 3 of the project focuses on handling exceptions that are thrown as a result of
erroneous input from the command line or the data file. In the MarketingCampaignPart3 class, the main
method, which reads in the file name as a command line argument, will need to handle a
FileNotFoundException that may result from attempting to open the file (e.g., if the file does not exist).
Also, the readFile method in MarketingCampaignList will need to handle exceptions that occur while
processing the data file, including a new exception called InvalidCategoryException.
• MarketingCampaign, DirectMC, IndirectMC, SearchEngineMC, SocialMediaMC, and
CampaignCostComparator, ROIComparator
Requirements and Design: There are no changes to these classes from Part 2.
• MarketingCampaignList.java
Requirements: The MarketingCampaignList class provides methods for reading in the data file and
generating the reports. The readFile method should redesigned to handle exceptions in the data.
Reading a “good” line of data results in a new element being added the MarketingCampaign array, and
reading a “bad” line of data results in the line + an exception message being added the to the invalid
records String array. A new report method produces the Invalid Records Report.
Design: The readFile method from Part 2 should be redesigned to handle exceptions. The
MarketingCampaignList class has fields, a constructor, and methods as outlined below.
(1) Fields: no change from Part 2.
(2) Constructor: no change from Part 2.
(3) Methods: The readFile method needs to be reworked and the generateInvalidRecordsReport
method needs to be added. See Part 2 for the full description of other methods in this class.
o readFile has no return value, accepts the data file name as a String, and throws
FileNotFoundException. If a FileNotFoundException occurs when attempting to open the
data file, it should be ignored in this method so that it can be handled in the calling method
(i.e., main). If a line from the file is processed successfully, a MarketingCampaign object of
the appropriate category (subclass) is added to the MarketingCampaign array in the class.
However, when an exception occurs as a result from erroneous data in a line read from the file,
it should be caught and handled as follows. The line should be concatenated with a newline
and the exception message and then the resulting String should be added to the invalid records
String array in the class.
The three exceptions that should be caught in this method are (1)
InvalidCategoryException (described below), (2) NumberFormatException, and (3)
NoSuchElementException. Note that the InvalidCategoryException must be explicitly thrown
by your code if the category is not D, I, S, or M (hint: the default case).
The
NumberFormatException will be thrown automatically if the item scanned in the line from the
file is not a double when Double.parseDouble expects it to be a double. The
NoSuchElementException will be thrown automatically if the item scanned does not exist (i.e.,
data is missing). For examples, see the output below for the Invalid Records Report.
o generateInvalidRecordsReport processes the invalid records array to produce the
Invalid Records Report and then returns the report as String. See the example result near the
end of the output for MarketingCampaignPart3 that begins on page 4 and ends on page 7.
Code and Test: See examples of exception handling in the text and the class notes. In the catch
blocks for the NumberFormatException and NoSuchElementException, the invalid line should be
concatenated with a newline and the exception message, and the resulting String should be added to
the invalidRecords String array. Note that for the NoSuchElementException, “: For missing input
data” will need to be concatenated to the end of the toString value of the NoSuchElementException
to form the complete message.
Download marketing_campaign_data_2_exceptions.csv from the assignment page in
Canvas to test your program. Your optional JUnit test methods should force the exceptions described
above to thrown and caught. Since the readFile method will propagate the FileNotFoundException if
the file is not found when the Scanner is created to read the file, test method could catch this exception
to check that it was thrown. Any other test method involving the readFile method must have the
throws FileNotFoundException clause.
InvalidCategoryException.java
Requirements and Design: The InvalidCategoryException class defines a new subclass of the
Exception class. The constructor accepts a String categoryIn representing the invalid category,
then invokes the super constructor with the message:
“For category: ” + categoryIn
See examples of creating user defined exceptions in text and class notes.
MarketingCampaignPart3.java
Requirements: The MarketingCampaignPart3 class contains the main method for running the
program. In addition to the specifications in Part 2, the main method should be modified as indicated
below.
Design: The MarketingCampaignPart3 class is the driver class and has a main method described
below.
o main accepts a file name as a command line argument, then within a try block, creates a
MarketingCampaignList object, and then invokes its methods to (1) read the file and process
the wireless network records and (2) to generate and print the four reports as shown in the
third run in example output beginning on page 4. If no command line argument is provided,
the program should indicate this and end as shown in the first run in the example output on
page 4.
If an FileNotFoundException is thrown in the readFile method in the
MarketingCampaignList class, it should be caught in the catch block of the try statement in
main. The catch block should print a message (“*** Attempted to read file: ” along with the
exception’s message).
For example, if the user entered “nofile.csv” as the command line
argument and this file does not exit, then the Run I/O in jGRASP would look like the second
run in the example output beginning on page 4). Note that since the main method is catching
FileNotFoundException, it no longer needs the throws clause in its declaration.
Code and Test: See examples of exception handling in the text and the class notes. Download
marketing_campaign_data_2_exceptions.csv from the assignment page in Canvas to
test your program. One of your optional JUnit test methods should call your main method with no
argument (i.e., an empty String array).
Another should call your main method with an argument that is
not a valid file name to ensure that your catch block is covered. Finally, a third should call your main
method with an argument that is the file name above. See “Code and Test” for
MarketingCampaignPart2 in Part 2 to see how to invoke your main method.
Example Output
Three separate runs are shown below: (1) one with no command line argument, (2) one with an invalid
file name as command line argument, and (3) one with valid file name as command line argument.
MMMM
MM«M —-jGRASP exec: java MarketingCampaignPart3
MM§MFile name expected as command line argument.
MM§MProgram ending.
MM§M
MM©M —-jGRASP: operation complete.
MMMM
MM«M —-jGRASP exec: java MarketingCampaignPart3 nofile.csv
MM§M *** Attempted to read file: nofile.csv (No such file or directory)
MM§M
MM©M —-jGRASP: operation complete.
MM«M —-jGRASP exec: java MarketingCampaignPart3 marketing_campaign_data_2_exceptions.csv
MM§M——————————-
MM§MMarketing Campaign Report
MM§M——————————-
MM§M
MM§MWeb Ads 3 (class SocialMediaMC)
MM§MRevenue: $35,000.00 Campaign Cost: $27,000.00 ROI: 29.63%
MM§M Base Cost: $3,000.00
MM§M Ad Cost: $24,000.00 = $3.00 per ad * 8000 ads
MM§M
MM§MAd Mailing (class DirectMC)
MM§MRevenue: $10,000.00 Campaign Cost: $7,000.00 ROI: 42.86%
MM§M Base Cost: $1,000.00
MM§M Mail Cost: $6,000.00 = $3.00 per mail piece * 2000 mail pieces
MM§M
M§MWeb Ads 4 (class IndirectMC)
MM§MRevenue: $5,000.00 Campaign Cost: $3,750.00 ROI: 33.33%
MM§M Base Cost: $1,500.00
MM§M Ad Cost: $2,250.00 = $2.25 per ad * 1000 ads
MM§M
MM§MWeb Ads 1 (class IndirectMC)
MM§MRevenue: $15,000.00 Campaign Cost: $8,500.00 ROI: 76.47%
MM§M Base Cost: $1,500.00
MM§M Ad Cost: $7,000.00 = $2.00 per ad * 3500 ads
MM§M
MM§MWeb Ads 2 (class SearchEngineMC)
MM§MRevenue: $27,500.00 Campaign Cost: $14,500.00 ROI: 89.66%
MM§M Base Cost: $2,000.00
MM§M Ad Cost: $12,500.00 = $2.50 per ad * 5000 ads
MM§M
MM§M—————————————–
MM§MMarketing Campaign Report (by Name)
MM§M—————————————–
MM§M
MM§MAd Mailing (class DirectMC)
MM§MRevenue: $10,000.00 Campaign Cost: $7,000.00 ROI: 42.86%
MM§M Base Cost: $1,000.00
MM§M Mail Cost: $6,000.00 = $3.00 per mail piece * 2000 mail pieces
MM§M
MM§MWeb Ads 1 (class IndirectMC)
MM§MRevenue: $15,000.00 Campaign Cost: $8,500.00 ROI: 76.47%
MM§M Base Cost: $1,500.00
MM§M Ad Cost: $7,000.00 = $2.00 per ad * 3500 ads
MM§M
MM§MWeb Ads 2 (class SearchEngineMC)
MM§MRevenue: $27,500.00 Campaign Cost: $14,500.00 ROI: 89.66%
MM§M Base Cost: $2,000.00
MM§M Ad Cost: $12,500.00 = $2.50 per ad * 5000 ads
MM§M
MM§MWeb Ads 3 (class SocialMediaMC)
MM§MRevenue: $35,000.00 Campaign Cost: $27,000.00 ROI: 29.63%
MM§M Base Cost: $3,000.00
MM§M Ad Cost: $24,000.00 = $3.00 per ad * 8000 ads
MM§M
MM§MWeb Ads 4 (class IndirectMC)
MM§MRevenue: $5,000.00 Campaign Cost: $3,750.00 ROI: 33.33%
MM§M Base Cost: $1,500.00
MM§M Ad Cost: $2,250.00 = $2.25 per ad * 1000 ads
MM§M
MM§M————————————————-
MM§MMarketing Campaign Report (by Lowest Campaign Cost)
MM§M————————————————-
MM§M
MM§MWeb Ads 4 (class IndirectMC)
MM§MRevenue: $5,000.00 Campaign Cost: $3,750.00 ROI: 33.33%
MM§M Base Cost: $1,500.00
MM§M Ad Cost: $2,250.00 = $2.25 per ad * 1000 ads
MM§M
MM§MAd Mailing (class DirectMC)
MM§MRevenue: $10,000.00 Campaign Cost: $7,000.00 ROI: 42.86%
MM§M Base Cost: $1,000.00
MM§M Mail Cost: $6,000.00 = $3.00 per mail piece * 2000 mail pieces
MM§M
MM§MWeb Ads 1 (class IndirectMC)
MM§MRevenue: $15,000.00 Campaign Cost: $8,500.00 ROI: 76.47%
MM§M Base Cost: $1,500.00
MM§M Ad Cost: $7,000.00 = $2.00 per ad * 3500 ads
MM§M
MM§MWeb Ads 2 (class SearchEngineMC)
MM§MRevenue: $27,500.00 Campaign Cost: $14,500.00 ROI: 89.66%
MM§M Base Cost: $2,000.00
MM§M Ad Cost: $12,500.00 = $2.50 per ad * 5000 ads
MM§M
MM§MWeb Ads 3 (class SocialMediaMC)
MM§MRevenue: $35,000.00 Campaign Cost: $27,000.00 ROI: 29.63%
MM§M Base Cost: $3,000.00
MM§M Ad Cost: $24,000.00 = $3.00 per ad * 8000 ads
MM§M
MM§M————————————————-
MM§MMarketing Campaign Report (by Highest ROI)
MM§M————————————————-
MM§M
MM§MWeb Ads 2 (class SearchEngineMC)
MM§MRevenue: $27,500.00 Campaign Cost: $14,500.00 ROI: 89.66%
MM§M Base Cost: $2,000.00
MM§M Ad Cost: $12,500.00 = $2.50 per ad * 5000 ads
MM§M
MM§MWeb Ads 1 (class IndirectMC)
MM§MRevenue: $15,000.00 Campaign Cost: $8,500.00 ROI: 76.47%
MM§M Base Cost: $1,500.00
MM§M Ad Cost: $7,000.00 = $2.00 per ad * 3500 ads
MM§M
MM§MAd Mailing (class DirectMC)
MM§MRevenue: $10,000.00 Campaign Cost: $7,000.00 ROI: 42.86%
MM§M Base Cost: $1,000.00
MM§M Mail Cost: $6,000.00 = $3.00 per mail piece * 2000 mail pieces
MM§M
MM§MWeb Ads 4 (class IndirectMC)
MM§MRevenue: $5,000.00 Campaign Cost: $3,750.00 ROI: 33.33%
MM§M Base Cost: $1,500.00
MM§M Ad Cost: $2,250.00 = $2.25 per ad * 1000 ads
MM§M
MM§MWeb Ads 3 (class SocialMediaMC)
MM§MRevenue: $35,000.00 Campaign Cost: $27,000.00 ROI: 29.63%
MM§M Base Cost: $3,000.00
MM§M Ad Cost: $24,000.00 = $3.00 per ad * 8000 ads
MM§M
MM§M———————-
MM§MInvalid Records Report
MM§M———————-
MM§M
MM§MD,Ad Mailing,1000.00,3.00,2000.0
MM§Mjava.lang.NumberFormatException: For input string: “2000.0”
MM§M
MM§MZ,Web Ads X,30500.0,2.75,6500
MM§MInvalidCategoryException: For category: Z
MM§M
MM§MI,Web Ads 4,500).0,2.25,1000
MM§Mjava.lang.NumberFormatException: For input string: “500).0”
MM§M
MM§MI,Web Ads 1,15000.0,2.0
MM§Mjava.util.NoSuchElementException: For missing input data
MM§M
MM§M
MM©M —-jGRASP: operation complete.
Notes
1. This project assumes that you are reading each double value as a String using next() and then parsing
it into a double with Double.parseDouble(…) as shown in the following example.
. . . Double.parseDouble(myInput.next());
This form of input will throw a java.lang.NumberFormatException if the value is not a double.
If you are reading in each double value as a double using nextDouble(), for example
. . . myInput.nextDouble();
then a java.util.InputMismatchException will be thrown if the value read in is not a double.
For this assignment, you should change your input to use Double.parseDouble(…) rather than
nextDouble(), since Web-CAT is looking for NumberFormatException rather than
java.util.InputMismatchException.
2. If you are using the JUnit Assert.assertArrayEquals method to check two MarketingCampaign arrays
for equality, then the equals and hashCode methods must be implemented in your
MarketingCampaign class; that is, Assert.assertArrayEquals calls equals(Object obj) on each object in
the array, so MarketingCampaign must have an equals method that overrides the one inherited from
the Object class. If the MarketingCampaign class does not override equals(Object obj), then the JUnit
Assert.assertArrayEquals method will use the inherited equals(Object obj) method which
means two MarketingCampaign arrays will be equal only if they are the same object (i.e., aliases).
Below is a simplified equals method and hashCode method you are free to use.
public boolean equals(Object obj) {
if (!(obj instanceof MarketingCampaign)) {
return false;
}
else {
MarketingCampaign c = (MarketingCampaign) obj;
return (name.equalsIgnoreCase(c.getName())); `
}
}
public int hashCode() {
return 0;
}