Exercise: E-Commerce Shipping Calculator
Problem statement
Retail platforms offer multiple shipping tiers. A standard postal shipment calculates its cost based on a flat rate plus a weight multiplier. An express shipment uses the exact same calculation for its base cost but adds a mandatory premium fee on top. The objective is to model this relationship without duplicating the core calculation logic.
Task requirements
Create a
PostalShipmentbase class with aWeightInPoundsproperty and aCalculateCost()method to calculate the shipping cost.The standard cost calculation is $5.00 flat plus $0.50 per pound.
Create an
ExpressShipmentderived class.The express cost calculation must execute the standard calculation and then add a $10.00 premium.
Constraints
Use inheritance to make
ExpressShipmenta derived class ofPostalShipment.Mark the calculation method in the base class with the
virtualkeyword.Use the
overridekeyword in the derived class to modify the calculation.Use the
basekeyword inside the derived class to execute the parent's math logic before adding the premium.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Use
public virtual decimal CalculateCost()in the parent class to allow child classes to change its behavior.In C#, you can call a method from the parent class using
base.MethodName().Ensure you cast the weight math to a
decimalif you multiply adoubleby a literal number, or use themsuffix for monetary values (e.g.,5.00m + (decimal)(WeightInPounds * 0.5)).
Exercise: E-Commerce Shipping Calculator
Problem statement
Retail platforms offer multiple shipping tiers. A standard postal shipment calculates its cost based on a flat rate plus a weight multiplier. An express shipment uses the exact same calculation for its base cost but adds a mandatory premium fee on top. The objective is to model this relationship without duplicating the core calculation logic.
Task requirements
Create a
PostalShipmentbase class with aWeightInPoundsproperty and aCalculateCost()method to calculate the shipping cost.The standard cost calculation is $5.00 flat plus $0.50 per pound.
Create an
ExpressShipmentderived class.The express cost calculation must execute the standard calculation and then add a $10.00 premium.
Constraints
Use inheritance to make
ExpressShipmenta derived class ofPostalShipment.Mark the calculation method in the base class with the
virtualkeyword.Use the
overridekeyword in the derived class to modify the calculation.Use the
basekeyword inside the derived class to execute the parent's math logic before adding the premium.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Use
public virtual decimal CalculateCost()in the parent class to allow child classes to change its behavior.In C#, you can call a method from the parent class using
base.MethodName().Ensure you cast the weight math to a
decimalif you multiply adoubleby a literal number, or use themsuffix for monetary values (e.g.,5.00m + (decimal)(WeightInPounds * 0.5)).