Defines method to add a cellphoneaccount object to the


Java Programming Assignment 8: Files, Exceptions, and Arrays

Problem Summary

A cell phone company specializes in cell phones for single people (i.e. plans for a single line). They would like to hire you to write a program to help them calculate cell phone bills and analyze data.

Each cell phone bill includes three components:

Phone charges (as defined below) for each cell phone

State and local taxes of 8.1% tax on phone charges (Note: 8.1% would be stored as 0.081)
$ 1.44 Universal Service fee

Phone charges are based on the type of plan. Two plans are available, and both include unlimited talk minutes.
P = Pay as you go plan (no texts or data included)
U = Unlimited talk & text plan (no charge for texts, 2 GB of data included) Phone charges are as follows for each plan:

Plan

Base Fee

Charge for texts

Charge for data

P

5.99

0.10 per text

$3.39 per GB*

U

59.99

No charge

$2.29 per GB over 2 GB*

*Charged by whole GB (not partial GB)

So be sure to round up to the nearest whole value before calculating charges

The company collets cell phone usage data monthly, and stores the data for each phone in a text data file. For each cell phone account, the file holds:

o The cell phone number - a 12-character String (###-###-####)
o The plan type (uppercased letter)
o The number of text messages sent and received
o The number of GB of data used, to one decimal place (e.g. 1.5) The number of lines in the data file is unknown, but will be less than 500.

Sample input data file lines:
303-333-3333 P 50 0.9
720-777-7777 U 444 1.5
720-222-2222 P 200 1.8
720-123-4567 U 2000 4.4

You may assume all data in input data file is valid data (i.e. formatted correctly), but you must verify that the file exists (i.e. can be opened). If the file does not exist, the program will loop until the user enters the filename for a file that can be opened.

The company would like you to write a program to read the file, calculate the account's monthly bill, and generate several reports.

The program will:

• Read the month's data from the file, line by line, and store the data into an array that holds cell phone account data objects (maximum of 500 cell phone account data objects).

• Calculate the month's total bill, based on the cell phone account data.

• Produce an output report file, containing the phone numbers and month's total bill for each account, along with the total collected from customers.

• Analyze the bills to determine the high, low, and average charged, and display the results.

Program Requirements

Overview of Required Classes and Methods

Three separate classes will be required for this program.

• A class to define a CellPhoneAccount

o Contains data fields, constructor, setters, and getters
o Defines method to calculate the bill total for one CellPhoneAccount object

• An AccountsListImpl class to implement the account array (to hold CellPhoneAccount objects)

o Contains data fields for the array and the number of objects in array
o Contains constructor and getter
o Defines method to add a CellPhoneAccount object to the array
o Defines method to read file data and use it to create objects and put them in the array
o Defines method to calculate bill totals for all CellPhoneAccount objects in the array

• A BillingAnalysis main class to run the program

o Instantiates an AccountsListImpl object
o Reads input data filename and uses AccountsListImpl object to call method to read the file data and fill the array.
o Uses AccountsListImpl object to calls methods to calculate all bill totals and write a billing report to a file.
o Calls static method to display a billing analysis summary
o Loops until user wants to quit

Project Requirements

Create a NetBeans project named: LastnameAssn8
             with a main class named: BillingAnalysis

Then define the classes and methods as follows:

1. Define a Java class with properties and methods for a cell phone account object, named:

CellPhoneAccount

The class will have a private data property for each item on the text file data line.

(i.e. Cell phone number, plan type, number of text messages sent and received, and number of GB of data used)

Additionally, it will have a billTotal private data property to hold the month's bill total.

Within the CellPhoneAccount class:

• Define a constructor, with parameters for each data property that is read from the text data file.

o Use the parameters to initialize the values.
o Initialize the billTotal to 0.

• Define getters and setters for each data property.

• Define an instance method to calculate and store the billTotal for a CellPhoneAccount object. The method will:

o Define constants for the calculations (from page 1 of this assignment).

- Constants will be defined for all fixed values (including base rates, included amounts, overage charges, universal fee, and tax rate). Be sure that you do not embed any constant values in the constant names.

o Use the data field values to calculate the month's total phone bill.
o Store the calculated value into the billTotal data property for the object.

2. Define a second class that will implement the cell phone accounts array for this program, named:

AccountsListImpl

The class will have the following public data properties:

- A static constant array size (maximum items that can be held in the array) set to 500

The class will have the following private data properties:

- An accountArray array (to hold CellPhoneAccount objects)
- A property to hold the number of CellPhoneAccount objects stored in the array

Note: The data property definitions will only define the array reference variable.

It will not create or initialize the array object.

Within the AccountsListImpl class:

• Define a constructor that will:

o Instantiate an accountArray array object (using new to initialize the array reference variable and the constant array size)
o Initialize the number of CellPhoneAccount objects stored in the array to 0

• Define a getter to return the number of objects stored in the array
• Define an instance method to add one CellPhoneAccount object to the accountArray. The method will:

o Have one parameter, the CellPhoneAccount object to add
o Test to see if the array is full

- If the array is full, will throw an ArrayIndexOutOfBoundsException

• Include a message to pass to the exception handler that states the array is full and stating which phone number cannot be added.

- Otherwise, store the CellPhoneAccount object into the accountArray and increment the number of objects stored in the array.

• Define an instance method to read and store all the data from the input file. The method will:

o Have one parameter: the name of the data input file (String)
o Try to open the input data file. If the file opened:

- Display a message that the program is reading and storing the file data
- In a loop:
- Read all data items from one line of the input data file
- Call the constructor to instantiate a new CellPhoneAccount object, passing in the data read from the file.
- Try to call the instance method to add the CellPhoneAccount object to the array (note: this is an instance method of the AccountsListImpl class)
- Catch any thrown ArrayIndexOutOfBoundsException exceptions.
- Display the message passed back via the throw.
- Also display a message that no more data will be read from the file.
- Set a value to exit the loop (so no more data will be read). Loop until there is no more file data or an exception is thrown.
- Close file.
- Display the number of CellPhoneAccount objects stored in the accountArray.

o Catch any FileNotFoundException exceptions. When caught:

- Display a message that says which file could not be opened.

• Define an instance method to loop through all the objects in the accountArray and calculate and store values for the billCharge data field.

The method will:

o For each CellPhoneAccount object stored in the array:

- Call the instance method to calculate the month's total phone bill (this is an instance method within the CellPhoneAccount class)

• Define an instance method to produce a billing report of monthly bill totals.

The method will create an output file with the billing report. The method should:

o Read the filename for the report output file from the user.
o Try to open the report output file

- If the file opened successfully, generate a report and write it to the output file.

- The output file report will contain a list of cell phone numbers and the bill charges associated with that phone numbers (accessed via getters), with the charges right- aligned on the decimal point.

- The last line of the output file will contain the total of all phone bill charges collected, right-aligned on the decimal point with the charges listed above it.

(see sample file output on next page)

Sample output file (using data from sample input data lines above)

303-333-3333 16.98
720-777-7777 66.29
720-222-2222 36.86
720-123-4567 73.72
Total 193.85

o Catch any FileIOException exceptions. When caught:

- Display a message that explains which file could not be opened and that a bill report will not be generated.

• Define three additional instance methods to:

o Determine and return the lowest bill charge in the accountArray
o Calculate and return the average bill charge in the accountArray
o Determine and return the highest bill charge in the accountArray

Note: You will need to use a getter to access the bill total in each object.within the array.

3. Within the BillingAnalysis main class:

• Define a static method to display a billing analysis summary, using the previously defined instance methods to calculate the low, high, and average bill charges.

o Parameters: the AccountsListImpl object
o The billing summary will be displayed as shown in the sample output on the next page.

- All figures will be right-aligned on the decimal point.

• Define a main method to:

o Display a description of what the program will do to the user.
o In a loop (outer loop):

- Create a new object of the AccountsListImpl class.
- In a loop (inner loop):
- Read the filename for the input data file from the user.
- Using the object, call the instance method to read and store the data from the input file (sends a message to the AccountsListImpl object).
- Get the number of CellPhoneAccount objects stored in the array.

Note that if the file could not be opened successfully, the data field that holds the number of CellPhoneAccount objects stored in the array will remain 0.

Loop, trying to read files, until the number of CellPhoneAccount objects stored in the array is not 0, indicating the file was read (i.e. the array now has data stored in it).

- Display a message to the user, that the program will calculate the monthly bills.
- Use the AccountsListImpl object to call:
- The instance method to loop through all the objects in the accountArray and calculate and store values the billCharge.
- The instance method to create the billing report of monthly bill totals.

- Display a blank line or two.
- Call the static method to display a billing analysis summary.
- Ask the user whether to run the program again, using a different input file. Loop until the user says s/he does not want to run the program again.

Sample Run (using sample input data lines from above)

Program will calculate cell phone bills, produce a report of bill totals, and display a billing analysis summary.

Enter input data filename: x Cannot open input file x
Enter input data filename: data.txt

Reading cell phone account data... Data stored for 4 cell phone accounts.

Calculating all phone bills...
Enter name of report output file: report.txt
Monthly Billing Analysis for 4 cell phone accounts: Lowest bill charge 16.98
Average bill charge 48.46
Highest bill charge 73.72

Run again (y/n)? n

WARNING: The objects, classes, and methods must be implemented exactly as specified above. If your program produces correct output, but you did not create and use the objects as specified, and implement the required classes and methods, you will lose a significant number of points.

See last pages for outline of code for the three required classes.

Coding Standards

The program must follow the CS210 Coding Standards from Content section 6.10.

Be sure to include the following comments:

o Comments at the top of each code file describing what the class does

- Include tags with the author's name (i.e. your full name) and the version of the code (e.g. version 1.0, Java Assn 4)

o Comments at the top of each method, describing what the method does

- Include tags with names and descriptions of each parameter and return value.

Testing

You will need to create test data files to test your program. Your test data files should test every possible execution path within your code, including erroneous data which cause exceptions.

For example, you could name the file accounts.txt. It will need to be placed in the top level of your
project directory, in order for your program to find it.

WARNING: Test data files that simply mimic the test data files from this requirements document will NOT be counted. You must come up with your own original test data files.

Before you submit your project, add your all files you used to test your program in the top level of your project directory (and add a number to the end of each file, if there are multiple test data files).

File examples: accounts1.txt
accounts2.txt (numbered, if multiple data files tested)

Submission

This programming assignment is due by midnight of the date listed in the Course Assignments by Week.

Programs submitted that do not compile without errors will not be accepted.

Again, you will submit a single zip file containing all of the files in your project.

• First export your project from NetBeans, as detailed in previous assignments

o Name your export file in the following format:
Assn.zip

For example: SmithAssn8.zip

NOTE: Save this zip file to some other directory, not your project directory.

• Then submit your .zip file to the Java Prog Assn 8 assignment submission folder (located under the Assignments/Dropbox tab in the online course).

o Warning: Only NetBeans export files will be accepted.

Do not use any other kind of archive or zip utility.

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Defines method to add a cellphoneaccount object to the
Reference No:- TGS02484996

Now Priced at $200 (50% Discount)

Recommended (94%)

Rated (4.6/5)