【问题标题】:How to get a decreasing rental cost based on the number of days如何根据天数降低租金
【发布时间】:2019-01-04 16:46:10
【问题描述】:

有没有一种快速的方法可以根据天数降低租金?

例如:

If I rent a car for 1day,  the cost is 100$
If I rent a car for 2days, the cost is 100$ + 70$ = 170$
If I rent a car for 3days, the cost is 100$ + 70$ + 50$ = 220$
If I rent a car for 4days, the cost is 100$ + 70$ + 50$ + 50$ = 270$
If I rent a car for 5days, the cost is 100$ + 70$ + 50$ + 50$ + 50$ = 320$

所以我需要一种快速的方法来根据天数计算总费用。 例如:

function getcost(days){
   ...
   return $cost;
}

echo getcost(1); // it show 100$
echo getcost(3); // it show 220$
// and so on...

【问题讨论】:

  • switch 语句?
  • 或者,编号数组 ($array[1 => 100, 2 => 70, 3 => 50]) - 所以function calcPrice($days) {#add up accordingly based on number of days (if above 3 keep @ 50)}
  • 那么,这是你的作业吗?

标签: php calculation


【解决方案1】:

假设从第三天开始,所有连续天都花费 50 美元:

function getcost(int $days) {
  return ($days > 1) ? (($days - 2) * 50 + 170) : (($days == 1) ? 100 : 0);
}

【讨论】:

    【解决方案2】:
    function getcost(days){
        $cost=0;
        for($idx=1;$idx<=$days;$idx++)
         {
           switch($idx)
           {
             case 1:
                     $cost+=100;
                     break;
             case 2:
                     $cost+=70;
                     break;
             default:
                     $cost+=50;
                     break;
    
           }
         }
      return $cost;
     }
    

    【讨论】:

      【解决方案3】:
      $price = $days * 50 + ($days > 1 ? 70 : 20);
      

      如果你愿意,你可以把它放在一个函数中。

      【讨论】:

        【解决方案4】:

        你可以这样试试-

        <?php
        function getcost($days){
           $array=['base' => 100, 'for_2nd' => 70, 'after_2nd' => 50];       
           if($days==1){
               $cost = $array['base'] * $days;
           }else if($days>=2){
               $cost = $array['base']  + ($days == 2 ? $array['for_2nd'] : $array['for_2nd'] + ($days - 2) * $array['after_2nd']); 
           }
           return $cost.'$';
        }
        
        echo getcost(1); // it show 100$
        echo getcost(2); // it show 170$
        echo getcost(3); // it show 220$
        echo getcost(4); // it show 270$
        echo getcost(5); // it show 320$
        ?>
        

        演示: https://3v4l.org/h9tF5

        【讨论】:

          【解决方案5】:

          如果您的日费率基于天数,那么为了让您拥有动态费率(可能是不同类型的汽车等),那么最好将某种形式的数组传递给函数。然后该函数使用它并添加天数,直到费率用完,并根据最后的费率添加任何剩余天数...

          // Cost for each day
          $costs = [100, 70, 50];
          
          function getcost( $costs, $days){
              $totalCost = 0;
              foreach ( $costs as $dayCost )  {
                  // Add each cost
                  $totalCost += $dayCost;
                  // Decrement number of days left and exit if reached 0
                  if ( --$days == 0 ) {
                      break;
                  }
              }
              // If remaining days - add last day cost * number of days
              if ($days > 0 ) {
                  $totalCost += ($dayCost*$days);
              }
              return $totalCost;
          }
          
          echo getcost($costs, 1); // it show 100$
          echo getcost($costs, 3); // it show 220$
          echo getcost($costs, 5); // it show 320$
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-11-07
            • 1970-01-01
            • 1970-01-01
            • 2021-09-02
            • 1970-01-01
            • 2020-12-02
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多