Question
This assignment is intended to give you practice in working with user-defined data types, and file input and output, in Java. Begin by downloading the
This assignment is intended to give you practice in working with user-defined data types, and file input and output, in Java.
Begin by downloading the attached input file. This file is an example of a character-separated values (CSV) data file, a format which is often used to store tables of information in text form. In CSV files, the "rows" (also known as records) correspond to information about a record, and the "columns" (also known as fields) within each row correspond to individual attributes of the record. These fields are joined with special characters, typically commas, which act as delimiters for the fields. In this particular file, the records correspond to order information for individual (fictitious) customers: the customer's name and address, their box type, and a series of shipping rates.
Notice that three shipping rates are provided on each line of the input file: "RateA", " RateB", and " RateC". These correspond to the three possible box types, but only one of them should be selected as the shipping rate for the order. To select the correct rate, check the customer's box type (this is in the "Box" column), which will be identified as "A", "B", or "C". If the customer's box type is "A", choose the first rate ("RateA"); if the box type is "B", choose the second rate ("RateB"); and if the box type is "C", choose the third rate ("RateC").
(Note: You should open this file only with a text editor, such as Visual Studio Code! If you have a spreadsheet such as Microsoft Excel installed on your workstation, it may own the association for CSV files, but you should use caution when viewing CSV files with spreadsheets. Your spreadsheet application may attempt to overwrite the file in a different format; if that happens, the data will be mangled and you will have to download a fresh copy from Canvas.)
You will write a simple Java program consisting of two classes, created in a source package called edu.jsu.mcis.cs310.participation1:
- A ParticipationAssignment1 class, containing the application logic and the main() method.
- A CustomerRecord class, for the data from each individual record of the input file. This class must include the necessary fields for the data in each record, as well as the necessary logic to decode the record data and retrieve the individual field values.
For every line (record) of the input file, your program should create a corresponding object of the CustomerRecord class. The constructor for this class should accept one entire line of CSV data as an argument, split this line into its individual field values, and use these values to initialize the object. (Later, we will see a better approach for working with tables of CSV data.)
Your CustomerRecord class will need to include a total of nine instance fields (members): the individual name and address fields, the box type, and the box price. (I suggest using the same field names shown in the header row, converted to lowercase.) All fields must be private, and your class must use appropriate data types for each; if a field contains a numeric value, it should be a numeric type and not a string. The ZIP code should be stored as a string, to preserve leading zeroes. Your class should also provide accessor methods ("getter methods") for every field, and a toString() method which returns the address and box price as a string, formatted as a standard address block followed by the shipping rate and a blank line. Your toString() method must use a formatter to format the box price as US currency, and it must use a StringBuilder to compose the output string. See the example output given below; the output from your program must exactly match this output!
(Refer to this tutorial (Links to an external site.) if you are unfamiliar with number formatting, and this tutorial (Links to an external site.) if you are unfamiliar with the StringBuilder class.)
Your ParticipationAssignment1 class should contain the main() method, as well as two additional public methods:
- getCustomerRecords(String filepath): This method should open the input file, read its contents one record at a time, and create an object of the CustomerRecord class for each record. (Remember to skip the header row, since it is not part of the data.) The complete path to the input file should be specified as a string argument. Once each CustomerRecord object has been created, it should be added to an ArrayList; once the end of the input file has been reached, this collection should be returned to the caller.
- printCustomerRecords(ArrayList records, String state): This method should accept the collection of records as an argument. It should begin by printing the number of records to the console, as shown in the example output, and it should then iterate every record in the collection, printing each record to the console. This method should also accept a state code (such as "AL") as a string argument; as it iterates through the records, it should count the number of orders with a state code that matches the one given in the argument. After all records have been printed, this total should be printed to the console. (The example shows the expected output for an argument of "AL", but your method should produce the correct results for any state.)
Below is a partial implementation of the ParticipationAssignment1 class to get you started. Your program should use this example as a starting point; in particular, the main()method shown below should be used without modification. Note that the two methods described above are doing most of the work; your main() method should simply call these methods, as shown here. There is also code in main() to get the complete path of the input file, assuming that the file has been downloaded to your workstation desktop; if you have it in some other location, you may make the necessary changes.
import java.io.File; import java.nio.file.Paths; import java.util.ArrayList; public class ParticipationAssignment1A { public ArrayList getCustomerRecords(String filepath) { /* INSERT YOUR CODE HERE */ } public void printCustomerRecords(ArrayList records, String state) { /* INSERT YOUR CODE HERE */ }
public static void main(String[] args) { ParticipationAssignment1A app = new ParticipationAssignment1A(); String filename = "input.csv"; String foldername = "Desktop"; String profilepath = System.getenv("USERPROFILE"); // for Windows if (profilepath == null) { profilepath = System.getProperty("user.home"); // for Unix } String sep = File.separator; String fullpath = profilepath + sep + foldername + sep + filename; ArrayList records = app.getCustomerRecords(fullpath); app.printCustomerRecords(records, "AL");
} }
Here is an example of what the output from your program should look like (only the first and last records out of the 500 in the file are shown):
Number of Records: 500 Martha C Zermeno 928 Arrowood Drive Jacksonville, FL 32217 $8.26 Erin F Smallwood 4474 Elk Street Los Angeles, CA 90017 $10.66 Dewayne J Brown 2403 Hurry Street Elkton, VA 22827 $47.43 Latrisha G Thornhill 1422 Oliver Street Fort Worth, TX 76118 $7.55 Larue J Villarreal 2907 Joy Lane Calabasas, CA 91302 $33.03 [... additional records should appear here ...] Lonnie V Hood 1535 Bedford Street Stamford, CT 06902 $8.10 Joyce B Ramsey 2951 Duck Creek Road San Bruno, CA 94066 $16.28 Number of AL Addresses: 7
The input file looks like this:
Note : I am not being able to upload the input file. If you can give me your email or whatever, I can send it there.
FirstName Middlelniti LastName StreetAddr City State Martha Zermeno 928 Arrow Jacksonvill FL Erin F Smallwoo 4474 Elk S Los Angel CA Dewayne J Brown 2403 Hurry Elkton VA Latrisha G Thornhill 1422 Olive Fort Worth TX Larue J Villarreal 2907 Joy L Calabasas CA Dorothy R George 1291 Kidd Port AlswO AK Walter J Smith 3922 Spru Burgettsto PA Rhonda B McLean 1775 Simo Sunbury PA Amber Hockman 5 Grim Av San Diego CA Michelle W Freeman 4611 Arch Green Riv WY Alisha J Charette 905 Ritter Fort Payn AL Carl Linville 2128 Short Georgetow TX Leroy J Swink 3640 Hillsi Wincheste MA Joshua T Pickett 2205 Elk C Indianapoli IN Cathy W Wilson 147 Memo Dekalb IL Linda D Dunson 1013 Upto Westbrook ME Sharon M Wilson 3322 Linco Lancaster PA Zip Code Box 32217 A 90017 B 22827 C 76118 A 91302 C 99653 B 15021 B 17801 C 92103 B 82935 B 35967 A 78626 A 01890 B 46205 B 60115 B 04092 B 17552 A RateA RateB RateC 8.26 13.37 37.38 7.55 10.66 33.03 9.97 16.28 47.43 7.55 10.66 33.03 7.55 10.66 33.03 5.84 8.1 23.62 5.44 7.25 19.89 7.55 10.66 33.03 7.55 10.66 33.03 5.84 8.1 23.62 7.55 10.66 33.03 8.26 13.37 37.38 5.84 8.1 23.62 6.16 6.16 6.16 7.55 33.03 9.97 16.28 5.84 Settin 10.66 8.1ctiva 23.62 ndonStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started