/// <summary> /// Add risk to the policy of insured object. /// </summary> /// <param name="nameOfInsuredObject">Name of insured object</param> /// <param name="risk">Risk that must be added</param> /// <param name="validFrom">Date when risk becomes active. Can not be in the past</param> public void AddRisk(string nameOfInsuredObject, Risk risk, DateTime validFrom) { if (validFrom < DateTime.Now) { throw new RiskAddInPastException(string.Format("Error: Risk can not be in the past {0:dd/MM/yyyy}!", validFrom)); } this.GetPolicy(nameOfInsuredObject, validFrom).InsuredRisks.Add(risk); this.SaveChanges(); }
/// <summary> /// Remove risk from the policy of insured object. /// </summary> /// <param name="nameOfInsuredObject">Name of insured object</param> /// <param name="risk">Risk that must be removed</param> /// <param name="validTill">Date when risk become inactive. Must be equal to or greater than date when risk become active</param> public void RemoveRisk(string nameOfInsuredObject, Risk risk, DateTime validTill) { var policy = this.GetPolicy(nameOfInsuredObject, validTill); if (policy == null) { throw new PolicyNotFoundException(string.Format("Error: No policy found '{0}'!", nameOfInsuredObject)); } var riskToRemove = policy.InsuredRisks.FirstOrDefault(q => q.Name == risk.Name && q.YearlyPrice == risk.YearlyPrice); if (riskToRemove == null) { throw new Exception(string.Format("Error: No risk to remove '{0}'!", risk.Name)); } policy.InsuredRisks.Remove(riskToRemove); this.SaveChanges(); }