using System; class SalaryCalculator { // Constants for the Canada Pension Plan calculations const float cppMaximum = 1831.5F; const float cppRate = 0.0495F; const float cppExemption = 3500.0F; // Constants for the Employment Insurance calculations const float eiMaximum = 772.2F; const float eiRate = 0.0198F; // Constants for the provincial tax calculations const float pTaxMaximum1 = 33375.0F; const float pTaxMaximum2 = 66752.0F; const float pTaxRate1 = 0.0605F; const float pTaxRate2 = 0.0915F; const float pTaxRate3 = 0.1116F; const float pTaxExemption = 8044.0F; // Constants for the federal tax calculations const float fTaxMaximum1 = 35000.0F; const float fTaxMaximum2 = 70000.0F; const float fTaxMaximum3 = 113804.0F; const float fTaxRate1 = 0.16F; const float fTaxRate2 = 0.22F; const float fTaxRate3 = 0.26F; const float fTaxRate4 = 0.29F; const float fTaxExemption = 8012.0F; // Declare a member field for the gross salary to be updated // by CalculateNet( ) float grossSalary; // Declare a member field for the CPP deduction to be updated // by CalculateNet( ) float cpp; // Declare a member field for the EI deduction to be updated // by CalculateNet( ) float ei; // Declare a member field for the provincial tax to be updated // by CalculateNet( ) float pTax; // Declare a member field for the federal tax to be updated // by CalculateNet( ) float fTax; // Declare a member field for the net salary to be updated // by CalculateNet( ) float netSalary; // CalculateCPP // // This method calculates Canada Pension Plan deductions based on // a gross salary. It returns the result of the calculation as a // single-precision floating point value. float CalculateCPP( float gross ) { // Declare a local variable to store the result float result = 0.0F; // Calculate the salary less the exemption and // ensure that it is greater than 0.0F float salary = Math.Max(gross - cppExemption, 0.0F); // Calculate the CPP deduction and ensure that it does not // exceed the cppMaximum result = Math.Min( salary * cppRate, cppMaximum ); return( result ); } // CalculateEI // // This method calculates Employment Insurance deductions based on // a gross salary. It returns the result of the calculation as a // single-precision floating point value. float CalculateEI( float gross ) { // Declare a local variable to store the result float result = 0.0F; // Ensure that the salary is greater than 0.0F float salary = Math.Max( gross, 0.0F ); // Calculate the EI deduction and ensure that it does not // exceed the eiMaximum result = Math.Min( salary * eiRate, eiMaximum ); return( result ); } // CalculatePTax // // This method calculates provincial taxes based on a gross salary. // It returns the result of the calculation as a single-precision // floating point value. float CalculatePTax( float gross ) { // Declare a local variable to store the result float result = 0.0F; // Calculate the salary less the exemption and // ensure that it is greater than 0.0F float salary = Math.Max( gross - pTaxExemption, 0.0F ); // These selection statements calculate provincial taxes based // on the actual rules. It is assumed that the brackets do // not have gaps between salary brackets. if( salary <= pTaxMaximum1 ) { // All salary is taxed at pTaxRate1 result = salary * pTaxRate1; } else if( salary <= pTaxMaximum2 ) { // Portions of salary are taxed at 2 different rates result = pTaxMaximum1 * pTaxRate1 + (salary - pTaxMaximum1) * pTaxRate2; } else { // Portions of salary are taxed at 3 different rates result = pTaxMaximum1 * pTaxRate1 + (pTaxMaximum2 - pTaxMaximum1) * pTaxRate2 + (salary - pTaxMaximum2) * pTaxRate3; } return( result ); } // CalculateFTax // // This method calculates federal taxes based on a gross salary. // It returns the result of the calculation as a single-precision // floating point value. // float CalculateFTax( float gross ) { // Declare a local variable to store the result float result = 0.0F; // Calculate the salary less the exemption and // ensure that it is greater than 0.0F float salary = Math.Max(gross - fTaxExemption, 0.0F); // These selection statements calculate federal taxes based // on the actual rules. It is assumed that the brackets do // not have gaps between salary brackets. if( salary <= fTaxMaximum1 ) { // All salary is taxed at fTaxRate1 result = salary * fTaxRate1; } else if( salary <= fTaxMaximum2 ) { // Portions of salary are taxed at 2 different rates result = fTaxMaximum1 * fTaxRate1 + (salary - fTaxMaximum1) * fTaxRate2; } else if( salary <= fTaxMaximum3 ) { // Portions of salary are taxed at 3 different rates result = fTaxMaximum1 * fTaxRate1 + (fTaxMaximum2 - fTaxMaximum1) * fTaxRate2 + (salary - fTaxMaximum2) * fTaxRate3; } else { // Portions of salary are taxed at 4 different rates result = fTaxMaximum1 * fTaxRate1 + (fTaxMaximum2 - fTaxMaximum1) * fTaxRate2 + (fTaxMaximum3 - fTaxMaximum2) * fTaxRate3 + (salary - fTaxMaximum3) * fTaxRate4; } return( result ); } // CalculateNet // // This method calculates a net salary by prompting a user for // a gross salary, calculating all deductions and taxes, and // then calculating the net result. The result of the calculation // is displayed but not returned. // void CalculateNet( ) { // Prompt the user for the grossSalary and parse it // accordingly. Console.WriteLine( "\nSalary Calculator\n" ); Console.Write( "Enter the gross salary of the employee: " ); grossSalary = float.Parse( Console.ReadLine( ) ); Console.Write( "\n" ); // NOTE: The assignment to the member fields is necessary // since the methods have not been designed to modify // the member fields directly. This design allows the // methods to be easily changed to static functions // for the purpose of calculating deductions without // the need for an instance of a SalaryCalculator. cpp = CalculateCPP( grossSalary ); ei = CalculateEI( grossSalary ); pTax = CalculatePTax( grossSalary ); fTax = CalculateFTax( grossSalary ); // Calculate the netSalary by subtracting the deductions // and taxes netSalary = grossSalary - cpp - ei - pTax - fTax; // Output the results in the format specified Console.WriteLine( "Gross Salary {0,10:C}", grossSalary ); Console.WriteLine( "Canada Pension Plan - {0,10:C}", cpp ); Console.WriteLine( "Employment Insurance - {0,10:C}", ei ); Console.WriteLine( "Provincial Tax - {0,10:C}", pTax ); Console.WriteLine( "Federal Tax - {0,10:C}", fTax ); Console.WriteLine( " -------------" ); Console.WriteLine( "Net Salary {0,10:C}", netSalary ); } // Main( ) provides the entry point for the program. It does not // accept any command-line arguments nor does it return a value to // the calling program. static void Main( ) { new SalaryCalculator().CalculateNet(); } }