/// <summary> /// Sets the percent of a party in the population group equal to the desired value. /// It will take shares from all others proportionally. /// </summary> /// <param name="pol">The political group we are setting.</param> /// <param name="percent">The percent we are setting it to.</param> public void SetPartyPercent(PoliticalGroup pol, double percent) { if (percent <= 0 || percent > 1) { throw new ArgumentOutOfRangeException("Percent must be greater than 0 or less than or equal to 1."); } if (!PoliticalBreakdown.Any(x => x.PoliticalGroup.Id == pol.Id)) { PoliticalBreakdown.Add(new PoliticalBreakdown { ParentId = Id, Percent = 0, PoliticalGroupId = pol.Id }); } double oldPercent = PoliticalBreakdown .Single(x => x.PoliticalGroupId == pol.Id) .Percent; double add = (oldPercent - percent) / (percent - 1); PoliticalBreakdown .Single(x => x.PoliticalGroupId == pol.Id) .Percent += add; var newSum = PolPercent(); foreach (var group in PoliticalBreakdown) { group.Percent = group.Percent / newSum; } }
public void ShiftPartyPercent(PoliticalGroup pol, double percent) { if (!PoliticalBreakdown.Any(x => x.PoliticalGroup == pol)) { PoliticalBreakdown.Add(new PoliticalBreakdown { ParentId = Id, Percent = 0, PoliticalGroupId = pol.Id }); } PoliticalBreakdown.Single(x => x.PoliticalGroupId == pol.Id) .Percent += percent; var newSum = PolPercent(); foreach (var group in PoliticalBreakdown) { group.Percent = group.Percent / newSum; } }