Question

c++ Program Description You are going to write a computer program/prototype to process mail packages that...

c++ Program Description

You are going to write a computer program/prototype to process mail packages that are sent to different cities. For each destination city, a destination object is set up with the name of the city, the count of packages to the city and the total weight of all the packages. The destination object is updated periodically when new packages are collected. You will maintain a list of destination objects and use commands to process data in the list.

Commands Description

You will read and process commands from the standard input, stopping when the Quit command is entered. Commands can have parameters. The commands you are to implement are of the form:

      Add city

      Output city

      Update city package_count package_weight

      Clear city

      Quit

  1. Add command: Add a destination object for the given city to the end of the list if a destination object for that city doesn't already exist and the list isn't full. If the list is full or a destination object for that city already exists, still read in the data; however, don't do anything with the data read in (toss it!), and print an error message (check the full condition first). Be sure to initialize the new destination object before adding it to the list.
  2. Output command: Output the average package weight of the destination object for the specified city. If the destination object for the given city doesn't exist, print an appropriate message. See the sample outputs below for the exact message.
  3. Update command: Update the destination object for the specified city by adding the given package_count and package_weight to the count and weight respectively. If the destination object doesn't exist, print an appropriate message.
  4. Clear command: Reset the count and weight of the destination object for the specified city to zero, but do not change the city name. If the destination object doesn't exist, print an appropriate message.
  5. Quit command: Terminate the program.

You can assume all inputs are formatted correctly. In particular, you do not need to check for invalid commands.

Requirements

0.   Your project should have separate header file and implementation file for each class used! Failure to do so will result in 0 credit!

1.      You must use the following declarations:

const int MAX_CITIES = 5;

const int NOT_FOUND = -1;

// Write a comment block here that describes this class.

class Destination

{

   string city;        // the name of the city where packages are to be sent

   int package_count; // count of packages

   float total_weight; // total weight of all packages

   . . . // remove this comment and add properly-commented methods which are described below.

};

// Write a comment block here that describes this class.

class DestinationList

{

   int num;                       // number of objects in the list

   Destination list[MAX_CITIES]; // array of destinations

   . . . // remove this comment and add properly-commented methods which are described below.

};

2.      You cannot add other data members to this Destination class. Failure to comply will result in a zero (0). We suggest you copy/paste these into your program!

3.      You are only allowed to manipulate a Destination's data member via the following four methods and one constructor.  These methods MUST be placed in the class Destination. Do NOT forget to complete the “TODO” portions indicated below.  You will lose points if you forget to finish these TODOs!

// This constructor initializes the city to name and

// both the package count and total weight to 0.

// params: TODO

Destination( string name )

// Adds numPackages and packageWeight to the package count and total weight, respectively.

// params: TODO

void RecordShipment( int numPackages, float packageWeight )

     

// Returns true if the object's city is the same as name; false otherwise

// params: TODO

bool DestinationHasName( string name )

     

// Zeroes the package count and total weight.

void ClearShipment( )

     

// returns average weight of the packages in a shipment, or 0.0 if there

// are no packages

float AverageWeight( )

4.      You must properly complete and comment these methods. None of these methods are allowed to print or read any data.

5.      For the DestinationList class, you must write a search function. The function must have one parameter: a string containing a city name. The function is to determine whether or not an object with the specified city name is in the list, returning the index of the city if it is found or a -1 if it is not. Furthermore, the function cannot read or print data. For this DestinationList class, you should have a default constructor and public methods, to add, update and output information maintained by this class.

6.      Lastly, the main program should ONLY have 2 variables. One of those variables will be of type DestinationList and the other of type string to hold the command.

7.      Display all float values with 2 decimal digits. Use

            cout << fixed << showpoint << setprecision(2);

8.      See the sample output for the exact wording of all messages.

9.      To get minimal credit for the assignment, your solution must work on both Test 1 and Test 2.

10.   Don't forget to follow the programming standards.

Sample Input (Test 1)

Add Madison

Add Chicago

Update Madison    2 60.75

Update Chicago    4 180

Update Chicago    3 46.1

Output Chicago

Output Madison

Add Dubuque

Update Dubuque    4 400

Clear Chicago

Output Chicago

Update Dubuque    2 106.44

Output Dubuque

Quit

Sample Output (Test 1)

Add Madison

Madison added to the list.

Add Chicago

Chicago added to the list.

Update Madison    2 60.75

Destination Madison updated with 2 packages weighing 60.75 lbs.

Update Chicago    4 180

Destination Chicago updated with 4 packages weighing 180.00 lbs.

Update Chicago    3 46.1

Destination Chicago updated with 3 packages weighing 46.10 lbs.

Output Chicago

Average weight of all packages to Chicago: 32.30

Output Madison

Average weight of all packages to Madison: 30.38

Add Dubuque

Dubuque added to the list.

Update Dubuque    4 400

Destination Dubuque updated with 4 packages weighing 400.00 lbs.

Clear Chicago

Output Chicago

Average weight of all packages to Chicago: 0.00

Update Dubuque    2 106.44

Destination Dubuque updated with 2 packages weighing 106.44 lbs.

Output Dubuque

Average weight of all packages to Dubuque: 84.41

Quit

Normal termination.

Sample Input (Test 2)

Add Madison

Add Chicago

Add Dubuque

Add Orlando

Add Madison

Add Miami

Add Denver

Update Chicago 3 40.5

Update Madison 2 50.79

Output Chicago

Output Denver

Update Orlando 38 482.12

Update Denver 10 100.33

Clear Chicago

Output Chicago

Clear Denver

