Skip to content

Instantly share code, notes, and snippets.

@adriatikgashi
Created February 27, 2022 18:01
Show Gist options
  • Save adriatikgashi/58cfb9d9dc8f990e8f8f8a95c9e7ac8c to your computer and use it in GitHub Desktop.
Save adriatikgashi/58cfb9d9dc8f990e8f8f8a95c9e7ac8c to your computer and use it in GitHub Desktop.
protocol Shipping {
func getCost(forOrder order: Order) -> Double
}
class GroundShipping: Shipping {
func getCost(forOrder order: Order) -> Double {
if order.getTotal() > 100 {
return 0
}
// $1.5 per kilogram, but $10 minimum
return max(10, order.getTotalWeight() * 1.5)
}
}
class AirShipping: Shipping {
func getCost(forOrder order: Order) -> Double {
return max(20, getTotalWeight() * 3)
}
}
struct Order {
let lineItems: [String]
let shipping: Shipping
func getShippingCost() -> Double {
return shipping.getCost(forOrder: self)
}
func getTotal() -> Double {
// calculate the total
return 0.0
}
func getTotalWeight() -> Double {
// calculate the total weight
return 0.0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment