In the following code I'm trying to calculate Net Wage of employees and classes Earning
, Deduction
and WageBalance
has a composition relationship with WageInfo
. And there is a class called WageManager
which is having a association with WageInfo
, handling all operations like BalanceWage()
. DataService class encapsulates all db logic. Please review my code and let me know of any flaws.
Is the way I've implemented the relationships correct? Would you have a method to calculate BalanceWage
in a more elegant way?
class WageManager
{
WageInfo _WageInfo;
DataService _DataService;
public WageManager(WageInfo wageInfo, DataService dataService )
{
_WageInfo = wageInfo;
_DataService = dataService;
}
public List<WageBalance> BalanceWages()
{
var earningList = _DataService.GetEarningsList(); //DataService returns a List<Earning> list
var deductionList = _DataService.GetDeductionList(); //DataService returns a List<Deduction>
int i = 0;
foreach (Earning e in earningList)
{
// Sum all earnings...
var totalEarnings = earningList[i].AmountForEPF + earningList[i].OverTimeAmount + earningList[i].IncentiveAllowance + earningList[i].SpecialAllowance + earningList[i].OtherAllowance + earningList[i].ExtraShiftAmount + earningList[i].BroughtForwardAmount;
// Sum all deductions...
var totalDeductions = deductionList[i].AdvanceAmount + deductionList[i].CarriedForwardAmount + deductionList[i].DeathDonationAmount + deductionList[i].EPFAmount + deductionList[i].FineAmount + deductionList[i].InsuranceInstalmentAmount + deductionList[i].LoanInstalmentAmount + deductionList[i].MealsAmount + deductionList[i].OtherDeductionAmount + deductionList[i].UniformInstalmentAmount + deductionList[i].WelfareAmount;
var employeeID = earningList[i].EmployeeID;
var netEarning = totalEarnings - totalDeductions;
WageInfo wageInfo = new WageInfo();
wageInfo.WageBalance.EmployeeID = employeeID;
wageInfo.WageBalance.NetWage = decimal.Truncate(netEarning / 100) * 100;
wageInfo.WageBalance.CarriedForwardAmount = netEarning - wageInfo.WageBalance.NetWage;
_WageInfo.WageBalanceList.Add(wageInfo.WageBalance);
i += 1;
}
_DataService.BalanceWages(_WageInfo.WageBalanceList);
return _WageInfo.WageBalanceList;
}
}
class WageInfo
{
public DateTime WagePeriodStartDate { get; set; }
public DateTime WagePeriodEndDate { get; set; }
public string Reference { get; set; }
public Deduction Deduction = new Deduction();
public Earning Earning = new Earning();
public WageBalance WageBalance = new WageBalance();
public List<WageBalance> WageBalanceList = new List<WageBalance>();
public WageInfo()
{
}
}
public class WageBalance
{
public int EmployeeID {get;set;}
public string EmployeeName { get; set; }
public decimal NetWage { get; set; }
public decimal CarriedForwardAmount { get; set; }
}
Deduction
haveemployeeID
also? And how do you knowGetEarningsList()
andGetDeductionList()
return matching elements? How do you know they will stay that way? \$\endgroup\$