User Story
As a developer,
I want a shipping cost calculator to determine the shipping cost based on the selected shipping method, So that I can make an informed decision during the checkout process.
When selectedShippingMethod is Standard, shippingCost is 5.00 percent.
When selectedShippingMethod is Express, shippingCost is 10.00 percent.
When selectedShippingMethod is Next Day, shippingCost is 20.00 percent.
For eveything else, shippingCost is 7.50 percent.
Codes
Solution 1:
public class ShippingCostCalculator {
public static Decimal calculateShippingCost(String selectedShippingMethod) {
Decimal shippingCost;
switch on selectedShippingMethod {
when 'Standard' {
shippingCost = 5.00;
}
when 'Express' {
shippingCost = 10.00;
}
when 'Next Day' {
shippingCost = 20.00;
}
when else {
shippingCost = 7.50;
}
}
return shippingCost;
}
}
Solution 2:
public class ShippingCostCalculator {
public static Decimal calculateShippingCost(String selectedShippingMethod) {
Decimal shippingCost;
if (selectedShippingMethod.equals("Standard")) {
shippingCost = 5.00;
} else if (selectedShippingMethod.equals("Express")) {
shippingCost = 10.00;
} else if (selectedShippingMethod.equals("Next Day")) {
shippingCost = 20.00;
} else {
shippingCost = 7.50;
}
return shippingCost;
}
}
Solution 3:
public class ShippingCostCalculator {
public static Decimal calculateShippingCost(String selectedShippingMethod) {
Decimal shippingCost;
if (selectedShippingMethod == "Standard") {
shippingCost = 5.00;
} else if (selectedShippingMethod == "Express") {
shippingCost = 10.00;
} else if (selectedShippingMethod == "Next Day") {
shippingCost = 20.00;
} else {
shippingCost = 7.50;
}
return shippingCost;
}
}