Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum Shipping: Error {
- case outOfRange(String)
- }
- protocol ShippingStrategy {
- func calculateShippingCost(
- packageWeightInKilograms: Float,
- packageDimensionsInInches: CGPoint,
- destination: String) throws -> Int
- }
- class WorldWideShippingStrategy: ShippingStrategy {
- func calculateShippingCost(
- packageWeightInKilograms: Float,
- packageDimensionsInInches: CGPoint,
- destination: String) throws -> Int {
- guard packageWeightInKilograms > 0.0 else {
- throw Shipping.outOfRange("package weight must be positive and non-zero")
- }
- guard packageDimensionsInInches.x > 0.0 || packageDimensionsInInches.y > 0.0 else {
- throw Shipping.outOfRange("Package dimensions must be positive and nonzero")
- }
- guard destination != null else {
- throw Shipping.outOfRange("Destination must be provided")
- }
- // calculate shipping cost
- let shippingCost = 50
- guard shippingCost > 0 else {
- throw Shipping.outOfRange("The calculated shipping cost is out of range")
- }
- return shippingCost
- }
- }
Add Comment
Please, Sign In to add comment