Quit

Sample Output (Test 2)

Add Madison

Madison added to the list.

Add Chicago

Chicago added to the list.

Add Dubuque

Dubuque added to the list.

Add Orlando

Orlando added to the list.

Add Madison

Madison is already in the list.

Add Miami

Miami added to the list.

Add Denver

Denver not added. List is full.

Update Chicago 3 40.5

Destination Chicago updated with 3 packages weighing 40.50 lbs.

Update Madison 2 50.79

Destination Madison updated with 2 packages weighing 50.79 lbs.

Output Chicago

Average weight of all packages to Chicago: 13.50

Output Denver

Cannot output. Denver is not in the list.

Update Orlando 38 482.12

Destination Orlando updated with 38 packages weighing 482.12 lbs.

Update Denver 10 100.33

Cannot update. Denver is not in the list.

Clear Chicago

Output Chicago

Average weight of all packages to Chicago: 0.00

Clear Denver

Cannot clear. Denver is not in the list.

Quit

Normal termination.

Homework Answers

Answer #1

C++ code:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


const int MAX_CITIES = 5; // Stores max cities.
const int NOT_FOUND = -1; // Stores the "not found" variable.


// This class holds the information to a Destination.
class Destination
{
private:
string city; // the name of the city where packages are to be sent
int package_count; // count of packages
float total_weight; // total weight of all packages

public:
// This constructor initializes the city to name and
// both the package count and total weight to 0.
// params: in
Destination( string name )
{
city = name;
package_count = 0;
total_weight = 0;
}

// Adds numPackages and packageWeight to the package count and total
// weight, respectively.
// params: in, in
void RecordShipment( int numPackages, float packageWeight )
{
package_count += numPackages;
total_weight += packageWeight;
}

// Returns true if the object's city is the same as name;
// false otherwise
// params: in
bool DestinationHasName( string name )
{
return ( city == name );
}
// Zeroes the package count and total weight.
void ClearShipment( )
{
package_count = 0;
total_weight = 0.0;
}

// returns average weight of the packages in a shipment,
// or 0.0 if there are no packages
float AverageWeight( )
{
if( package_count == 0 )
return float( package_count );
else
return total_weight / package_count;
}
};


// This class holds a list of Densination ojects.
class DestinationList
{
private:
int num; // number of objects in the list
Destination list[MAX_CITIES]; // array of destinations

// Searches city names to make sure there isn't another with same name.
int SearchCity( string name )
{
int position = NOT_FOUND; // Store the position of the city.
for( int i = 0; i < MAX_CITIES; i++ )
{
if( list[i].DestinationHasName( name ) )
position = i;
}
return position;
}

public:
DestinationList ()
{
num = 0; // Stores the total number of cities
}

// Adds a new city as a destination object at the end of the list of
// destinations.
void AddCity()
{
string cityToAdd(); // Stores the city to add to the list.
cin >> cityToAdd;

if( num == MAX_CITIES )
cout << cityToAdd << " not added. List is full." << endl;
else
{
if( SearchCity( cityToAdd ) == NOT_FOUND )
{
Destination loc( cityToAdd ); // Object to store city as a
list[num] = loc; // Destination.
cout << cityToAdd << " added to the list." << endl;
num++;
}
else
cout << cityToAdd << " is already in the list." << endl;
}
}
  
// Displays the average package weight of a city and displays an
// error if there is no such city.
void OutputCity()
{
string cityToOutput; // Stores the city to output.
cin >> cityToOutput;
int position = SearchCity( cityToOutput ); // Sets the position of
if( position == NOT_FOUND ) // the city to output.
cout << "Cannot output. " << cityToOutput
<< " is not in the list." << endl;
else
cout << "Average weight of all packages to " << cityToOutput
<< ": " << list[position].AverageWeight() << endl;
}

// Updates a destinations package count and the total package weight.
// params: in, in
void UpdateCity()
{
string cityToUpdate; // Stores the city to be updated.
cin >> cityToUpdate;
int position = SearchCity( cityToUpdate ); // Sets the position of
// the city to be
// updated.
int packageCount; // Stores the number of packages.
float totalWeight; // Stores the total weight.

if( position > NOT_FOUND )
{
cin >> packageCount >> totalWeight;
list[position].RecordShipment( packageCount, totalWeight );
cout << "Destination " << cityToUpdate << " updated with "
<< packageCount << " packages weighing " << totalWeight
<< " lbs." << endl;
}
else
cout << "Cannot update. " << cityToUpdate
<< " is not in the list." << endl;
}

// Clears the number of and weight of the packagesg going to a
// destination, but leaves the city name as is. Outputs error if
// there is no destination specified.
void ClearCity()
{
string cityToClear; // Stores the city to be cleared.
cin >> cityToClear;

int position = SearchCity( cityToClear );
if( position == NOT_FOUND )
cout << "Cannot clear. " << cityToClear
<< " is not in the list." << endl;
else
list[position].ClearShipment();
}
// Terminates the program.
void Quit()
{
cout << "Normal termination.";
}
};

int main()
{
DestinationList delivery; // To make a Destination list object.
string command; // Stores which command is being called.
cout << fixed << showpoint << setprecision(2);

cin >> command;

while( command != "Quit" )
{
if( command == "Add" )
delivery.AddCity();
else if( command == "Update" )
delivery.UpdateCity();
else if( command == "Output" )
delivery.OutputCity();
else if( command == "Clear" )
delivery.ClearCity();

cin >> command;
}

delivery.Quit();

return 0;
}

Please do up vote.

Thanks.

Do not forget to up vote.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT