//Trainee public string AddTrainee(Trainee trainee) { try { //Tester id validation if (DAL_Class.IsIdFormatValid(trainee.Id) == false) { throw new Exception(trainee.GetName() + "'s Id " + trainee.Id + " is not valid"); } else if (DAL_Class.IsTraineeIdFound(trainee.Id) == true) { throw new Exception(trainee.GetName() + "'s Id " + trainee.Id + " is already found"); } else { DataSource.TraineesList.Add(trainee); SaveTraineesList(DataSource.TraineesList); return(null); } } catch (Exception e) { return(e.Message); } }
//Data manipulations on TRAINEES /// <summary> /// Adds a trainee to the trainee list in the Data Source /// </summary> /// <param name="trainee">Trainee to be added</param> public string AddTrainee(Trainee trainee) { string error = "Failed to add " + trainee.GetName(); if (IsTraineeIdFound(trainee.Id) == true) { return("Id " + trainee.Id + " was already found"); } else if (TraineePropertiesValidation(trainee) == true) { error = m_xamlImp.AddTrainee(trainee); } return(error); }
/// <summary> /// Updates a trainee's properties /// </summary> /// <param name="targetTrainee">Trainee to be updated</param> /// <param name="sourceTrainee">Trainee update source</param> public string UpdateTrainee(Trainee targetTrainee, Trainee sourceTrainee) { try { if (IsTraineeIdFound(targetTrainee.Id) == false) { throw new Exception(targetTrainee.GetName() + " was not found"); } else if (TraineePropertiesValidation(sourceTrainee) == false) { throw new Exception("Couldnt update the trainee because of 1 or more errors"); } else { return(m_xamlImp.UpdateTrainee(targetTrainee, sourceTrainee)); } } catch (Exception e) { return(e.Message); } }
/// <summary> /// Return true if all of the Test's properties are valid /// </summary> /// <param name="test">Test to be validated</param> /// <returns></returns> public static bool TestPropertiesValidation(Test test) { Trainee trainee = GetTraineeById(test.TraineeId); Tester tester = GetTesterById(test.TesterId); try { //Trainee's Id format validation if (IsIdFormatValid(trainee.Id) == false) { throw new Exception(trainee.GetName() + "'s Id is not valid"); } //Trainee's existence validation else if (trainee == null) { throw new Exception(trainee.GetName() + " was not found!"); } //Tester's Id format validation else if (IsIdFormatValid(tester.Id) == false) { throw new Exception(tester.GetName() + "'s id is not valid"); } //Tester's existence validation else if (tester == null) { throw new Exception(tester.GetName() + " was not found!"); } //Number of days passed since the trainee's last test validation else if (trainee.DaysPassedSinceLastTest < Configuration.MinimalDaysBetweenTests) { throw new Exception("Test can only be scheduled at least " + Configuration.MinimalDaysBetweenTests + " days after the trainee's last test."); } //Validating if the tester is available for test at that date & time else if (IsTesterFreeForTest(tester, test.TestDateAndTime) == false) { throw new Exception("Tester with Id" + tester.Id + " is busy at " + test.TestDateAndTime); } //Test location validation else if (IsAddressExists(test.TestLocation) == false) { throw new Exception("Test location \"" + test.TestLocation + "\" is not valid"); } //Tester's notes validations else if (test.TesterNotes == "" || test.TesterNotes == null) { throw new Exception("You didnt write any notes"); } //Trainee's driving lessons' count validation else if (trainee.DrivingLessonsCount < Configuration.MinimalLessonsCount) { throw new Exception(trainee.GetName() + " must do at least " + Configuration.MinimalLessonsCount + " driving lessons before doing a test"); } //Tester's weekly tests count validation else if (tester.MaximalWeeklyTests <= tester.WeeklyTestsCount) { throw new Exception(tester.GetName() + " can't do more than " + tester.MaximalWeeklyTests + " tests in a week"); } //Validating if the trainee is currently owning the desired lisence else if (IsLisenceOwned(trainee.OwnedLisences, trainee.WantedLisence) == false) { throw new Exception("Lisence " + trainee.CarType + " is already owned by " + trainee.GetName()); } //Validating if the tester can test a trainee for the trainee's desired lisence else if (tester.CarType != trainee.CarType) { throw new Exception(tester.GetName() + " can't test " + trainee.GetName() + " for lisence " + trainee.CarType); } else { return(true); } } catch (Exception e) { MessageBox.Show(e.ToString()); return(false); } }
//Validations private void ValidateTraineeIdField() { if (traineeIdTb.Validator == null) { return; } Trainee trainee = GetTrainee(); traineeIdTb.Validator.Visibility = Visibility.Visible; try { if (traineeIdTb.IsHintVisible == true || GetTraineeId() == "") { throw new Exception("trainee's Id field can't be empty"); } else if (BlValidations.IsIdFormatValid(GetTraineeId()) == false) { throw new Exception("Id " + trainee.Id + "'s format is not valid"); } else if (trainee == null) { throw new Exception("Couldn't find trainee with Id " + GetTraineeId()); } else if (GetTesterById(GetTraineeId()) != null) { throw new Exception("This Id belongs to an existing Tester"); } else if (trainee.DaysPassedSinceLastTest < Configuration.MinimalDaysBetweenTests) { throw new Exception("Test can only be scheduled at least " + Configuration.MinimalDaysBetweenTests + " days after the trainee's last test."); } else if (trainee.DrivingLessonsCount < Configuration.MinimalLessonsCount) { throw new Exception(trainee.GetName() + " must do at least " + Configuration.MinimalLessonsCount + " driving lessons before doing a test"); } else if (BlValidations.IsLisenceOwned(trainee.OwnedLisences, trainee.WantedLisence) == true) { throw new Exception("Lisence " + trainee.CarType + " is already owned by " + trainee.GetName()); } else if (BlValidations.IsCarTypeExist(trainee.ScheduleList, trainee.CarType) == true) { throw new Exception("trainee with Id " + trainee.Id + " is already have an upcomming test for " + trainee.CarType); } else { traineeIdTb.Validator.Validate(true); traineeIdTb.Validator.ToolTip = "Good"; traineeNameTb.Text = trainee.GetName(); traineeCarTypeCb.ComboBox.SelectedItem = trainee.CarType; } } catch (Exception e) { traineeIdTb.Validator.ToolTip = e.Message; traineeIdTb.Validator.Validate(false); traineeNameTb.Text = ""; traineeCarTypeCb.Text = ""; } }
//Events private void Window_Loaded(object sender, RoutedEventArgs e) { traineeDetailsTb.Text = string.Format("Id: {0}, {1}", m_targetTest.TraineeId, m_targetTrainee.GetName()); testerDetailsTb.Text = string.Format("Id: {0}, {1}", m_targetTester.Id, m_targetTester.GetName()); testDetailsTb.Text = string.Format("Testing for {0} (Car Type) \nat {1} \non {2}", m_targetTest.CarType, m_targetTest.TestLocation, m_targetTest.TestDateAndTime); m_mainWin = UIFactory.GetMainWin(); }