Guest User

Untitled

a guest
Jan 17th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. enum Shipping: Error {
  2. case outOfRange(String)
  3. }
  4.  
  5. protocol ShippingStrategy {
  6. func calculateShippingCost(
  7. packageWeightInKilograms: Float,
  8. packageDimensionsInInches: CGPoint,
  9. destination: String) throws -> Int
  10. }
  11.  
  12. class WorldWideShippingStrategy: ShippingStrategy {
  13.  
  14. func calculateShippingCost(
  15. packageWeightInKilograms: Float,
  16. packageDimensionsInInches: CGPoint,
  17. destination: String) throws -> Int {
  18. guard packageWeightInKilograms > 0.0 else {
  19. throw Shipping.outOfRange("package weight must be positive and non-zero")
  20. }
  21.  
  22. guard packageDimensionsInInches.x > 0.0 || packageDimensionsInInches.y > 0.0 else {
  23. throw Shipping.outOfRange("Package dimensions must be positive and nonzero")
  24. }
  25.  
  26. guard destination != null else {
  27. throw Shipping.outOfRange("Destination must be provided")
  28. }
  29.  
  30. // calculate shipping cost
  31. let shippingCost = 50
  32.  
  33. guard shippingCost > 0 else {
  34. throw Shipping.outOfRange("The calculated shipping cost is out of range")
  35. }
  36.  
  37. return shippingCost
  38. }
  39.  
  40. }
Add Comment
Please, Sign In to add comment