+1 (315) 557-6473 

Telephone Service simulation Homework with C++


Simulating a Telephone Service

Write a C++ homework where a company XYZ provides regular (wired) telephone service. Its service area is shown in the figure below. Each cell represents a region that is covered by a central office. The number in each cell represents the area code of the cell.

Area 1

In this project, you will write a program that simulates telephone service provided by a switch at the central office of area code 405. The switch receives a call from cell 405 and generates billing information based on how long the call has lasted, namely, call connection time.

Your program will generate a call by prompting user input for the destination phone number. Once getting a destination phone number, a call connection time must be generated by using a random number generator. Based on the call connection time, a service fee will be calculated in the following way: If a call is requested to a destination phone number with the same area code of 405, which is called a local call, the service rate is 2 cents per mins. Let us call this service rate the basic rate. If a call request is long-distance, i.e. to another area code, the service rate depends on the distance between the 405 cells and the destination cell. If these two cells are adjacent, the service rate is 1.25 times the basic rate. If the two cells are one-cell apart, the service rate is 1.75 times the basic rate. Table 1 shows an example of service rates according to the destination number of a call request

Table

Take a pause and draw the flow chart of the overall program.

The detailed requirements of your simulation program are as follows.

R1: User Input

Define the following function to get, validate user input and return a valid phone number.

 unsinged longget_phone_number();

Specifically, this function gest from user input for destination phone number by prompting the user with "Enter a 10-dit phone number (e.g. 4501234567): ", and check the length of the phone number by calling another function, get_phone_length(), and checking the area code by calling is_valid_areacode().

 unsigned char get_phone_length(unsigned int);

 boolis_valid_areacode(unsigned int);(function prototype)

 Your program will consider only 405, 520, 550, 620, and 650 as valid area codes that are shaded in Fig. 1. If the user enters an invalid phone number, displays "Invalid area code".

R2:Call Connection Time

Once you validate an input, generate call connection time by using a random number generator. Now, to be more realistic on call connection time, we want to restrict its value to the set of minutes,{1, 2, 3, ...,180}. Define the following function to get a call connection time

unsinged char get_call_time();

R3:Compute Service Charge

Once a call is terminated, compute the service charge based on the rates shown inTable 1. To do so, define the following function

double get_service_charge(unsigned long);

R4:Maximum Number of Valid Calls

Your program must run until 5valid calls have been served.

R5: Output for a Valid Call

After serving each valid call, display the call number (i.e. Same as the number of the served calls), destination phone number, connection time for that call request, and service charge. Design your own format for display and service charge must have two decimal places. The output for each required field must be aligned for all calls.

R6: Final Output

After serving 5valid calls, display the following information. Delineate this final output from R4 outputs.

R6-1: Call Statistics

This includes the total number of served calls, the number of the served local calls, the number of the served long-distance calls, and the average call connection time. The numeric values must be right-aligned and texts must be left-aligned; floating-point numbers must have two decimal places.

R6-2: Total Avenue

The total revenue must be displayed in units of dollars with currency signs. The text and numeric value must be aligned with the outputs from R6-1.

R7: No Number Literals

For the service rates, do not use literals. Instead, use constant variables.

Const double basic_rate= 0.02;

Solution:

#include < iostream> #include < iomanip> #include < sstream> #include < cmath> #include < random> #include < time.h> #include < string> using namespace std; // global variables and functions initialization #define MAXIMUM_CALL 5 const double basic_rate = 0.02; unsigned long long get_phone_number(); unsigned char get_phone_length(unsigned long long number); bool is_valid_areacode(unsigned int areacode); bool is_local_areacode(unsigned long long phone_number); double get_service_charge(unsigned long long phone_number, unsigned char call_time); unsigned char get_call_time(); // main program int main() { int number_served_calls = 0; int number_local_calls = 0; int number_long_distance_calls = 0; double sum_call_time = 0; double average_call_time = 0; double total_revenue = 0; while(number_served_calls < MAXIMUM_CALL) { unsigned long long phone_number = get_phone_number(); if(phone_number == 0) { continue; } else { number_served_calls++; unsigned char call_time = get_call_time(); double service_charge = get_service_charge(phone_number,call_time); cout << "\tcall count : " << number_served_calls << endl; cout << "\tdest.phone number : " << phone_number << endl; cout << "\tconnection time : " << (int)call_time << endl; cout << "\tservice charge : " << service_charge << endl; if(is_local_areacode(phone_number)) number_local_calls++; else number_long_distance_calls++; sum_call_time += call_time; total_revenue += service_charge; } } average_call_time = sum_call_time/number_served_calls; cout << "\n\n\n"; cout << "-----------------------------------STATISTICS-----------------------------------\n"; cout << setw(35) << std::left<< "Number of local calls: "<< setw(8)<> phone_number_str; phone_number = strtoull(phone_number_str.c_str(), 0, phone_number_str.length()); if(get_phone_length(phone_number) != 10) { cout << "\tinvalid phone number length: "<< (int)get_phone_length(phone_number) << "\n"; return 0; } unsigned int areacode=phone_number / 10000000; if(!is_valid_areacode(areacode)) { cout << "\tinvalid area code: "<< areacode <<"\n"; return 0; } return phone_number; } // input: the phone number // return: the length of phone number unsigned char get_phone_length(unsigned long long number) { return log10(number)+1; } // input: area code // return: true if the area code is 405, 520, 550, 620 or 650, otherwise false. bool is_valid_areacode(unsigned int areacode) { switch(areacode) { case 405: case 520: case 550: case 620: case 650: return true; default: return false; } } // input: phone number // return: true if the area code is 405 otherwise false. bool is_local_areacode(unsigned long long phone_number) { unsigned int areacode=phone_number / 10000000; if(areacode == 405) { return true; } else { return false; } } // input: None // return: a random call time (in minute) from 1 to 180. unsigned char get_call_time() { srand(time(NULL)); return (unsigned char)(rand() % 180 + 1); } // input: phone number and call time // return: the service charge according to the area code of the phone number and the call time times basic rate double get_service_charge(unsigned long long phone_number, unsigned char call_time) { unsigned int areacode=phone_number / 10000000; switch(areacode) { case 405: return basic_rate*call_time; case 520: case 550: return 1.25*basic_rate*call_time; case 620: case 650: return 1.75*basic_rate*call_time; default: return 0.0; } }

Output:

Phone