public Transfer[] Calculate(Expenditure[] expenditures, string[] partners) { var engagements = CalculateEngagements(expenditures, partners); var credits = CalculateCredits(expenditures, partners); var transfers = Calculate(engagements, credits); return transfers.ToArray(); }
public void AbsoluteValueTest() { var albina = new Expenditure { Amount = 100, Who = "Глеб" }; var danil = new Expenditure { Amount = 0, Who = "Альбина" }; var gleb = new Expenditure { Amount = 0, Who = "Данил" }; var calculator = new FlowsCalculator(); var transactions = calculator.Calculate(new[] { albina, gleb, danil }, new[] { "Глеб", "Альбина", "Данил" }); }
public void Test() { var albina = new Expenditure{Amount = 200, Who = "Глеб"}; var danil = new Expenditure{Amount = 100, Who = "Альбина"}; var gleb = new Expenditure { Amount = 9, Who = "Данил" }; var calculator = new FlowsCalculator(); var transactions = calculator.Calculate(new[] { albina, gleb, danil }, new[] { "Глеб", "Альбина", "Данил" }); transactions.First(x => x.From == "Данил").Amount.Should().Be(94); transactions.First(x => x.From == "Альбина").Amount.Should().Be(3); }
public void Test2() { var albina = new Expenditure { Amount = 200, Who = "Глеб" }; var danil = new Expenditure { Amount = 674, Who = "Альбина" }; var gleb = new Expenditure { Amount = 500, Who = "Данил" }; var sasha = new Expenditure { Amount = 13, Who = "Санек" }; var calculator = new FlowsCalculator(); var transactions = calculator.Calculate(new[] { albina, gleb, danil, sasha }, new[] { "Глеб", "Альбина", "Данил", "Санек" }); transactions.First(x => (x.From == "Санек" && x.To == "Альбина")).Amount.Should().Be((decimal)327.25); transactions.First(x => (x.From == "Глеб" && x.To == "Данил")).Amount.Should().Be((decimal)146.75); transactions.First(x => (x.From == "Санек" && x.To == "Данил")).Amount.Should().Be((decimal)6.5); }
private List<Transfer> CalculateTransfers(Expenditure[] expenditures, string[] partners) { var transfers = new List<Transfer>(); var total = expenditures.Sum(x => x.Amount); var average = decimal.Round(total / partners.Length, 2); foreach (var partner in partners) { //если этот партнер что-то тратил и он потратил меньше среднего //то пусть довнесет разницу if (expenditures.Any(x => x.Who == partner)) { var partnerExpenditure = expenditures.First(x => x.Who == partner); if (expenditures.First(x => x.Who == partner).Amount < average) { transfers.Add(new Transfer { Amount = average - partnerExpenditure.Amount, From = partner, To = FundName }); } else { //если партнер потратил больше среднего //то пусть возмет из общего котла transfers.Add(new Transfer { Amount = partnerExpenditure.Amount - average, From = FundName, To = partner }); } } else { //партнер не тратил ничего, так что пусть вносит среднее transfers.Add(new Transfer { Amount = average, From = partner, To = FundName }); } } return transfers; }
public Transfer[] Calculate(Expenditure[] expenditures, string[] partners) { var transfers = CalculateTransfers(expenditures, partners); return transfers.ToArray(); }