예제 #1
0
        private bool UpdateOrAddCostPerDay(DriveBy driveBy)
        {
            VehicleQueries vehicleQueries = new VehicleQueries();
            bool           result         = false;

            result = vehicleQueries.UpdateOrAddCostPerDayByVehicleId(driveBy);

            return(result);
        }
예제 #2
0
        private bool AddDriveBy(DriveBy driveBy)
        {
            VehicleQueries vehicleQueries = new VehicleQueries();
            bool           result         = false;

            result = vehicleQueries.AddCurrentDriveBy(driveBy);

            return(result);
        }
예제 #3
0
        public bool UpdateOrAddCostPerDayByVehicleId(DriveBy driveBy)
        {
            bool success = false;

            try
            {
                // Establish DB connection
                VehicleDB db = new VehicleDB(connectionString);
                db.Connection.Open();

                //Query. Check if there is any post for the date.
                var queryResult =
                    (from v in db.CostPerDays
                     where v.VehicleId == driveBy.VehicleId && v.Date.Date == driveBy.PassedAt.Date
                     select v).SingleOrDefault();

                //Handle result
                if (queryResult == null)
                {
                    //Add.
                    CostPerDays costPerdays = new CostPerDays
                    {
                        VehicleId   = driveBy.VehicleId,
                        Date        = driveBy.PassedAt,
                        CostThisDay = driveBy.PassageCost
                    };

                    db.CostPerDays.InsertOnSubmit(costPerdays);

                    db.SubmitChanges();

                    db.Connection.Close();

                    success = true;
                }
                else
                {
                    //Update.
                    int newCostForTheDay = queryResult.CostThisDay + driveBy.PassageCost;
                    queryResult.CostThisDay = newCostForTheDay;

                    db.SubmitChanges();

                    db.Connection.Close();

                    success = true;
                }
            }
            catch (Exception ex)
            {
                success = false;
            }

            return(success);
        }
예제 #4
0
        public List <DriveBy> GetDriveBysForTheLastHourByVehicleId(int vehichleId, DateTime vehiclePassedAt)
        {
            List <DriveBy> driveBysList = new List <DriveBy>();

            try
            {
                DateTime date60MinutesBackInTime = vehiclePassedAt.AddMinutes(-60);

                // Establish DB connection
                VehicleDB db = new VehicleDB(connectionString);
                db.Connection.Open();

                //Query
                var queryResult =
                    from v in db.DriveBys
                    where v.VehicleId == vehichleId && v.PassedAt >= date60MinutesBackInTime
                    select v;

                //Close DB Connection
                db.Connection.Close();

                //Handle result
                if (queryResult == null || queryResult.Count() == 0)
                {
                    //Nothing was found.
                    return(driveBysList = null);
                }

                foreach (var item in queryResult)
                {
                    DriveBy driveBy = new DriveBy();

                    driveBy.Id          = item.Id;
                    driveBy.VehicleId   = item.VehicleId;
                    driveBy.PassedAt    = item.PassedAt;
                    driveBy.PassageCost = item.PassageCost;

                    driveBysList.Add(driveBy);
                }
            }
            catch (Exception ex)
            {
                driveBysList = null;
            }

            return(driveBysList);
        }
예제 #5
0
        public bool AddCurrentDriveBy(DriveBy driveBy)
        {
            bool driveByAdded = false;

            try
            {
                // Establish DB connection
                VehicleDB db = new VehicleDB(connectionString);
                db.Connection.Open();

                //Query
                DriveBys driveBys = new DriveBys
                {
                    VehicleId   = driveBy.VehicleId,
                    PassageCost = driveBy.PassageCost,
                    PassedAt    = driveBy.PassedAt
                };

                // Add the new object.
                db.DriveBys.InsertOnSubmit(driveBys);

                // Submit the change to the database.
                db.SubmitChanges();

                //Close DB Connection
                db.Connection.Close();

                driveByAdded = true;
            }
            catch (Exception ex)
            {
                driveByAdded = false;
            }

            return(driveByAdded);
        }
