/// <summary> /// updates a property for a mother /// </summary> /// <param name="m">the mother to update</param> /// <param name="prop">the property to update (if it is Comments, it will ADD the new value to the list)</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 UpdateMother(Mother m, Mother.Props prop, object newVal) { if (prop == Mother.Props.HoursNeeded) { DateTime[,] temp = newVal as DateTime[, ]; if (temp == null) { throw new Exception("wrong type for the 3rd parameter"); } for (int i = 0; i < 6; i++) { if (m.DaysNeeded[i] && temp[0, i].CompareTo(temp[1, i]) >= 0) { m.DaysNeeded[i] = false; UpdateMother(m, Mother.Props.DaysNeeded, m.DaysNeeded); throw new Exception("starting time must be before ending time"); } else if (!m.DaysNeeded[i]) // To make things simple: WorkDays is the "key" to WorkHours. That means that there will be no hours in a day in which the nanny doens't work { temp[0, i] = default(DateTime); temp[1, i] = default(DateTime); } } } dal.UpdateMother(m, prop, newVal); //if (prop == Mother.Props.DaysNeeded) //{ // Since we decided the HoursNeeded is depended on DaysNeeded, after a succesful change to the key we need to change the hours too // DateTime[,] temp = (DateTime[,])(m.HoursNeeded.Clone()); // for (int i = 0; i < 6; i++) // if (!m.DaysNeeded[i]) // { // temp[0, i] = default(DateTime); // temp[1, i] = default(DateTime); // } // UpdateMother(m, Mother.Props.HoursNeeded, temp); //} IEnumerable <Contract> HerContracts = from Contract con in GetContracts() where FindChildByID(con.ChildID).MotherID == m.ID select con; foreach (Contract con in HerContracts) { CalculateSalary(con); } }
public void UpdateMother(Mother m, Mother.Props prop, object newVal) { LoadData(TypeToLoad.Mother); XElement MotherToUpdate = (from ch in MothersRoot.Elements() where ch.Element("ID").Value == m.ID select ch).FirstOrDefault(); switch (prop) { case Mother.Props.Address: if (newVal is string) { MotherToUpdate.Element("Address").Value = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Mother.Props.Comments: if (newVal is string) { //MotherToUpdate.Element("Comments").RemoveAll(); //foreach (string comment in m.Comments) // MotherToUpdate.Element("Comments").Add(new XElement("Comment", comment)); MotherToUpdate.Element("Comments").Add(new XElement("Comment", (string)newVal)); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Mother.Props.DaysNeeded: if (newVal is bool[] && ((bool[])newVal).Length == 7) { MotherToUpdate.Element("DaysNeeded").RemoveAll(); for (int i = 0; i < 6; i++) { //m.DaysNeeded[i] = ((bool[])newVal)[i]; MotherToUpdate.Element("DaysNeeded").Add(new XElement(((Days)i).ToString(), ((bool[])newVal)[i])); } } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Mother.Props.FirstName: if (newVal is string) { MotherToUpdate.Element("FirstName").Value = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Mother.Props.HoursNeeded: DateTime[,] temp = newVal as DateTime[, ]; if (temp != null && temp.GetLength(0) == 2 && temp.GetLength(1) == 6) { MotherToUpdate.Element("HoursNeeded").RemoveAll(); for (int i = 0; i < 6; i++) { MotherToUpdate.Element("HoursNeeded").Add(new XElement(((Days)i).ToString(), new XElement("Begin", temp[0, i].Hour), new XElement("End", temp[1, i].Hour))); //m.HoursNeeded[0, i] = temp[0, i]; //m.HoursNeeded[1, i] = temp[1, i]; } } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Mother.Props.LastName: if (newVal is string) { MotherToUpdate.Element("LastName").Value = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Mother.Props.NeedNannyAddress: if (newVal is string) { MotherToUpdate.Element("NeedNannyAddress").Value = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Mother.Props.Phone: if (newVal is string) { MotherToUpdate.Element("Phone").Value = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; } MothersRoot.Save(Address + "\\Mothers.xml"); }
/// <summary> /// updates a property for a mother /// </summary> /// <param name="m">the mother to update</param> /// <param name="prop">the property to update (if it is Comments, it will ADD the new value to the list)</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 UpdateMother(Mother m, Mother.Props prop, object newVal) { switch (prop) { case Mother.Props.Address: if (newVal is string) { m.Address = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Mother.Props.Comments: if (newVal is string) { m.Comments.Add((string)newVal); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Mother.Props.DaysNeeded: if (newVal is bool[] && ((bool[])newVal).Length == 7) { for (int i = 0; i < 7; i++) { m.DaysNeeded[i] = ((bool[])newVal)[i]; } } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Mother.Props.FirstName: if (newVal is string) { m.FirstName = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Mother.Props.HoursNeeded: DateTime[,] temp = newVal as DateTime[, ]; if (temp != null && temp.GetLength(0) == 2 && temp.GetLength(1) == 6) { for (int i = 0; i < 6; i++) { m.HoursNeeded[0, i] = temp[0, i]; m.HoursNeeded[1, i] = temp[1, i]; } } else { throw new Exception("wrong type for the 3rd parameter"); } break; //case Mother.Props.ID: // if (newVal is string) // { // foreach (Nanny ch in DataSource.Nannies) // if (ch.ID == m.ID) // throw new Exception("ID already exists!"); // foreach (Child ch in DataSource.Children) // if (ch.ID == m.ID) // throw new Exception("ID already exists!"); // foreach (Mother ch in DataSource.Mothers) // if (ch != m && ch.ID == m.ID) // throw new Exception("ID already exists!"); // m.ID = (string)newVal; // } // else // throw new Exception("wrong type for the 3rd parameter"); // break; case Mother.Props.LastName: if (newVal is string) { m.LastName = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Mother.Props.NeedNannyAddress: if (newVal is string) { m.NeedNannyAddress = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Mother.Props.Phone: if (newVal is string) { m.Phone = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; } }