Permalink
Browse files

if else 計算運費

  • Loading branch information...
1 parent 7aa2262 commit 6a2c6c45e9cfd590633c9a06a9db06a979ba1141 Sam.IO.Hsiao committed Nov 27, 2016
Showing with 45 additions and 0 deletions.
  1. +45 −0 app/Services/ShippingService.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace App\Services;
+
+class ShippingService
+{
+ /**
+ * 計算運費
+ * @param array $weightArray
+ * @param string $companyName
+ * @return int
+ */
+ public function calculateFee(array $weightArray, string $companyName): int
+ {
+ $amount = 0;
+
+ if ($companyName == 'BlackCat') {
+ $weights = collect($weightArray);
+
+ foreach ($weights as $weight) {
+ $amount = $amount + (100 + $weight * 10);
+ }
+ } elseif ($companyName == 'Hsinchu') {
+ $weights = collect($weightArray);
+
+ foreach ($weights as $weight) {
+ $amount = $amount + (80 + $weight * 15);
+ }
+ } else if ($companyName == 'PostOffice') {
+ $weights = collect($weightArray);
+
+ foreach ($weights as $weight) {
+ $amount = $amount + (60 + $weight * 20);
+ }
+ } else {
+ $weights = collect($weightArray);
+
+ foreach ($weights as $weight) {
+ $amount = $amount + (100 + $weight * 10);
+ }
+ }
+
+ return $amount;
+ }
+}

0 comments on commit 6a2c6c4

Please sign in to comment.