예제 #6
0
        public string ReturnTotalTollFeeForToday(string typedRegistrationNumber)
        {
            DateTime vehiclePassedAt = DateTime.Now;

            //Try to get vehicle from database
            Vehicle vehicle = new Vehicle();

            vehicle = GetVehicle(typedRegistrationNumber.ToUpper());

            if (vehicle != null)
            {
                //In case the vehicle is a toll free vehicle, return a message saying so.
                //(And the only vehicle that is not a toll free vehicle is the type is the: CAR.)
                if (vehicle.VehicleType != "Car")
                {
                    return("The vehicle with registration number " + vehicle.RegistrationNumber +
                           " is a " + vehicle.VehicleType + " vehicle and it is a toll free vehicle.");
                }


                //Is it weekend or a holiday today? If yes, Yay! NO TOLL FEE!!
                if (DateSystem.IsWeekend(vehiclePassedAt, CountryCode.SE) | DateSystem.IsPublicHoliday(vehiclePassedAt, CountryCode.SE))
                {
                    return("It's weekend or holiday today. So " + vehicle.RegistrationNumber + " does not have to pay any toll fee today!");
                }


                //Has the CAR passed at least once in the last 60 min period?
                //Then the passage with the highest price is the only one that counts.
                List <DriveBy> lastHoursDriveBys = new List <DriveBy>();
                lastHoursDriveBys = GetDriveBys1HourBack(vehicle.Id, vehiclePassedAt);

                //Get current Toll fee
                int tollFeeForCurrentDriveBy = GetTollFee(vehiclePassedAt);

                //Mapp driveBy
                DriveBy curentDriveBy = new DriveBy();
                curentDriveBy.PassageCost = tollFeeForCurrentDriveBy;
                curentDriveBy.PassedAt    = vehiclePassedAt;
                curentDriveBy.VehicleId   = vehicle.Id;

                if (lastHoursDriveBys != null)
                {
                    //Check if the latest passage is the more expensive than the other ones.
                    bool       currentTollFeeIsMoreExpensive = false;
                    List <int> tollFeeCostsTheLastHour       = new List <int>();
                    for (int i = 0; i < lastHoursDriveBys.Count(); i++)
                    {
                        tollFeeCostsTheLastHour.Add(lastHoursDriveBys[i].PassageCost);
                    }
                    if (tollFeeForCurrentDriveBy > tollFeeCostsTheLastHour.Max())
                    {
                        currentTollFeeIsMoreExpensive = true;
                    }

                    if (currentTollFeeIsMoreExpensive)
                    {
                        //If yes, okay, get the diffrence and add that difference to the CostPerDay to update the total toll fee for the day.
                        int costDiff = tollFeeForCurrentDriveBy - tollFeeCostsTheLastHour.Max();

                        //Add current passing to DriveBys table
                        bool driveByAdded = false;
                        curentDriveBy.PassageCost = costDiff;
                        driveByAdded = AddDriveBy(curentDriveBy);

                        //Update the CostPerDays table with the diff
                        bool costPerDayWasAddedOrUpdated = false;
                        costPerDayWasAddedOrUpdated = UpdateOrAddCostPerDay(curentDriveBy);

                        if (driveByAdded && costPerDayWasAddedOrUpdated)
                        {
                            CostPerDay cost = new CostPerDay();
                            cost = GetTodaysTotalCost(vehicle.Id, curentDriveBy.PassedAt);
                            if (cost == null)
                            {
                                return("Could not get the total cost for today but the current toll fee replaced the previous highest toll fee for the past hour for " + vehicle.RegistrationNumber + ".");
                            }
                            else
                            {
                                return("Current Toll fee (" + tollFeeForCurrentDriveBy.ToString() + " kr) replaced the previous highest toll fee for the past hour for " + vehicle.RegistrationNumber + ".  Now the total cost for " + cost.Date.ToShortDateString() + " is " + cost.CostThisDay + " kr.");
                            }
                        }
                        else
                        {
                            return("Could not Add the current driveby and/or could not Add/Update the total cost for today with the current toll fee for: " + vehicle.RegistrationNumber + ".");
                        }
                    }
                    else
                    {
                        //If NO, good. No need to add anything to the CostPerDays table. Only add to the DriveBys table.

                        //Add current passaing to DriveBys table
                        bool driveByAdded = false;
                        driveByAdded = AddDriveBy(curentDriveBy);

                        if (driveByAdded)
                        {
                            CostPerDay cost = new CostPerDay();
                            cost = GetTodaysTotalCost(vehicle.Id, curentDriveBy.PassedAt);
                            if (cost == null)
                            {
                                return("Could not get the total cost for today. Current toll fee did not need to be added to the DB for: " + vehicle.RegistrationNumber + ".");
                            }
                            else
                            {
                                return("Toll fee (" + curentDriveBy.PassageCost.ToString() + " kr) did not need to be added for " + vehicle.RegistrationNumber + ". Total cost for " + cost.Date.ToShortDateString() + " is " + cost.CostThisDay + " kr.");
                            }
                        }
                        else
                        {
                            return("Someting went wrong when adding DriveBy to the DB when one or more already exist in the table for " + vehicle.RegistrationNumber + ".");
                        }
                    }
                }
                else
                {
                    // No Drivebys the last hour for this car. Just add the latest passage to the  CostPerDay & DriveBys  tables.
                    bool driveByAdded = false;
                    driveByAdded = AddDriveBy(curentDriveBy);

                    if (driveByAdded)
                    {
                        //Update or add total cost for the day.
                        bool costPerDayWasAddedOrUpdated = false;
                        costPerDayWasAddedOrUpdated = UpdateOrAddCostPerDay(curentDriveBy);

                        if (costPerDayWasAddedOrUpdated)
                        {
                            CostPerDay cost = new CostPerDay();
                            cost = GetTodaysTotalCost(vehicle.Id, curentDriveBy.PassedAt);
                            if (cost == null)
                            {
                                return("Could not get the total cost for today but the current toll fee was added for " + vehicle.RegistrationNumber + ".");
                            }
                            else
                            {
                                return("Toll fee (" + curentDriveBy.PassageCost.ToString() + " kr) was added for " + vehicle.RegistrationNumber + ". Total cost for " + cost.Date.ToShortDateString() + " is " + cost.CostThisDay + " kr.");
                            }
                        }
                        else
                        {
                            return("Could not Add or Update the total cost for the day with the current toll fee for: " + vehicle.RegistrationNumber + ".");
                        }
                    }
                    else
                    {
                        //Something went wrong
                        return("Something went wrong when trying to add the passage to the DB for " + vehicle.RegistrationNumber + ".");
                    }
                }
            }

            //Vehicle not found.
            return("Vehicle not found. Try a different registration number.");
        }