Created
February 20, 2025 10:29
-
-
Save otienoelvis/7a7923af0a26317402a4d073930fb340 to your computer and use it in GitHub Desktop.
Payroll Calc POC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using static SampleRestsharp.DeepCalc; | |
namespace SampleRestsharp; | |
internal class DeepCalc | |
{ | |
public class PayrollData | |
{ | |
public decimal GrossSalary { get; set; } | |
public decimal NetSalary { get; set; } | |
public decimal Tax { get; set; } | |
public decimal Deductions { get; set; } | |
public decimal Bonuses { get; set; } | |
// Add other properties as needed | |
} | |
// Interface for a payroll calculation step | |
public interface IPayrollCalculationStep | |
{ | |
void Calculate(PayrollData data); | |
} | |
// Base class for all payroll calculation steps | |
public abstract class PayrollCalculationStep : IPayrollCalculationStep | |
{ | |
public abstract void Calculate(PayrollData data); | |
} | |
public class TaxCalculationStep : PayrollCalculationStep | |
{ | |
private readonly decimal _taxRate; | |
public TaxCalculationStep(decimal taxRate) | |
{ | |
_taxRate = taxRate; | |
} | |
public override void Calculate(PayrollData data) | |
{ | |
data.Tax = data.GrossSalary * _taxRate; | |
data.NetSalary -= data.Tax; | |
} | |
} | |
// Step to calculate deductions | |
public class DeductionCalculationStep : PayrollCalculationStep | |
{ | |
private readonly decimal _deductionAmount; | |
public DeductionCalculationStep(decimal deductionAmount) | |
{ | |
_deductionAmount = deductionAmount; | |
} | |
public override void Calculate(PayrollData data) | |
{ | |
data.Deductions = _deductionAmount; | |
data.NetSalary -= data.Deductions; | |
} | |
} | |
// Step to calculate bonuses | |
public class BonusCalculationStep : PayrollCalculationStep | |
{ | |
private readonly decimal _bonusAmount; | |
public BonusCalculationStep(decimal bonusAmount) | |
{ | |
_bonusAmount = bonusAmount; | |
} | |
public override void Calculate(PayrollData data) | |
{ | |
data.Bonuses = _bonusAmount; | |
data.NetSalary += data.Bonuses; | |
} | |
} | |
public class PayrollCalculator | |
{ | |
private readonly List<IPayrollCalculationStep> _calculationSteps; | |
public PayrollCalculator(List<IPayrollCalculationStep> calculationSteps) | |
{ | |
_calculationSteps = calculationSteps; | |
} | |
public PayrollData Calculate(PayrollData data) | |
{ | |
foreach (var step in _calculationSteps) | |
{ | |
step.Calculate(data); | |
} | |
return data; | |
} | |
} | |
public class CalculationStepConfig | |
{ | |
public string Type { get; set; } | |
public Dictionary<string, decimal> Parameters { get; set; } | |
} | |
public class PayrollConfig | |
{ | |
public List<CalculationStepConfig> CalculationSteps { get; set; } | |
} | |
public static class PayrollConfigurationLoader | |
{ | |
public static List<IPayrollCalculationStep> LoadConfiguration(string configFilePath = "") | |
{ | |
//var configJsodn = File.ReadAllText(configFilePath); | |
var configJson = @"{ | |
""CalculationSteps"": [ | |
{ | |
""Type"": ""TaxCalculationStep"", | |
""Parameters"": { ""TaxRate"": 0.2 } | |
}, | |
{ | |
""Type"": ""DeductionCalculationStep"", | |
""Parameters"": { ""DeductionAmount"": 100 } | |
}, | |
{ | |
""Type"": ""BonusCalculationStep"", | |
""Parameters"": { ""BonusAmount"": 50 } | |
} | |
] | |
}"; | |
var config = JsonConvert.DeserializeObject<PayrollConfig>(configJson); | |
var calculationSteps = new List<IPayrollCalculationStep>(); | |
foreach (var stepConfig in config.CalculationSteps) | |
{ | |
IPayrollCalculationStep step = stepConfig.Type switch | |
{ | |
"TaxCalculationStep" => new TaxCalculationStep(stepConfig.Parameters["TaxRate"]), | |
"DeductionCalculationStep" => new DeductionCalculationStep(stepConfig.Parameters["DeductionAmount"]), | |
"BonusCalculationStep" => new BonusCalculationStep(stepConfig.Parameters["BonusAmount"]), | |
_ => throw new ArgumentException($"Unknown calculation step type: {stepConfig.Type}") | |
}; | |
calculationSteps.Add(step); | |
} | |
return calculationSteps; | |
} | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Load configuration | |
var calculationSteps = PayrollConfigurationLoader.LoadConfiguration(); | |
// Create the payroll calculator | |
var payrollCalculator = new PayrollCalculator(calculationSteps); | |
// Example employee data | |
var payrollData = new PayrollData | |
{ | |
GrossSalary = 5000, | |
NetSalary = 5000 // Initially, NetSalary = GrossSalary | |
}; | |
// Calculate payroll | |
payrollCalculator.Calculate(payrollData); | |
// Output results | |
Console.WriteLine($"Gross Salary: {payrollData.GrossSalary}"); | |
Console.WriteLine($"Tax: {payrollData.Tax}"); | |
Console.WriteLine($"Deductions: {payrollData.Deductions}"); | |
Console.WriteLine($"Bonuses: {payrollData.Bonuses}"); | |
Console.WriteLine($"Net Salary: {payrollData.NetSalary}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment