In Java
Let the CSV file be a large list of company names, addresses, grades and dates and let certain dates be in the wrong format so that you can filter it out later. We are using java code for this problem.
Purpose: The purpose of this assignment is to reinforce the topics of Exception Handling and File I/O. You will practice working with built-in exceptions as well as a custom-made exception. You will practice correct use of the try/catch block. You will also practice reading and writing data from a file.
Detailed Instructions:For this assignment, I will be giving you some classes already written, as well as a .csv file containing information about Los Angeles Restaurant and Facility Inspections from 2018. Using this data, you will write a program that will read the data, and display various results about the data.
Package Declaration and General Setup:
- Create a new project in Eclipse for this assignment.
- Create a package in your new project calledhw07.
- Do not forget to include theheader comment, as well as comments throughout your code documenting your work.
- NOTE: I am going to be more strict about documenting your code from now on. Failure to properly add a header comment to each .java file and other comments throughout will result in a deduction of points.
- Download the following .csv file. You will need this for your assignment. Make sure to place this file in the correct location in your project setup. (It should go in the project folder. Watch the videos from this week if you can't figure it out.) This file has data on restaurants and facilities in Los Angeles, specifically how well the facility did during its 2018 health inspection.NOTE:You are not allowed to change this file in anyway.
The Facility Class:
GENERAL NOTES:
- Download theFacility.javafile and add it to your hw07 package.
- Add your header comment to this class.
- For this class, I ask you to complete thesplitDate()method. Replace the comments at lines 45 and 46 with code that will take a String date in the format YYYY-MM-DD and correctly split it into its year, month, and day. Once you have the correct parts, assign each part to the appropriate data field.
- NOTE: ThesplitDate()method is the only method you are allowed to change and you are only allowed to add code inside theifpart of the if/else block. You arenot allowedto change any other code in this class, or add anything to this class. If you change this class in any way other than what is required, your submission will not be accepted, and you will receive a0for the assignment.
The DateFormatException Class:
GENERAL NOTES:
- Download theDateFormatException.javafile and add it to your hw07 package.
- Add your header comment to this class.
- This is a custom exception that I wrote.
- This class extends Exception, which means this will be acheckedexception. Make a note of where I use this exception in the Facility.java file.
- You arenot allowedto change any other code in this class, or add anything to this class. If you change this class in any way, your submission will not be accepted, and you will receive a0for the assignment.
The CSVReaderWriter Class:
GENERAL NOTES:
- This is a class that you will design yourself.
- This class will have only two methods, one for reading the data from a .csv file and one for writing the data to a .csv file.
- Declare this class to befinal. We do not want this class to be extended.
DATA FIELDS:
- This class shall havenodata fields and you are not allowed to add any data fields to this class.
CONSTRUCTORS:
- This class shall havea default
constructorwith no code in the body of the constructor.
- Make this constructorprivate. (We do not want to allow this class to be instantiated.)
METHOD
S:
readData():
- modifiers:
- This method shall bepublicandstatic.
- parameters:
- This method will take one parameter which will be a String. The string will be the file name of the .csv file that we want to read.
- No other parameters are allowed.
- return:
- This method shall return aFacilityDatabaseobject populated with Facility objects.
- implementation:
- This purpose of this method is to use file input to read the data of the .csv file given above.
- The .csv file lists its data in the following format:
- facility_name, facility_address, facility_zipcode, date_of_inspection, score, grade.
- This method shall read each line of the .csv file, parse the line into its separate pieces of data, create a Facility object for each line of data, and store each Facility object in theFacilityDatabase. Once the method is finished processing the file, it shall return theFacilityDatabase.
- other notes:
- You will need to use exception handling to implement this method.
- Some of the dates in the file are intentionally, incorrectly formatted. This method should be able to detect that there was a DateFormatException, prevent the creation of a Facility object for that piece of data, but continue to process the rest of the facilities in the .csv file.
- HINT: You may need to use nested try/catch statements to complete this method.
writeData():
- modifiers:
- This method shall bepublicandstatic.
- parameters:
- This method will take two parameters one that is aFacilityDatabaseobject and one that is a String. TheFacilityDatabaseis your pre-populated database of Facility objects and The string will be the file name of the .csv file that we want to write the data to. NOTE:DO NOTwrite to the same .csv file that you read from. Write to a new file with a different name.
- No other parameters are allowed.
- return:
- This method shall return nothing.
- implementation:
- The purpose of this method is to take yourFacilityDatabaseand write all of the data to anew.csv file. The new file should be inexactly the same
formatas the input .csv file (facility_name, facility_address, facility_zipcode, date_of_inspection, score, grade)
- One facility per line.
- No other methods necessary for this Class. If you wish to add some private helper methods, feel free.
The FacilityDatabase Class:
GENERAL NOTES:
- This class is used to store the data for each Facility in our input file
- This class shall be asubclassofArrayList. (Just like the previous assignment).
DATA FIELDS:
- This classis not allowedto have any data fields. If this class has any datafields, your assignment is invalidated and will receive no credit.
CONSTRUCTORS:
- This class shall have a default constructor. This constructor must exist, but will have no code in the body of the constructor.
METHOD
S:
displayFacilitiesWithGrade():
- modifiers: This method shall be a public instance method.
- parameters:
- This method shall take acharparameter. This parameter is a possible facility grade (A, B, etc).
- This method shall take anintparameter. This parameter is the limit on how many facilities to display (i.e. if the limit is given at 10, then only display the first 10 facilities that match the criteria.) If there are less facilities than the number given, simply display as many as you can. If the number given is -1, then display all facilities that match the criteria.
- No other parameters allowed.
- return: This method shall return nothing.
- implementation:
- This method shall iterate over the list of facilities and display only those facilities that have a grade indicated by the parameter value.
displayFacilitiesWithScoreGreater():
- modifiers: This method shall be a public instance method.
- parameters:
- This method shall take anintparameter. This parameter is a possible facility score. (90, 87, etc).
- This method shall take anotherintparameter. This parameter is the limit on how many facilities to display (i.e. if the limit is given at 10, then only display the first 10 facilities that match the criteria.) If there are less facilities than the number given, simply display as many as you can. If the number given is -1, then display all facilities that match the criteria.
- No other parameters allowed.
- return: This method shall return nothing.
- implementation:
- This method shall iterate over the list of facilities and display only those facilities that have a scoregreater than or equal tothe score indicated by the parameter value.
displayFacilitiesWithScoreLess():
- modifiers: This method shall be a public instance method.
- parameters:
- This method shall take anintparameter. This parameter is a possible facility score. (90, 87, etc).
- This method shall take anotherintparameter. This parameter is the limit on how many facilities to display (i.e. if the limit is given at 10, then only display the first 10 facilities that match the criteria.) If there are less facilities than the number given, simply display as many as you can. If the number given is -1, then display all facilities that match the criteria.
- return: This method shall return nothing.
- implementation:
- This method shall iterate over the list of facilities and display only those facilities that have a scoreless than or equal tothe score indicated by the parameter value.
displayFacilitiesByZipcode():
- modifiers: This method shall be a public instance method.
- parameters:
- This method shall take anintparameter. This parameter is a possible facility zipcode.
- This method shall take anotherintparameter. This parameter is the limit on how many facilities to display (i.e. if the limit is given at 10, then only display the first 10 facilities that match the criteria.) If there are less facilities than the number given, simply display as many as you can. If the number given is -1, then display all facilities that match the criteria.
- return: This method shall return nothing.
- implementation:
- This method shall iterate over the list of facilities and display only those facilities that have a zipcode indicated by the parameter value.
displayFacilitiesByMonth():
- modifiers: This method shall be a public instance method.
- parameters:
- This method shall take oneintparameter. This parameter is a possible facility inspection date month.
- This method shall take anotherintparameter. This parameter is the limit on how many facilities to display (i.e. if the limit is given at 10, then only display the first 10 facilities that match the criteria.) If there are less facilities than the number given, simply display as many as you can. If the number given is -1, then display all facilities that match the criteria.
- return: This method shall return nothing.
- implementation:
- This method shall iterate over the list of facilities and display only those facilities that have an inspection date month indicated by the parameter value.
displayCountsByGrade():
- modifiers: This method shall be a public instance method.
- parameters: This method shall takeno parameters.
- return: This method shall return nothing.
- implementation:
- This method shall iterate over the list of facilities and display how many facilities received each possible grade (A, B, and C are the only options).
- The output should look something like the following, (NOTE: These are not the correct counts).
A-Grade Facilities: 10 B-Grade Facilities: 4 C-Grade Facilities: 7
The Main Class:In your Main class, demonstrate that all of the following functionality is 100% working
- You are correctly able to use File I/O to read the data from the .csv file and populate a database of Facility objects.
- Part of this is correctly being able to read all of the data exception the data where the date is formatted incorrectly and will throw a DateFormatException.
- You are correctly able to use File I/O to write the data back to a new .csv file.
- All methods in the FacilityDatabase class are working:
- Show the results of calling each method using the following parameters:
- any methods with a limit, should be limited to the first 10 results.
- for displayFacilitiesWithGrade(), use a grade of C
- for displayFacilitiesWithScoreGreater(), use a score of 90
- for displayFacilitiesWithScoreLess(), use a score of 79
- for displayFacilitiesByZipcode() use a zipcode of 90032
- for displayFacilitiesByMonth() use a month of 5
- displayCountsByGrade() has no parameters so just show the results of this method.
- Make sure the output of your Main class, is easy to follow and organized. Make sure you main class is organized into different methods.