//Method to update driver points static string updateDriverPoints(Driver driver, TrafficViolationType typeOfTrafficViolation) { if ((driver.Points - typeOfTrafficViolation.Points) > 0) { driver.Points = driver.Points - typeOfTrafficViolation.Points; } else { driver.Points = 0; } Console.Write($"Driver ({driver.Id}) with {driver.Points} points{System.Environment.NewLine}"); return($"Driver ({driver.Id}) with {driver.Points} points"); }
//Method to register a traffic violation public static string registerTrafficViolation(string carPlate, string polismanId, string idTrafficViolation, DateTime date, string description) { string result = string.Empty; try { //Search and get trafficViolationType TrafficViolationType typeOfTrafficViolation = trafficViolationTypesList.Find(x => x.IdTrafficViolation.Equals(idTrafficViolation)); if (typeOfTrafficViolation != null) { //Seach and get driver Driver driverInfractor = driversList.Find(x => x.Id.Equals(carsList.Find(c => c.CarPlate.Equals(carPlate)).Driver)); //If driver has a valid license (with points) if (driverInfractor.Points > 0) { //Save trafficViolation in temporal variable TrafficViolation trafficViolationtoAdd = new TrafficViolation(idTrafficViolation, polismanId, typeOfTrafficViolation.Description, date, description, carPlate, typeOfTrafficViolation.Points); //Search all driver's car List <string> driverCars = carsList.Where(x => x.Driver.Equals(driverInfractor.Id)).Select(x => x.CarPlate).ToList(); //Search all trafficViolations of all driver's car List <TrafficViolation> driverTrafficViolations = trafficViolationsList.Where(x => driverCars.Contains(x.Car)).OrderByDescending(x => x.Date).ToList(); if (driverTrafficViolations.Count() > 0) { TrafficViolation lastDriverTrafficViolation = driverTrafficViolations.First(); //If trafficViolation to add is more older than the previous trafficViolation if (lastDriverTrafficViolation.Date.CompareTo(trafficViolationtoAdd.Date) > 0) { //Check points of driver if ((driverInfractor.Points - trafficViolationtoAdd.Points) <= 0 && driverInfractor.Points >= 0) { trafficViolationsList.Remove(lastDriverTrafficViolation); //driverInfractor.Points += lastDriverTrafficViolations.Points; result = updateDriverPoints(driverInfractor, typeOfTrafficViolation); //Increment numberOfViolations in typeTrafficViolation and policeman typeOfTrafficViolation.NumberOfViolations++; polismenList.Find(x => x.PolismanId.Equals(polismanId)).NumberOfViolations++; //Add trafficViolatio to List trafficViolationsList.Add(trafficViolationtoAdd); } else { //Increment numberOfViolations in typeTrafficViolation and policeman result = updateDriverPoints(driverInfractor, typeOfTrafficViolation); typeOfTrafficViolation.NumberOfViolations++; polismenList.Find(x => x.PolismanId.Equals(polismanId)).NumberOfViolations++; trafficViolationsList.Add(trafficViolationtoAdd); } } else { result = updateDriverPoints(driverInfractor, typeOfTrafficViolation); //Increment numberOfViolations in typeTrafficViolation and policeman typeOfTrafficViolation.NumberOfViolations++; polismenList.Find(x => x.PolismanId.Equals(polismanId)).NumberOfViolations++; trafficViolationsList.Add(trafficViolationtoAdd); } } else { result = updateDriverPoints(driverInfractor, typeOfTrafficViolation); //Increment numberOfViolations in typeTrafficViolation and policeman typeOfTrafficViolation.NumberOfViolations++; polismenList.Find(x => x.PolismanId.Equals(polismanId)).NumberOfViolations++; //If driver hasn't trafficViolations trafficViolationsList.Add(trafficViolationtoAdd); } } else { result = "ERROR: Driver without license"; Console.Write($"ERROR: Driver without license{System.Environment.NewLine}"); } trafficViolationsList = trafficViolationsList.OrderBy(x => x.Date).ToList(); } } catch (Exception e) { Console.Write($"Method:registerTrafficViolation. {System.Environment.NewLine}" + $"Error: {e.ToString()}.{System.Environment.NewLine}" + $"Message: {e.Message}.{System.Environment.NewLine}"); } return(result); }