Beispiel #1
0
        public bool AddDrivingTest(Test test)
        {
            Trainee helpTrainee = GetTrainees().FirstOrDefault(t => t.ID == test.TraineeID);
            Tester  helpTester  = GetTesters().FirstOrDefault(t => t.ID == test.TesterID);


            //get all trainee tests. the last test will be first;
            var TraineeTests = ((from t in GetTests()
                                 where t.TraineeID == test.TraineeID
                                 select t).OrderByDescending(t => test.TestDay.Year)
                                .ThenByDescending(t => test.TestDay.Month)
                                .ThenByDescending(t => test.TestDay.Day).ToList());

            //check there is enough days between tests
            if (TraineeTests.Any())
            {
                TimeSpan ts = test.TestDay - TraineeTests.First().TestDay;
                if (ts.Days < Configuration.daysBetweenTests)
                {
                    throw new Exception("There must be at least " + Configuration.daysBetweenTests + " days before a trainee can take another test\n");
                }
            }
            //check trainee took enough lessons
            if (helpTrainee.NumOfLessons < Configuration.minLessons)
            {
                throw new Exception("A trainee cannot take a test if he took less than" + Configuration.minLessons + " lessons\n");
            }
            //find all tests trainee succedded
            var licence = from t in GetTests()
                          where t.TraineeID == test.TraineeID && t.TestResult == true
                          select t;

            //check if trainee already have a licence to this type of car
            foreach (var item in licence)
            {
                if (item.Vehicle == test.Vehicle)
                {
                    throw new Exception("Trainee already has licence for this type of car");
                }
            }

            try
            {
                IDal _dal = FactorySingletonDal.GetDal();
                if (_dal.AddDrivingTest(test))
                {
                    helpTester.NumOfTests++;
                    helpTrainee.NumOfTests++;
                    return(true);
                }
                return(false);
            }
            catch (Exception e)
            {
                throw e;
            }
        }