/// <summary> /// updates a property for a nanny /// </summary> /// <param name="n">the nanny to update</param> /// <param name="prop">the property to update (if it is Recommendations, 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 UpdateNanny(Nanny n, Nanny.Props prop, object newVal) { if ((prop == Nanny.Props.MaxChildren || prop == Nanny.Props.MaxAge || prop == Nanny.Props.MinAge || prop == Nanny.Props.HourSalary || prop == Nanny.Props.MonthSalary || prop == Nanny.Props.Expertise) && (!(newVal is int) || (int)newVal <= 0)) { throw new Exception(prop + "'s value must be a positive integer"); } if (prop == Nanny.Props.MaxAge) { if (!(newVal is int)) { throw new Exception("wrong type for the 3rd parameter"); } if ((int)newVal < n.MinAge) { throw new Exception("maximal age cannot be below minimal age or wrong parameter type"); } string errMsg = "Contracts exist which contradict the new age limit:"; IEnumerable <Contract> NannysContracts = from Contract c in GetContracts() where c.NannyID == n.ID select c; foreach (Contract c in NannysContracts) { if (Math.Abs((FindChildByID(c.ChildID).BirthDate - DateTime.Today).Days) / 30 > (int)newVal) { errMsg += "\n" + c.SerialNumber; } } if (errMsg != "Contracts exist which contradict the new age limit:") { throw new Exception(errMsg); } } if (prop == Nanny.Props.MinAge) { if (!(newVal is int)) { throw new Exception("wrong type for the 3rd parameter"); } if ((int)newVal > n.MaxAge) { throw new Exception("maximal age cannot be below minimal age or wrong parameter type"); } string errMsg = "Contracts exist which contradict the new age limit:"; IEnumerable <Contract> NannysContracts = from Contract c in GetContracts() where c.NannyID == n.ID select c; foreach (Contract c in NannysContracts) { if (Math.Abs((FindChildByID(c.ChildID).BirthDate - DateTime.Today).Days) / 30 < (int)newVal) { errMsg += "\n" + c.SerialNumber; } } if (errMsg != "Contracts exist which contradict the new age limit:") { throw new Exception(errMsg); } } //&& (!(newVal is int) || (int)newVal < n.MinAge)) //throw new Exception("maximal age cannot be below minimal age or wrong parameter type"); //if (prop == Nanny.Props.MinAge && (!(newVal is int) || (int)newVal > n.MaxAge)) // throw new Exception("minimal age cannot be above maximal age or wrong parameter type"); if (prop == Nanny.Props.WorkHours) { 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 (n.WorkDays[i] && temp[0, i].CompareTo(temp[1, i]) >= 0) { n.WorkDays[i] = false; UpdateNanny(n, Nanny.Props.WorkDays, n.WorkDays); throw new Exception("starting time must be before ending time"); } else if (!n.WorkDays[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.UpdateNanny(n, prop, newVal); //if (prop == Nanny.Props.WorkDays) //{ // Since we decided the WorkHours is depended on WorkDays, after a succesful change to the key we need to change the hours too // DateTime[,] temp = (DateTime[,])(n.WorkHours.Clone()); // for (int i = 0; i < 6; i++) // if (!n.WorkDays[i]) // { // temp[0, i] = default(DateTime); // temp[1, i] = default(DateTime); // } // UpdateNanny(n, Nanny.Props.WorkHours, temp); //} IEnumerable <Contract> HerContracts = from Contract con in GetContracts() where con.NannyID == n.ID select con; foreach (Contract con in HerContracts) { CalculateSalary(con); } }
public void UpdateNanny(Nanny n, Nanny.Props prop, object newVal) { LoadData(TypeToLoad.Nanny); XElement NannyToUpdate = (from ch in NanniesRoot.Elements() where ch.Element("ID").Value == n.ID select ch).FirstOrDefault(); switch (prop) { case Nanny.Props.Address: if (newVal is string) { NannyToUpdate.Element("Address").Value = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.Elevator: if (newVal is bool) { NannyToUpdate.Element("Elevator").Value = newVal.ToString(); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.Expertise: if (newVal is int) { NannyToUpdate.Element("Expertise").Value = newVal.ToString(); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.FirstName: if (newVal is string) { NannyToUpdate.Element("FirstName").Value = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.Floor: if (newVal is int) { NannyToUpdate.Element("Floor").Value = newVal.ToString(); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.HourSalary: if (newVal is int) { NannyToUpdate.Element("HourSalary").Value = newVal.ToString(); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.IsCostByHour: if (newVal is bool) { NannyToUpdate.Element("IsCostByHour").Value = newVal.ToString(); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.LastName: if (newVal is string) { NannyToUpdate.Element("LastName").Value = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.MaxAge: if (newVal is int) { NannyToUpdate.Element("MaxAge").Value = newVal.ToString(); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.MaxChildren: if (newVal is int) { NannyToUpdate.Element("MaxChildren").Value = newVal.ToString(); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.MinAge: if (newVal is int) { NannyToUpdate.Element("MinAge").Value = newVal.ToString(); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.MonthSalary: if (newVal is int) { NannyToUpdate.Element("MonthSalary").Value = newVal.ToString(); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.Phone: if (newVal is string) { NannyToUpdate.Element("Phone").Value = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.Recommendations: if (newVal is string) { NannyToUpdate.Element("Recommendations").Add(new XElement("Recommendation", (string)newVal)); } break; case Nanny.Props.VacationByMinisterOfEducation: if (newVal is bool) { NannyToUpdate.Element("VacationByMinisterOfEducation").Value = newVal.ToString(); } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.WorkDays: if (newVal is bool[] && ((bool[])newVal).Length == 7) { NannyToUpdate.Element("WorkDays").RemoveAll(); for (int i = 0; i < 6; i++) { //m.DaysNeeded[i] = ((bool[])newVal)[i]; NannyToUpdate.Element("WorkDays").Add(new XElement(((Days)i).ToString(), ((bool[])newVal)[i])); } } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.WorkHours: DateTime[,] temp = newVal as DateTime[, ]; if (temp != null && temp.GetLength(0) == 2 && temp.GetLength(1) == 6) { NannyToUpdate.Element("WorkHours").RemoveAll(); for (int i = 0; i < 6; i++) { NannyToUpdate.Element("WorkHours").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; } NanniesRoot.Save(Address + "\\Nannies.xml"); }
/// <summary> /// updates a property for a nanny /// </summary> /// <param name="n">the nanny to update</param> /// <param name="prop">the property to update (if it is Recommendations, 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 UpdateNanny(Nanny n, Nanny.Props prop, object newVal) { switch (prop) { case Nanny.Props.Address: if (newVal is string) { n.Address = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; //case Nanny.Props.BirthDate: // if (newVal is DateTime) // n.BirthDate = (DateTime)newVal; // else // throw new Exception("wrong type for the 3rd parameter"); // break; case Nanny.Props.Elevator: if (newVal is bool) { n.Elevator = (bool)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.Expertise: if (newVal is int) { n.Expertise = (int)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.FirstName: if (newVal is string) { n.FirstName = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.Floor: if (newVal is int) { n.Floor = (int)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.HourSalary: if (newVal is int) { n.HourSalary = (int)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; //case Nanny.Props.ID: // if (newVal is string) // { // foreach (Nanny ch in DataSource.Nannies) // if (n != ch && ch.ID == n.ID) // throw new Exception("ID already exists!"); // foreach (Child ch in DataSource.Children) // if (ch.ID == n.ID) // throw new Exception("ID already exists!"); // foreach (Mother ch in DataSource.Mothers) // if (ch.ID == n.ID) // throw new Exception("ID already exists!"); // n.ID = (string)newVal; // } // else // throw new Exception("wrong type for the 3rd parameter"); // break; case Nanny.Props.IsCostByHour: if (newVal is bool) { n.IsCostByHour = (bool)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.LastName: if (newVal is string) { n.LastName = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.MaxAge: if (newVal is int) { n.MaxAge = (int)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.MaxChildren: if (newVal is int) { n.MaxChildren = (int)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.MinAge: if (newVal is int) { n.MinAge = (int)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.MonthSalary: if (newVal is int) { n.MonthSalary = (int)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.Phone: if (newVal is string) { n.Phone = (string)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.Recommendations: if (newVal is string) { n.Recommendations.Add((string)newVal); } break; case Nanny.Props.VacationByMinisterOfEducation: if (newVal is bool) { n.VacationByMinisterOfEducation = (bool)newVal; } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.WorkDays: if (newVal is bool[] && ((bool[])newVal).Length == 7) { for (int i = 0; i < 7; i++) { n.WorkDays[i] = ((bool[])newVal)[i]; } } else { throw new Exception("wrong type for the 3rd parameter"); } break; case Nanny.Props.WorkHours: DateTime[,] temp = newVal as DateTime[, ]; if (temp != null && temp.GetLength(0) == 2 && temp.GetLength(1) == 6) { for (int i = 0; i < 6; i++) { n.WorkHours[0, i] = temp[0, i]; n.WorkHours[1, i] = temp[1, i]; } } else { throw new Exception("wrong type for the 3rd parameter"); } break; } }