首页 > 解决方案 > Caculation of net profit

问题描述

Name:

Instructions:

Read the following requirements carefully Use these files to complete the assignment Attach any/all files required to complete this quiz to Quiz 3 located in the Course Content section of Blackboard

Comments/Constraints There is no one solution; however, your code should satisfy the requirements You will be graded on your solution as well as your code structure, syntax and annotations You may use any resources available to you (Google, book, homework, etc.) This is an individual assignment

Quiz Requirements/Scenario

Simple profit from sale of primary home function

Assumptions (for IRS purposes – this really won’t factor into your code, just an FYI) This is your primary residence You have owned your home for more than 5 years If the person is married, assume they have been married for 3 years or more

Rules Determining net and gross profits After selling a piece of property, the amount you gross is determined by the sales price minus the cost basis minus the liabilities (commissions, expenses, capital improvements, etc.). Taxes To determine your net, you need to factor in the capital gains taxes (if applicable) When you sell your primary residence, you can make up to $250,000 in profit if you're a single owner, twice that if you're married, and not owe any capital gains taxes. If you make more than the allowed amount, you pay 15% on the difference between the gross and the allowed amounts. Quiz 3 base requirements

Using the above, create a function labeled “calculateNetProfit” with the following input parameters: Sale price, Total Liabilities, Cost Basis, Owner Status (two values allowed: single or married) Choose either single or married and ensure that the conditional statements are working Finally, call that function using whichever parameters you’d like

Above I have instructions on how to calculate the net profit and below is my php code

function calculateNetProfit($salePrice,$costBasis,$liabilities) {
    $ownerStatus = "single";
    $grossAmount = ($salePrice- $costBasis) - $liabilities;

    if ($ownerStatus >= 250000) {
        $netProfit = $grossAmount *.15;
        return $netProfit;
    } else {
        echo $grossAmount;
    }

}
echo calculateNetProfit(320000,100000,2200);

I was wondering if I am doing this correctly please let me know thanks

标签: php

解决方案


我会将这两个问题分开(计算金额并确定税收状况)。此外,您计算的是乘以 0.15 所欠的税额,因此为了便于阅读,我重命名了一个变量并将其从grossAmount

function isOverTaxThreshold($ownerStatus, $grossAmount){
    switch($ownerStatus){
      case "single":
          return $grossAmount >= 250000;
      case "married":
          return $grossAmount >= 500000;
      default:
          throw new Exception("ownerStatus must be one of [single, married]");
    }
}

function calculateNetProfit($salePrice,$costBasis,$liabilities,$ownerStatus) {
    $grossAmount = ($salePrice- $costBasis) - $liabilities;

    if (isOverTaxThreshold($ownerStatus, $grossAmount)) {
        $taxAmount = $grossAmount * .15;
        return $grossAmount - $taxAmount;
    } else {
        return $grossAmount;
    }
}


var_dump(calculateNetProfit(500000,100000,2200, "single"));
var_dump(calculateNetProfit(500000,100000,2200, "married"));

推荐阅读