/// <summary> /// updates a property for a contract /// </summary> /// <param name="m">the contract to update</param> /// <param name="prop">the property to update </param> /// <param name="newVal">the new value for the property</param> /// <param name="TerminateLoop">if set on true, it will not use calculate salary in the end, used to prevent recurisve calls</param> /// <exception cref="System.Exception">thrown if the new value is not of a proper type for the property</exception> public void UpdateContract(Contract c, Contract.Props prop, object newVal, bool TerminateLoop = false) { Nanny NannyInContract; Child ChildInContract; switch (prop) { case Contract.Props.ChildID: if (!(newVal is string)) { throw new Exception("wrong type for the 3rd parameter"); } ChildInContract = FindChildByID((string)newVal); if (ChildInContract == null) { throw new Exception("Child doesn't exist"); } NannyInContract = FindNannyByID(c.NannyID); if (Math.Abs((ChildInContract.BirthDate - DateTime.Today).Days) / 30 < NannyInContract.MinAge || (ChildInContract.BirthDate - DateTime.Today).Days / 30 > NannyInContract.MaxAge) { throw new Exception("child's age out of nannies boundries"); } break; case Contract.Props.NannyID: if (!(newVal is string)) { throw new Exception("wrong type for the 3rd parameter"); } NannyInContract = FindNannyByID((string)newVal); if (NannyInContract == null) { throw new Exception("Nanny doesn't exist"); } break; case Contract.Props.Beginning: if (newVal is DateTime) { if (((DateTime)newVal).CompareTo(c.End) > 0) { throw new Exception("beginning must come before the end"); } } break; case Contract.Props.End: if (newVal is DateTime) { if (((DateTime)newVal).CompareTo(c.Beginning) < 0) { throw new Exception("end must come after the beginning"); } } break; case Contract.Props.PerHour: if (newVal is int) { if ((int)newVal <= 0) { throw new Exception("salary must be a positive integer"); } } break; } //if(prop == Contract.Props.ChildID) //{ // if (!(newVal is string)) // throw new Exception("wrong type for the 3rd parameter"); // Child ChildInContract = FindChildByID((string)newVal); // if (ChildInContract == null) // throw new Exception("Child doesn't exist"); //} //if (prop == Contract.Props.NannyID) //{ // if (!(newVal is string)) // throw new Exception("wrong type for the 3rd parameter"); // Nanny NannyInContract = FindNannyByID((string)newVal); // if (NannyInContract == null) // throw new Exception("Nanny doesn't exist"); //} //if (prop == Contract.Props.Beginning && (!(newVal is DateTime) || ((DateTime)newVal).CompareTo(c.End) > 0)) // throw new Exception("beginning must come before the end or wrong parameter type"); //if (prop == Contract.Props.End && (!(newVal is DateTime) || ((DateTime)newVal).CompareTo(c.Beginning) < 0)) // throw new Exception("end must come after the beginning or wrong parameter type"); //if (prop == Contract.Props.PerHour && (!(newVal is int) || (int)newVal <= 0)) // throw new Exception("salary must be a positive integer"); dal.UpdateContract(c, prop, newVal); if (!TerminateLoop && prop != Contract.Props.IsMet && prop != Contract.Props.Beginning && prop != Contract.Props.End && prop != Contract.Props.IsSigned && prop != Contract.Props.PerMonth) { CalculateSalary(c); } }
public void UpdateContract(Contract c, Contract.Props prop, object newVal) { LoadData(TypeToLoad.Contract); XElement ContractToUpdate = (from ch in ContractsRoot.Elements() where ch.Element("SerialNumber").Value == c.SerialNumber select ch).FirstOrDefault(); switch (prop) { case Contract.Props.Beginning: if (newVal is DateTime) { ContractToUpdate.Element("Beginning").RemoveAll(); ContractToUpdate.Element("Beginning").Add(new XElement("Year", ((DateTime)newVal).Year), new XElement("Month", ((DateTime)newVal).Month), new XElement("Day", ((DateTime)newVal).Day)); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Contract.Props.ChildID: if (newVal is string) { foreach (Child ch in GetChildren()) { if (ch.ID == (string)newVal) { ContractToUpdate.Element("ChildID").Value = (string)newVal; break; } //else // throw new Exception("child doesn't exist"); } } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Contract.Props.End: if (newVal is DateTime) { ContractToUpdate.Element("End").RemoveAll(); ContractToUpdate.Element("End").Add(new XElement("Year", ((DateTime)newVal).Year), new XElement("Month", ((DateTime)newVal).Month), new XElement("Day", ((DateTime)newVal).Day)); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Contract.Props.IsByHour: if (newVal is bool) { ContractToUpdate.Element("IsByHour").Value = newVal.ToString(); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Contract.Props.IsMet: if (newVal is bool) { ContractToUpdate.Element("IsMet").Value = newVal.ToString(); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Contract.Props.IsSigned: if (newVal is bool) { ContractToUpdate.Element("IsSigned").Value = newVal.ToString(); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Contract.Props.NannyID: if (newVal is string) { foreach (Nanny ch in GetNannies()) { if (ch.ID == (string)newVal) { ContractToUpdate.Element("NannyID").Value = (string)newVal; break; } else { throw new Exception("nanny doesn't exist"); } } } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Contract.Props.PerHour: if (newVal is int) { ContractToUpdate.Element("PerHour").Value = newVal.ToString(); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Contract.Props.PerMonth: if (newVal is int) { ContractToUpdate.Element("PerMonth").Value = newVal.ToString(); } else { throw new Exception("wrong type for the 3rd parameter"); } break; } ContractsRoot.Save(Address + "\\Contracts.xml"); }
/// <summary> /// updates a property for a contract /// </summary> /// <param name="m">the contract to update</param> /// <param name="prop">the property to update </param> /// <param name="newVal">the new value for the property</param> /// <exception cref="System.Exception">thrown if the new value is not of a proper type for the property</exception> public void UpdateContract(Contract c, Contract.Props prop, object newVal) { switch (prop) { case Contract.Props.Beginning: if (newVal is DateTime) { c.Beginning = (DateTime)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Contract.Props.ChildID: if (newVal is string) { foreach (Child ch in DataSource.Children) { if (ch.ID == (string)newVal) { c.ChildID = (string)newVal; break; } //else // throw new Exception("child doesn't exist"); } } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Contract.Props.End: if (newVal is DateTime) { c.End = (DateTime)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Contract.Props.IsByHour: if (newVal is bool) { c.IsByHour = (bool)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Contract.Props.IsMet: if (newVal is bool) { c.IsMet = (bool)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; //case Contract.Props.IsSigned: // if (newVal is bool) // c.IsSigned = (bool)newVal; // else // throw new Exception("wrong type for the 3rd parameter"); // break; case Contract.Props.NannyID: if (newVal is string) { foreach (Nanny ch in DataSource.Nannies) { if (ch.ID == (string)newVal) { c.NannyID = (string)newVal; break; } else { throw new Exception("nanny doesn't exist"); } } } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Contract.Props.PerHour: if (newVal is int) { c.PerHour = (int)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Contract.Props.PerMonth: if (newVal is int) { c.PerMonth = (int)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; } }