Пример #1
0
        /// <summary>
        /// Combine salary calculations and produce a Payslip for an Employee
        /// </summary>
        /// <param name="employee"></param>
        public string GeneratePayslip(EmployeeDetail employee)
        {
            var employeeName = $"{employee.FirstName} {employee.LastName}";
            var taxBracket   = _taxTable.GetTaxBracket(employee.Salary);
            var incomeTax    = _payCalculator.CalculateIncomeTax(employee.Salary, taxBracket);
            var grossIncome  = _payCalculator.CalculateGrossIncome(employee.Salary);
            var netIncome    = _payCalculator.CalculateNetIncome(grossIncome, incomeTax);
            var super        = _payCalculator.CalculateSuper(grossIncome, employee.SuperRate);
            var payPeriod    = employee.PayPeriod;

            //  Name, payPeriod, grossIncome, incomeTax, netIncome, super
            var payslip = $"{employeeName}, {payPeriod}, {grossIncome}, {incomeTax}, {netIncome}, {super}";

            return(payslip);
        }
Пример #2
0
        /// <summary>
        /// Import records from CSV file
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public List <EmployeeDetail> Import(string filepath)
        {
            var employees = new List <EmployeeDetail>();
            var failures  = new List <string>();

            using (var fs = File.OpenRead(filepath))
                using (var reader = new StreamReader(fs))
                {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
                        if (line == null)
                        {
                            continue;
                        }
                        var values = line.Split(',');

                        try
                        {
                            var employeeDetail = new EmployeeDetail
                            {
                                FirstName = values[0],
                                LastName  = values[1],
                                Salary    = Convert.ToDecimal(values[2]),
                                SuperRate = Convert.ToDecimal(Regex.Replace(values[3], @"[^0-9]+", "")),
                                PayPeriod = values[4],
                            };

                            employees.Add(employeeDetail);
                        }
                        catch (Exception ex)
                        {
                            failures.Add(line + " | ERROR: " + ex.Message);
                        }
                    }
                }

            if (failures.Any())
            {
                File.WriteAllLines("failures.csv", failures);
            }

            return(employees);
        }