Exemplo n.º 1
0
        public IHttpActionResult PostTrafficViolation(TrafficViolation trafficViolation)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.TrafficViolations.Add(trafficViolation);
            db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = trafficViolation.TrafficViolationId }, trafficViolation));
        }
Exemplo n.º 2
0
        public IHttpActionResult DeleteTrafficViolation(int id)
        {
            TrafficViolation trafficViolation = db.TrafficViolations.Find(id);

            if (trafficViolation == null)
            {
                return(NotFound());
            }

            db.TrafficViolations.Remove(trafficViolation);
            db.SaveChanges();

            return(Ok(trafficViolation));
        }
Exemplo n.º 3
0
        public void CheckWriteIntoCsvFile()
        {
            try
            {
                TrafficViolationDetection trafficViolationDetection = new TrafficViolationDetection();
                string csvFilePath = @"\\tsclient\E\vinothkanth\Assesment\GovernmentOfIndia\TrafficViolence\dummyRecord.csv";

                TrafficViolation trafficViolation = null;

                // it will be throw an
                trafficViolationDetection.WriteIntoCsvFile(trafficViolation, csvFilePath);
            }
            catch (Exception ex)
            {
                Assert.AreEqual("Object reference not set to an instance of an object.", ex.Message.ToString());
            }
        }
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                FileStream              fsTrafficViolations = null;
                TrafficViolation        violation           = new TrafficViolation();
                BinaryFormatter         bfTrafficViolations = new BinaryFormatter();
                List <TrafficViolation> violations          = new List <TrafficViolation>();
                string strTrafficViolationsFile             = Server.MapPath("~/App_Data/TrafficViolations.tts");

                if (!string.IsNullOrEmpty("TrafficViolationNumber"))
                {
                    if (System.IO.File.Exists(strTrafficViolationsFile))
                    {
                        using (fsTrafficViolations = new FileStream(strTrafficViolationsFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            violations = (List <TrafficViolation>)bfTrafficViolations.Deserialize(fsTrafficViolations);
                        }
                    }

                    TrafficViolation tv = violations.Find(viol => viol.TrafficViolationId == id);

                    violations.Remove(tv);

                    using (fsTrafficViolations = new FileStream(strTrafficViolationsFile, FileMode.Create, FileAccess.Write, FileShare.Write))
                    {
                        bfTrafficViolations.Serialize(fsTrafficViolations, violations);
                    }
                }

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
        public void CheckInfractionData()
        {
            // To check the currect data
            Infraction validInfraction = new Infraction("001/2018", "PC220", "09/09/2013", "Chennai", "speeding");
            string     actualValue     = validInfraction.GetIntoCsvFormat();
            string     expectedValue   = @"001/2018,PC220,09/09/2013,Chennai,speeding";

            Assert.AreEqual(expectedValue, actualValue);

            try
            {
                // to cheack empty fields
                Infraction infraction = new Infraction(" ", " ", " ", " ", " ");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("Invalid infraction ticket number", ex.Message);
            }

            try
            {
                // to cheack invalid infraction id, the real formate is 0000/1234
                Infraction infraction = new Infraction("0670", "PC101", "03/03/2018", "Chennai", "Speeding");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("Invalid infraction ticket number", ex.Message);
            }

            try
            {
                // to cheack invalid police id , the real formate is AB000
                Infraction infraction = new Infraction("001/2018", "LL-101", "03/03/2018", "Chennai", "Speeding");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("Invalid police Id", ex.Message);
            }

            try
            {
                // to cheack invalid date
                Infraction infraction = new Infraction("001/2018", "PC101", "-3/+3/218", "Chennai", "Speeding");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("Invalid date", ex.Message);
            }

            try
            {
                // to cheack future date
                Infraction infraction = new Infraction("001/2018", "PC101", "03/03/2028", "Chennai", "Speeding");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("It is a future date", ex.Message);
            }


            try
            {
                // to cheack invalid place, it dont have any special charecter
                Infraction infraction = new Infraction("001/2018", "PC101", "08/09/2018", "Chenna=-sd11#", "Speeding");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("Invalid location", ex.Message);
            }

            try
            {
                // to cheack invalid infraction format,it dont have any special charecter
                Infraction infraction = new Infraction("001/2018", "PC101", "03/12/2018", "Madhurai", "S--pe@eding");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("Invalid infraction format", ex.Message);
            }

            try
            {
                // to check empty values
                TrafficViolation.CreateInfraction(new string[] { });
            }
            catch (IndexOutOfRangeException exception)
            {
                Assert.AreEqual("Array Index Out Of Bounce", exception.Message);
            }

            try
            {
                // to check out of boundary data
                TrafficViolation.CreateInfraction(new string[] { "data", "data" });
            }
            catch (IndexOutOfRangeException exception)
            {
                Assert.AreEqual("Array Index Out Of Bounce", exception.Message);
            }

            try
            {
                // to check invalid data
                TrafficViolation.CreateInfraction(new string[] { "001/2018", "PC101", "03/12/2018", "Madhurai", "S--pe@eding" });
            }
            catch (Exception exception)
            {
                Assert.AreEqual("Invalid infraction format", exception.Message);
            }
        }
        public void CheckVehicleData()
        {
            // To check valid data
            Vehicle validVehicle  = new Vehicle("TN31CT5052", "RAM", "GUINDY NH45 MAIN ROAD", "LMV", "BAJAJ R16", "02/07/2025");
            string  actualValue   = @"TN31CT5052,RAM,GUINDY NH45 MAIN ROAD,LMV,BAJAJ R16,02/07/2025";
            string  expectedValue = validVehicle.GetIntoCsvFormat();

            Assert.AreEqual(expectedValue, actualValue);

            try
            {
                // to test invalid vehicle data
                Vehicle vehicle = new Vehicle(" ", " ", " ", " ", " ", " ");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Invalid Vehicle License Number", exception.Message);
            }

            try
            {
                // to test invalid vehicle license number, the real value eg. TN12AB1234
                Vehicle vehicle = new Vehicle("T3C52", "RAM", "GUINDY MAIN ROAD", "LMV", "BAJAJ R16", "02/07/2025");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Invalid Vehicle License Number", exception.Message);
            }

            try
            {
                // to check invalid name , the name formate RAM
                Vehicle vehicle = new Vehicle("TN32CT5252", "R]AM +3", "GUINDY MAIN ROAD", "LMV", "BAJAJ R16", "02/07/2025");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Invalid name format", exception.Message);
            }

            try
            {
                // to check invalid address, the adderss formate 2nd main road chennai
                Vehicle vehicle = new Vehicle("TN32CN5252", "Krish", "+;.GUINDY- MAIN ROA]D", "LMV", "BAJAJ R16", "08/07/2025");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Invalid address format", exception.Message);
            }

            try
            {
                // to check invalid category
                Vehicle vehicle = new Vehicle("TN32CN5252", "Krish", "2nd MainRoad Salem", "RT", "BAJAJ R16", "03/07/2025");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Invalid vehicle category", exception.Message);
            }

            try
            {
                // to check invalid model,  it not contain any special charecter
                Vehicle vehicle = new Vehicle("TN32CN5252", "Krish", "2nd MainRoad Salem", "LMV", "B[-AJAJ +R16", "12/07/2025");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Invalid vehicle model formate", exception.Message);
            }

            try
            {
                // to check invalid date
                Vehicle vehicle = new Vehicle("TN32NC5252", "Krish", "2nd MainRoad Salem", "LMV", "Bajaj R15", "32/00/2125");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Invalid expiry date formate", exception.Message);
            }

            try
            {
                // to check empty list
                TrafficViolation.CreateVehicle(new string[] { });
            }
            catch (IndexOutOfRangeException exception)
            {
                Assert.AreEqual("Array Index Out Of Bounce", exception.Message);
            }

            try
            {
                // to check out of boundary data
                TrafficViolation.CreateVehicle(new string[] { "data", "data" });
            }
            catch (IndexOutOfRangeException exception)
            {
                Assert.AreEqual("Array Index Out Of Bounce", exception.Message);
            }

            try
            {
                // to check out of boundary data
                TrafficViolation.CreateVehicle(new string[] { "#", "TN23CN5252", "RAM", "GUINDY MAIN ROAD", "PLK", "BAJAJ R16", "03/07/2025" });
            }
            catch (Exception exception)
            {
                Assert.AreEqual("Invalid vehicle category", exception.Message);
            }
        }
        public void CheckDriverData()
        {
            //to check valid data
            Driver validDriver   = new Driver("TN5619951234567", "Ravanan", "Address 2nd street", "03/03/2000", "07/07/2018", "HMV");
            string actualValue   = validDriver.GetIntoCsvFormat();
            string expectedValue = @"TN5619951234567,Ravanan,Address 2nd street,HMV,03/03/2000,07/07/2018";

            Assert.AreEqual(expectedValue, actualValue);

            try
            {
                // test Invalid Driver
                Driver driver = new Driver(" ", " ", " ", " ", " ", " ");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Invalid Driver License Id", exception.Message);
            }

            try
            {
                // test Invalid Driver license id, the id formate is TN0019991234567
                Driver driver = new Driver("TN56151235", "Ravanan", "Address 2nd street", "03/03/2000", "07/07/2018", "HMV");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Invalid Driver License Id", exception.Message);
            }

            try
            {
                // test Invalid name, the name formate Ravanan , it dont have any special charecter
                Driver driver = new Driver("TN5619951234567", "23*Ravanan", "Address 2nd street", "03/03/2000", "07/07/2018", "HMV");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Invalid Name", exception.Message);
            }

            try
            {
                // test Invalid address formate, it not contain any special charecter
                Driver driver = new Driver("TN5619951234567", "Ravanan", "-=Addre32ss = 2nd street", "03/03/2000", "07/07/2018", "HMV");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Invalid Address", exception.Message);
            }

            try
            {
                // test Invalid DOB date
                Driver driver = new Driver("TN5619951234567", "Ravanan", "Address 2nd street", "32/15/2100", "07/07/2000", "HMV");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Invalid DOB Formate", exception.Message);
            }

            try
            {
                // test Invalid DOI date
                Driver driver = new Driver("TN5619951234567", "Ravanan", "Address 2nd street", "03/05/2000", "04/42/1998", "HMV");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Invalid DOI Formate", exception.Message);
            }

            try
            {
                // test Invalid License Category
                Driver driver = new Driver("TN5619951234567", "Ravanan", "Address 2nd street", "03/05/2000", "07/06/1995", "LLL");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("License Category Is Not Found", exception.Message);
            }

            try
            {
                // test Invalid Age
                Driver driver = new Driver("TN5619951234567", "Ravanan", "Address 2nd street", "03/05/2008", "07/06/2013", "HMV");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Invalid DOB Formate", exception.Message);
            }

            try
            {
                // test Invalid Age
                Driver driver = new Driver("TN5619951234567", "Ravanan", "Address 2nd street", "03/05/2000", "07/06/2019", "HMV");
            }
            catch (ArgumentException exception)
            {
                Assert.AreEqual("Invalid DOI", exception.Message);
            }

            try
            {
                // to check enpty list
                TrafficViolation.CreateDriver(new string[] { });
            }
            catch (IndexOutOfRangeException exception)
            {
                Assert.AreEqual("Array Index Out Of Bounce", exception.Message);
            }

            try
            {
                // to check out of boundary data
                TrafficViolation.CreateDriver(new string[] { "data", "data" });
            }
            catch (IndexOutOfRangeException exception)
            {
                Assert.AreEqual("Array Index Out Of Bounce", exception.Message);
            }

            try
            {
                // to check invalid data
                TrafficViolation.CreateDriver(new string[] { "#", "TN5619951234567", "Ravanan", "Address 2nd street", "03/05/2000", "07/06/2019", "HMV" });
            }
            catch (Exception exception)
            {
                Assert.AreEqual("Invalid DOB Formate", exception.Message);
            }
        }
        // GET: Drivers/Citation/
        public ActionResult Citation(int id)
        {
            FileStream              fsTrafficViolations = null;
            BinaryFormatter         bfTrafficViolations = new BinaryFormatter();
            List <TrafficViolation> violations          = new List <TrafficViolation>();
            string strTrafficViolationsFile             = Server.MapPath("~/App_Data/TrafficViolations.tts");

            if (System.IO.File.Exists(strTrafficViolationsFile))
            {
                using (fsTrafficViolations = new FileStream(strTrafficViolationsFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    violations = (List <TrafficViolation>)bfTrafficViolations.Deserialize(fsTrafficViolations);
                }

                TrafficViolation violation = violations.Find(viol => viol.TrafficViolationId == id);

                ViewBag.TrafficViolationNumber = violation.TrafficViolationNumber;

                ViewBag.ViolationDate  = violation.ViolationDate;
                ViewBag.ViolationTime  = violation.ViolationTime;
                ViewBag.PaymentAmount  = violation.PaymentAmount.ToString("C");
                ViewBag.PaymentDueDate = violation.PaymentDueDate;

                ViewBag.PhotoVideo = GetPhotoVideoOptions(violation.PhotoAvailable, violation.VideoAvailable);

                FileStream           fsViolationsTypes = null;
                ViolationType        category          = new ViolationType();
                BinaryFormatter      bfViolationsTypes = new BinaryFormatter();
                List <ViolationType> violationsTypes   = new List <ViolationType>();
                string strViolationsTypesFile          = Server.MapPath("~/App_Data/ViolationsTypes.tts");

                if (System.IO.File.Exists(strViolationsTypesFile))
                {
                    using (fsViolationsTypes = new FileStream(strViolationsTypesFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        violationsTypes = (List <ViolationType>)bfViolationsTypes.Deserialize(fsViolationsTypes);
                    }

                    ViolationType type = violationsTypes.Find(cat => cat.ViolationTypeId == violation.ViolationTypeId);
                    ViewBag.ViolationName        = type.ViolationName;
                    ViewBag.ViolationDescription = type.Description;
                }

                FileStream      fsVehicles = null;
                BinaryFormatter bfVehicles = new BinaryFormatter();
                List <Vehicle>  vehicles   = new List <Vehicle>();

                string strVehiclesFile = Server.MapPath("~/App_Data/Vehicles.tts");

                using (fsVehicles = new FileStream(strVehiclesFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    vehicles = (List <Vehicle>)bfVehicles.Deserialize(fsVehicles);
                }

                Vehicle vehicle = vehicles.Find(car => car.VehicleId == violation.VehicleId);

                ViewBag.Vehicle = vehicle.Make + " " + vehicle.Model + ", " +
                                  vehicle.Color + ", " + vehicle.VehicleYear + " (Tag # " +
                                  vehicle.TagNumber + ")";

                FileStream      fsDrivers      = null;
                BinaryFormatter bfDrivers      = new BinaryFormatter();
                List <Driver>   drivers        = new List <Driver>();
                string          strDriversFile = Server.MapPath("~/App_Data/Drivers.tts");

                using (fsDrivers = new FileStream(strDriversFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    drivers = (List <Driver>)bfDrivers.Deserialize(fsDrivers);
                }

                Driver person = drivers.Find(driver => driver.DriverId == violation.VehicleId);

                ViewBag.DriverName    = person.FirstName + " " + person.LastName + " (Drv. Lic. # " + person.DrvLicNumber + ")";
                ViewBag.DriverAddress = person.Address + " " + person.City + ", " + person.State + " " + person.ZIPCode;

                FileStream      fsCameras      = null;
                BinaryFormatter bfCameras      = new BinaryFormatter();
                List <Camera>   cameras        = new List <Camera>();
                string          strCamerasFile = Server.MapPath("~/App_Data/Cameras.tts");

                using (fsCameras = new FileStream(strCamerasFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    cameras = (List <Camera>)bfCameras.Deserialize(fsCameras);
                }

                Camera viewer = cameras.Find(cmr => cmr.CameraId == violation.CameraId);

                ViewBag.Camera            = viewer.Make + " " + viewer.Model + " (" + viewer.CameraNumber + ")";
                ViewBag.ViolationLocation = viewer.Location;
            }

            return(View());
        }
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                FileStream              fsTrafficViolations = null;
                TrafficViolation        violation           = new TrafficViolation();
                BinaryFormatter         bfTrafficViolations = new BinaryFormatter();
                List <TrafficViolation> violations          = new List <TrafficViolation>();
                string strTrafficViolationsFile             = Server.MapPath("~/App_Data/TrafficViolations.tts");

                if (!string.IsNullOrEmpty("TrafficViolationNumber"))
                {
                    if (System.IO.File.Exists(strTrafficViolationsFile))
                    {
                        using (fsTrafficViolations = new FileStream(strTrafficViolationsFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            violations = (List <TrafficViolation>)bfTrafficViolations.Deserialize(fsTrafficViolations);
                        }
                    }

                    violation = violations.FindLast(viol => viol.TrafficViolationId == id);

                    int             cameraId       = 0;
                    FileStream      fsCameras      = null;
                    List <Camera>   cameras        = new List <Camera>();
                    BinaryFormatter bfCameras      = new BinaryFormatter();
                    string          strCamerasFile = Server.MapPath("~/App_Data/Cameras.tts");

                    if (System.IO.File.Exists(strCamerasFile))
                    {
                        using (fsCameras = new FileStream(strCamerasFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            cameras = (List <Camera>)bfCameras.Deserialize(fsCameras);
                        }

                        foreach (Camera cmr in cameras)
                        {
                            if (cmr.CameraNumber == collection["CameraNumber"])
                            {
                                cameraId = cmr.CameraId;
                                break;
                            }
                        }
                    }

                    int             vehicleId       = 0;
                    FileStream      fsVehicles      = null;
                    List <Vehicle>  vehicles        = new List <Vehicle>();
                    BinaryFormatter bfVehicles      = new BinaryFormatter();
                    string          strVehiclesFile = Server.MapPath("~/App_Data/Vehicles.tts");

                    if (System.IO.File.Exists(strVehiclesFile))
                    {
                        using (fsVehicles = new FileStream(strVehiclesFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            vehicles = (List <Vehicle>)bfVehicles.Deserialize(fsVehicles);
                        }

                        foreach (Vehicle car in vehicles)
                        {
                            if (car.TagNumber == collection["TagNumber"])
                            {
                                vehicleId = car.VehicleId;
                                break;
                            }
                        }
                    }

                    int                  violationTypeId        = 0;
                    FileStream           fsViolationsTypes      = null;
                    BinaryFormatter      bfViolationsTypes      = new BinaryFormatter();
                    List <ViolationType> violationsTypes        = new List <ViolationType>();
                    string               strViolationsTypesFile = Server.MapPath("~/App_Data/ViolationsTypes.tts");

                    if (System.IO.File.Exists(strViolationsTypesFile))
                    {
                        using (fsViolationsTypes = new FileStream(strViolationsTypesFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            violationsTypes = (List <ViolationType>)bfViolationsTypes.Deserialize(fsViolationsTypes);
                        }

                        foreach (ViolationType vt in violationsTypes)
                        {
                            if (vt.ViolationName == collection["ViolationName"])
                            {
                                violationTypeId = vt.ViolationTypeId;
                                break;
                            }
                        }
                    }

                    violation.TrafficViolationNumber = int.Parse(collection["TrafficViolationNumber"]);
                    violation.CameraId        = cameraId;
                    violation.VehicleId       = vehicleId;
                    violation.ViolationTypeId = violationTypeId;
                    violation.ViolationDate   = collection["ViolationDate"];
                    violation.ViolationTime   = collection["ViolationTime"];
                    violation.PhotoAvailable  = collection["PhotoAvailable"];
                    violation.VideoAvailable  = collection["VideoAvailable"];
                    violation.PaymentDueDate  = collection["PaymentDueDate"];
                    violation.PaymentDate     = collection["PaymentDate"];
                    violation.PaymentAmount   = double.Parse(collection["PaymentAmount"]);
                    violation.PaymentStatus   = collection["PaymentStatus"];

                    using (fsTrafficViolations = new FileStream(strTrafficViolationsFile, FileMode.Create, FileAccess.Write, FileShare.Write))
                    {
                        bfTrafficViolations.Serialize(fsTrafficViolations, violations);
                    }
                }

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
        // GET: TrafficViolations/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            TrafficViolation        violation           = null;
            FileStream              fsTrafficViolations = null;
            BinaryFormatter         bfTrafficViolations = new BinaryFormatter();
            List <TrafficViolation> violations          = new List <TrafficViolation>();
            string strTrafficViolationsFile             = Server.MapPath("~/App_Data/TrafficViolations.tts");

            if (System.IO.File.Exists(strTrafficViolationsFile))
            {
                using (fsTrafficViolations = new FileStream(strTrafficViolationsFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    violations = (List <TrafficViolation>)bfTrafficViolations.Deserialize(fsTrafficViolations);
                }

                violation = violations.Find(viol => viol.TrafficViolationId == id);

                List <SelectListItem> itemsCameras         = new List <SelectListItem>();
                List <SelectListItem> paymentsStatus       = new List <SelectListItem>();
                List <SelectListItem> itemsPhotoAvailable  = new List <SelectListItem>();
                List <SelectListItem> itemsVideoAvailable  = new List <SelectListItem>();
                List <SelectListItem> itemsViolationsTypes = new List <SelectListItem>();

                List <Camera>   cameras = new List <Camera>();
                BinaryFormatter bfCameras = new BinaryFormatter();
                FileStream      fsCameras = null, fsViolationsTypes = null;
                string          strCamerasFile = Server.MapPath("~/App_Data/Cameras.tts");

                if (System.IO.File.Exists(strCamerasFile))
                {
                    using (fsCameras = new FileStream(strCamerasFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        cameras = (List <Camera>)bfCameras.Deserialize(fsCameras);

                        foreach (Camera cmr in cameras)
                        {
                            itemsCameras.Add(new SelectListItem()
                            {
                                Text = cmr.CameraNumber, Value = cmr.CameraNumber, Selected = (violation.CameraId == cmr.CameraId)
                            });
                        }
                    }
                }

                BinaryFormatter      bfViolationsTypes = new BinaryFormatter();
                List <ViolationType> violationsTypes   = new List <ViolationType>();
                string strViolationsTypesFile          = Server.MapPath("~/App_Data/ViolationsTypes.tts");

                if (System.IO.File.Exists(strViolationsTypesFile))
                {
                    using (fsViolationsTypes = new FileStream(strViolationsTypesFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        violationsTypes = (List <ViolationType>)bfViolationsTypes.Deserialize(fsViolationsTypes);

                        foreach (ViolationType vt in violationsTypes)
                        {
                            itemsViolationsTypes.Add(new SelectListItem()
                            {
                                Text = vt.ViolationName, Value = vt.ViolationName, Selected = (violation.ViolationTypeId == vt.ViolationTypeId)
                            });
                        }
                    }
                }

                itemsPhotoAvailable.Add(new SelectListItem()
                {
                    Value = "Unknown"
                });
                itemsPhotoAvailable.Add(new SelectListItem()
                {
                    Text = "No", Value = "No", Selected = (violation.PhotoAvailable == "No")
                });
                itemsPhotoAvailable.Add(new SelectListItem()
                {
                    Text = "Yes", Value = "Yes", Selected = (violation.PhotoAvailable == "Yes")
                });
                itemsVideoAvailable.Add(new SelectListItem()
                {
                    Value = "Unknown"
                });
                itemsVideoAvailable.Add(new SelectListItem()
                {
                    Text = "No", Value = "No", Selected = (violation.VideoAvailable == "No")
                });
                itemsVideoAvailable.Add(new SelectListItem()
                {
                    Text = "Yes", Value = "Yes", Selected = (violation.VideoAvailable == "Yes")
                });

                paymentsStatus.Add(new SelectListItem()
                {
                    Value = "Unknown"
                });
                paymentsStatus.Add(new SelectListItem()
                {
                    Text = "Pending", Value = "Pending", Selected = (violation.PaymentStatus == "Pending")
                });
                paymentsStatus.Add(new SelectListItem()
                {
                    Text = "Rejected", Value = "Rejected", Selected = (violation.PaymentStatus == "Rejected")
                });
                paymentsStatus.Add(new SelectListItem()
                {
                    Text = "Cancelled", Value = "Cancelled", Selected = (violation.PaymentStatus == "Cancelled")
                });
                paymentsStatus.Add(new SelectListItem()
                {
                    Text = "Paid Late", Value = "Paid Late", Selected = (violation.PaymentStatus == "Paid Late")
                });
                paymentsStatus.Add(new SelectListItem()
                {
                    Text = "Paid On Time", Value = "Paid On Time", Selected = (violation.PaymentStatus == "Paid On Time")
                });

                Vehicle         car             = null;
                FileStream      fsVehicles      = null;
                string          strTagNumber    = string.Empty;
                List <Vehicle>  vehicles        = new List <Vehicle>();
                BinaryFormatter bfVehicles      = new BinaryFormatter();
                string          strVehiclesFile = Server.MapPath("~/App_Data/Vehicles.tts");

                if (System.IO.File.Exists(strVehiclesFile))
                {
                    using (fsVehicles = new FileStream(strVehiclesFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        vehicles = (List <Vehicle>)bfVehicles.Deserialize(fsVehicles);
                    }

                    car = vehicles.Find(engine => engine.VehicleId == violation.VehicleId);
                }

                ViewBag.CameraNumber           = itemsCameras;
                ViewBag.PaymentStatus          = paymentsStatus;
                ViewBag.TagNumber              = car.TagNumber;
                ViewBag.PaymentDate            = violation.PaymentDate;
                ViewBag.ViolationName          = itemsViolationsTypes;
                ViewBag.PhotoAvailable         = itemsPhotoAvailable;
                ViewBag.VideoAvailable         = itemsVideoAvailable;
                ViewBag.PaymentAmount          = violation.PaymentAmount;
                ViewBag.ViolationDate          = violation.ViolationDate;
                ViewBag.ViolationTime          = violation.ViolationTime;
                ViewBag.PaymentDueDate         = violation.PaymentDueDate;
                ViewBag.TrafficViolationNumber = violation.TrafficViolationNumber;
            }

            if (violation == null)
            {
                return(HttpNotFound());
            }

            return(View(violation));
        }
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                int                     trafficViolationId = 0;
                bool                    vehicleFound = false;
                List <Camera>           cameras = new List <Camera>();
                List <Vehicle>          vehicles = new List <Vehicle>();
                BinaryFormatter         bfCameras = new BinaryFormatter();
                BinaryFormatter         bfDrivers = new BinaryFormatter();
                int                     cameraId = 0, vehicleId = 0, violationTypeId = 0;
                BinaryFormatter         bfViolationsTypes = new BinaryFormatter();
                List <ViolationType>    violationsTypes = new List <ViolationType>();
                BinaryFormatter         bfTrafficViolations = new BinaryFormatter();
                string                  strCamerasFile = Server.MapPath("~/App_Data/Cameras.tts");
                string                  strVehiclesFile = Server.MapPath("~/App_Data/Vehicles.tts");
                List <TrafficViolation> trafficViolations = new List <TrafficViolation>();
                string                  strViolationsTypesFile = Server.MapPath("~/App_Data/ViolationsTypes.tts");
                string                  strTrafficViolationsFile = Server.MapPath("~/App_Data/TrafficViolations.tts");
                FileStream              fsTrafficViolations = null, fsVehicles = null, fsCameras = null, fsViolationsTypes = null;


                // Open the file for the cameras
                if (System.IO.File.Exists(strCamerasFile))
                {
                    using (fsCameras = new FileStream(strCamerasFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        /* After opening the file, get the list of cameras from it
                         * and store it in the List<Camera> variable. */
                        cameras = (List <Camera>)bfCameras.Deserialize(fsCameras);

                        // Check each record
                        foreach (Camera cmr in cameras)
                        {
                            /* When you get to a record, find out if its CameraNumber value
                             * is the same as the camera number the user selected. */
                            if (cmr.CameraNumber == collection["CameraNumber"])
                            {
                                /* If you find a record with that tag number, make a note. */
                                cameraId = cmr.CameraId;
                                break;
                            }
                        }
                    }
                }

                // Open the file for the vehicles
                if (System.IO.File.Exists(strVehiclesFile))
                {
                    using (fsVehicles = new FileStream(strVehiclesFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        /* After opening the file, get the list of vehicles from it
                         * and store it in the List<Vehicle> variable. */
                        vehicles = (List <Vehicle>)bfDrivers.Deserialize(fsVehicles);

                        // Check each record
                        foreach (Vehicle car in vehicles)
                        {
                            /* When you get to a record, find out if its TagNumber value
                             * is the same as the tag number the user provided. */
                            if (car.TagNumber == collection["TagNumber"])
                            {
                                /* If you find a record with that tag number, make a note. */
                                vehicleId    = car.VehicleId;
                                vehicleFound = true;
                                break;
                            }
                        }
                    }
                }

                if (System.IO.File.Exists(strViolationsTypesFile))
                {
                    using (fsViolationsTypes = new FileStream(strViolationsTypesFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        violationsTypes = (List <ViolationType>)bfViolationsTypes.Deserialize(fsViolationsTypes);

                        foreach (ViolationType vt in violationsTypes)
                        {
                            if (vt.ViolationName == collection["ViolationName"])
                            {
                                violationTypeId = vt.ViolationTypeId;
                                break;
                            }
                        }
                    }
                }

                /* If the vehicleFound is true, it means the car that committed the infraction has been identified. */
                if (vehicleFound == true)
                {
                    // Check whether a file for traffic violations was created already.
                    if (System.IO.File.Exists(strTrafficViolationsFile))
                    {
                        // If a file for traffic violations exists already, open it ...
                        using (fsTrafficViolations = new FileStream(strTrafficViolationsFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            // ... and store the records in our trafficViolations variable
                            trafficViolations = (List <TrafficViolation>)bfTrafficViolations.Deserialize(fsTrafficViolations);
                        }

                        foreach (TrafficViolation tv in trafficViolations)
                        {
                            trafficViolationId = tv.TrafficViolationId;
                        }
                    }

                    trafficViolationId++;

                    TrafficViolation citation = new TrafficViolation()
                    {
                        TrafficViolationId     = trafficViolationId,
                        TrafficViolationNumber = int.Parse(collection["TrafficViolationNumber"]),
                        CameraId        = cameraId,
                        VehicleId       = vehicleId,
                        ViolationTypeId = violationTypeId,
                        ViolationDate   = collection["ViolationDate"],
                        ViolationTime   = collection["ViolationTime"],
                        PhotoAvailable  = collection["PhotoAvailable"],
                        VideoAvailable  = collection["VideoAvailable"],
                        PaymentDueDate  = collection["PaymentDueDate"],
                        PaymentDate     = collection["PaymentDate"],
                        PaymentAmount   = double.Parse(collection["PaymentAmount"]),
                        PaymentStatus   = collection["PaymentStatus"]
                    };

                    trafficViolations.Add(citation);

                    using (fsTrafficViolations = new FileStream(strTrafficViolationsFile, FileMode.Create, FileAccess.Write, FileShare.Write))
                    {
                        bfTrafficViolations.Serialize(fsTrafficViolations, trafficViolations);
                    }
                }

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }