Exercise: E-Commerce Shipping Calculator

Calculate inherited logistics costs using virtual methods and base class invocation.

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 PostalShipment base class with a WeightInPounds property and a CalculateCost() method to calculate the shipping cost.

  • The standard cost calculation is $5.00 flat plus $0.50 per pound.

  • Create an ExpressShipment derived class.

  • The express cost calculation must execute the standard calculation and then add a $10.00 premium.

Constraints

  • Use inheritance to make ExpressShipment a derived class of PostalShipment.

  • Mark the calculation method in the base class with the virtual keyword.

  • Use the override keyword in the derived class to modify the calculation.

  • Use the base keyword 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 decimal if you multiply a double by a literal number, or use the m suffix for monetary values (e.g., 5.00m + (decimal)(WeightInPounds * 0.5)).

Exercise: E-Commerce Shipping Calculator

Calculate inherited logistics costs using virtual methods and base class invocation.

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 PostalShipment base class with a WeightInPounds property and a CalculateCost() method to calculate the shipping cost.

  • The standard cost calculation is $5.00 flat plus $0.50 per pound.

  • Create an ExpressShipment derived class.

  • The express cost calculation must execute the standard calculation and then add a $10.00 premium.

Constraints

  • Use inheritance to make ExpressShipment a derived class of PostalShipment.

  • Mark the calculation method in the base class with the virtual keyword.

  • Use the override keyword in the derived class to modify the calculation.

  • Use the base keyword 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 decimal if you multiply a double by a literal number, or use the m suffix for monetary values (e.g., 5.00m + (decimal)(WeightInPounds * 0.5)).

C# 14.0
namespace Retail;
// TODO: Define a public class named PostalShipment
// TODO: Add a public double property WeightInPounds
// TODO: Create a public virtual method CalculateCost() returning a decimal
// TODO: Define a public class named ExpressShipment that inherits from PostalShipment
// TODO: Override CalculateCost() to call the base method and add 10.00m