Package-delivery services, such as FedEx®, DHL® and UPS®, offer a number of different shipping options, each with specific costs associated. Create an inheritance hierarchy to represent various types of packages. Use Package as the base class of the hierarchy, then include classes TwoDayPackage and OvernightPackage that derive from Package. Base class Package should include the name, address, city, state and zip code for the package’s sender and recipient, and instance variables that store the weight (in ounces) and cost per ounce to ship the package. Package’s constructor should initialize these private instance variables. Ensure that the weight and cost per ounce contain positive values. Package should provide a public method CalculateCost that returns a decimal indicating the cost associated with shipping the package. Package’s CalculateCost method should determine the cost by multiplying the weight by the cost per ounce. Derived class TwoDayPackage should inherit the functionality of base class Package, but also include an instance variable that represents a flat fee the shipping company charges for two-day delivery service. TwoDayPackage’s constructor should receive a value to initialize this instance variable. TwoDayPackage should redefine method CalculateCost so that it computes the shipping cost by adding the flat fee to the weight-based cost calculated by base class Package’s CalculateCost method. Class OvernightPackage should inherit directly from class Package and contain an instance variable representing an additional fee per ounce charged for overnight delivery service. OvernightPackage should redefine method CalculateCost so that it adds the additional fee per ounce to the standard cost per ounce before calculating the shipping cost. Write a test application that creates objects of each type of Package and tests method CalculateCost. Use the Package inheritance hierarchy created above to create an application that displays the address information and calculates the shipping costs for several Packages. The application should contain an array of Package objects of classes TwoDayPackage and OvernightPackage. Loop through the array to process the Packages polymorphically. For each Package, use properties to obtain the address information of the sender and the recipient, then print the two addresses as they would appear on mailing labels. Also, call each Package’s CalculateCost method and print the result. Keep track of the total shipping cost for all Packages in the array, and display this total when the loop terminates.
The Answer to the Question
is below this banner.
Can't find a solution anywhere?
NEED A FAST ANSWER TO ANY QUESTION OR ASSIGNMENT?
Get the Answers Now!You will get a detailed answer to your question or assignment in the shortest time possible.
Here's the Solution to this Question
Solution in C++ Programming Language
#include <iostream>
#include <string>
using namespace std;
class Package // Base Class
{
private: // data members
string name, city, state, sender, recipient;
int zip;
string address;
float weight; // in ounce
double cost; // per ounce
public: //member functions
void setName(string name);
void setCity(string city);
void setState(string state);
void setZip(int zip);
void setAddress(string address);
void setSender(string sender);
void setRecipient(string recipient);
string getName()
{
return name;
}
string getCity()
{
return city;
}
string getState()
{
return state;
}
int getZip()
{
return zip;
}
string getAddress()
{
return address;
}
string getSender()
{
return sender;
}
string getRecipient()
{
return recipient;
}
double calculateCost(float weight,double costPerOunce) //function that calculate the cost z
{
double z; //total cost = weight*cost per ounce
z = weight * costPerOunce; //the cost z
cout<< "The Base Cost = " <<z << endl<<endl;
return z;
}
}; // end class Package
void Package::setName(string n)
{
name = n;
}
void Package::setCity(string c)
{
city = c;
}
void Package::setState(string s)
{
state = s;
}
void Package::setZip (int zp)
{
zip = zp;
}
void Package::setAddress(string adr)
{
address = adr;
}
void Package::setSender(string sen)
{
sender = sen;
}
void Package::setRecipient(string rec)
{
recipient = rec;
}
class TwoDayPackage: public Package // derived class
{
public:
double calcShippingCost(float weight, double costPerOunce, double flatFee)
/* function that calculate shipping cost by adding the flat fee to the weight-based cost
calculated by base class Package’s calculateCost function*/
{
double z; //shippingcost of Two day Package class
z= calculateCost(weight,costPerOunce) + flatFee;
cout<< "The TwoDayPackage Cost = " <<z << endl;
return z;
}
private:
double flatFee;
}; // end TwoDayPackage
class OvernightPackage: public Package //derived class that adds the additional fee per ounce
{
public:
double calcCostOvernight(float weight,double costPerOunce,double additionalCost )
{
double z; //shippingcost of overnight class
z = calculateCost(weight, costPerOunce)+(additionalCost * weight);
cout<< "The OvernightPackage Cost = " <<z << endl;
return z;
}
private:
double overnightCost; //per ounce
}; // end class OvernightPackage
int main()
{
int i; //i represent the user`s choice number
string customerName,customerAddress,city,state,senderAddress,recipientAddress;
float packageWeight;
string customerCity;
double costPerOunce;
double flatFee;
double additionalCost;
string customerState;
int customerZipcode;
Package base; //the object base of the package class
TwoDayPackage twoday; //the object twoday of the first inhereted calss
OvernightPackage overnight; //the object overnight of the second inhereted calss
cout<<" *****Welcome To The American Package Delievery Services*****"<<endl<<endl;
cout<<"Please Fill In The Requested Data Follow: "<<endl<<"-----------------------------------------"<<endl<<endl;;
cout<<"Enter Customer Name "<<endl<<endl;
cin>>customerName;
cout<<endl;
base.setName(customerName); //call function setName
cout<<"Enter Customer Address"<<endl<<endl;
cin>>customerAddress;
cout<<endl;
base.setAddress(customerAddress);
cout<<"Enter Customer City"<<endl<<endl;
cin>>customerCity;
cout<<endl;
base.setCity(customerCity);
cout<<"Enter Customer State"<<endl<<endl;
cin>>customerState;
cout<<endl;
base.setState(customerState);
cout<<"Enter Customer ZIP code"<<endl<<endl;
cin>>customerZipcode;
cout<<endl;
base.setZip(customerZipcode);
cout<<"Enter Weight"<<endl;
cin>>packageWeight;
cout<<endl;
cout<<"Enter Cost Per Ounce"<<endl;
cin>>costPerOunce;
cout<<endl;
cout<<"Please Enter Your Choice From The Menu Below:"<<endl<<endl;
cout<<" 1- Calculate Base Cost "<<endl<<endl;
cout<<" 2- Calculate Two Day Cost "<<endl<<endl;
cout<<" 3- Calculate Over Night Cost"<<endl<<endl;
cin>>i;
cout<<endl; //i represent customer choice
switch (i)
{
case 1 :
base.calculateCost(packageWeight,costPerOunce);
break;
case 2 :
cout<<"Enter Flat Cost"<<endl<<endl; //additonal(to weight and cost) needed information
cin>>flatFee;
twoday.calcShippingCost(packageWeight,costPerOunce,flatFee);
break;
case 3 :
cout<<"Enter The Additional Cost"<<endl<<endl;
cin>>additionalCost;
overnight.calcCostOvernight(packageWeight,costPerOunce,additionalCost);
break;
default:
cout<<"INVALID CHOICE....Please Enter ur Choice Number From 1-->3 "<<endl;
} // end switch
cout<<"Enter sender address "<<endl<<endl;
cin>>senderAddress;
cout<<endl;
base.setSender( senderAddress);
cout<<"Enter ricipent address"<<endl<<endl;
cin>>recipientAddress;
cout<<endl;
base.setRecipient(recipientAddress);
cout<<"address from:"<< senderAddress<<endl;
cout<<"To:"<<recipientAddress<<endl;
system ("pause");
return 0;
} // end main
Solution in C# Programming Language
using System;
public class Package
{
string senderName;
string senderAddress;
string senderCity;
string senderState;
string senderZip;
string recipientName;
string recipientAddress;
string recipientCity;
string recipientState;
string recipientZip;
decimal weight;
decimal costPerOunce;
public string SenderName
{
get{ return senderName; }
set{ senderName = value; }
}
public string SenderAddress
{
get{ return senderAddress; }
set{ senderAddress = value; }
}
public string SenderCity
{
get{ return senderCity; }
set{ senderCity = value; }
}
public string SenderState
{
get{ return senderState; }
set{ senderState = value; }
}
public string SenderZip
{
get{ return senderZip; }
set{ senderZip = value; }
}
public string RecipientName
{
get{ return recipientName; }
set{ recipientName = value; }
}
public string RecipientAddress
{
get{ return recipientAddress; }
set{ recipientAddress = value; }
}
public string RecipientCity
{
get{ return recipientCity; }
set{ recipientCity = value; }
}
public string RecipientState
{
get{ return recipientState; }
set{ recipientState = value; }
}
public string RecipientZip
{
get{ return recipientZip; }
set{ recipientZip = value; }
}
public decimal Weight
{
get{ return weight; }
set
{
if (value > 0)
weight = value;
else
Console.WriteLine("Weight can't be less than zero");
}
}
public decimal CostPerOunce
{
get{ return costPerOunce; }
set
{
if (value > 0)
costPerOunce = value;
else
Console.WriteLine("Cost per ounce can't be less than zero");
}
}
public Package
(
string senderName,
string senderAddress,
string senderCity,
string senderState,
string senderZip,
string recipientName,
string recipientAddress,
string recipientCity,
string recipientState,
string recipientZip,
decimal weight,
decimal costPerOunce
)
{
SenderName = senderName;
SenderAddress = senderAddress;
SenderCity = senderCity;
SenderState = senderState;
SenderZip = senderZip;
RecipientName = recipientName;
RecipientAddress = recipientAddress;
RecipientCity = recipientCity;
RecipientState = recipientState;
RecipientZip = recipientZip;
Weight = weight;
CostPerOunce = costPerOunce;
}
public virtual decimal CalculateCost()
{
return weight * costPerOunce;
}
}
public class TwoDayPackage : Package
{
decimal twoDayDeliveryFee;
public decimal TwoDayDeliveryFee
{
get { return twoDayDeliveryFee; }
set { twoDayDeliveryFee = value; }
}
public TwoDayPackage
(
string senderName,
string senderAddress,
string senderCity,
string senderState,
string senderZip,
string recipientName,
string recipientAddress,
string recipientCity,
string recipientState,
string recipientZip,
decimal weight,
decimal costPerOunce,
decimal twoDayDeliveryFee
)
: base
(
senderName,
senderAddress,
senderCity,
senderState,
senderZip,
recipientName,
recipientAddress,
recipientCity,
recipientState,
recipientZip,
weight,
costPerOunce
)
{
TwoDayDeliveryFee = twoDayDeliveryFee;
}
public override decimal CalculateCost()
{
return base.CalculateCost() + TwoDayDeliveryFee;
}
}
public class OvernightPackage : Package
{
decimal overnightDeliveryFeePerOunce;
public decimal OvernightDeliveryFeePerOunce
{
get { return overnightDeliveryFeePerOunce; }
set { overnightDeliveryFeePerOunce = value; }
}
public OvernightPackage
(
string senderName,
string senderAddress,
string senderCity,
string senderState,
string senderZip,
string recipientName,
string recipientAddress,
string recipientCity,
string recipientState,
string recipientZip,
decimal weight,
decimal costPerOunce,
decimal overnightDeliveryFeePerOunce
)
: base
(
senderName,
senderAddress,
senderCity,
senderState,
senderZip,
recipientName,
recipientAddress,
recipientCity,
recipientState,
recipientZip,
weight,
costPerOunce
)
{
OvernightDeliveryFeePerOunce = overnightDeliveryFeePerOunce;
}
public override decimal CalculateCost()
{
return (CostPerOunce + OvernightDeliveryFeePerOunce) * Weight;
}
}
class Test
{
static void Main()
{
Package p = new Package
(
"John Smith",
"3256 Epiphenomenal Avenue",
"Minneapolis",
"Minnesota",
"MN 55416",
"Fred Smith",
"220 Second Avenue",
"St. Petersburg",
"Florida",
"FL 33705",
160M,
0.1M
);
Console.WriteLine(p.CalculateCost());
TwoDayPackage tdp = new TwoDayPackage
(
"John Smith",
"3256 Epiphenomenal Avenue",
"Minneapolis",
"Minnesota",
"MN 55416",
"Fred Smith",
"220 Second Avenue",
"St. Petersburg",
"Florida",
"FL 33705",
160M,
0.1M,
1.5M
);
Console.WriteLine(tdp.CalculateCost());
OvernightPackage op = new OvernightPackage
(
"John Smith",
"3256 Epiphenomenal Avenue",
"Minneapolis",
"Minnesota",
"MN 55416",
"Fred Smith",
"220 Second Avenue",
"St. Petersburg",
"Florida",
"FL 33705",
160M,
0.1M,
0.02M
);
Console.WriteLine(op.CalculateCost());
Console.ReadKey();
}
}