private static void CalculateInterFillupStatistics(FillupEntry fillup, FillupEntry priorFillup)
 {
     if (priorFillup != null && fillup != null)
     {
         fillup.Distance = Math.Abs(fillup.Odometer - priorFillup.Odometer);
     }
 }
        public static IEnumerable<FillupEntry> ReadFillupEntriesFromFile(Stream stream)
        {
            using (var reader = new StreamReader(stream))
            {
                string[] fieldNames = null;
                for (;;)
                {
                    string line = reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    if (fieldNames == null)
                    {
                        fieldNames = line.Split(';');
                        continue;
                    }

                    string[] fieldValues = line.Split(';');
                    var fillupEntry = new FillupEntry {UnitOfMeasure = FillupUnits.Litres};
                    int index = 0;
                    foreach (string fieldValue in fieldValues)
                    {
                        ParseField(fillupEntry, fieldNames[index++], fieldValue);
                    }

                    yield return fillupEntry;
                }
            }
        }
示例#3
0
        public void WhenAddingFillupEntryWithAllData_ThenPersistsToTheDatabase()
        {
            var repository = new FillupRepository();

            var fillupEntry = new FillupEntry
            {
                Date           = DateTime.Now,
                Odometer       = 3000,
                PricePerUnit   = 3.24d,
                TotalUnits     = 13.01d,
                Remarks        = "Remarkable",
                TransactionFee = 1.25d,
                Vendor         = "Ideal Vendor"
            };

            repository.Create(defaultTestUser.UserId, defaultVehicle.VehicleId, fillupEntry);

            // Verification
            var repositoryForVerification = new FillupRepository();
            var enteredFillup             = repositoryForVerification.GetFillups(defaultVehicle.VehicleId).First();

            Assert.NotNull(enteredFillup);
            Assert.Equal(fillupEntry.Date.ToShortDateString(), enteredFillup.Date.ToShortDateString());
            // We only care that the dates map.
            AssertExtensions.PropertiesAreEqual(fillupEntry, enteredFillup, "Odometer", "PricePerUnit", "TotalUnits",
                                                "Remarks", "TransactionFee", "Vendor");
        }
        void CreateReminders(Vehicle vehicle)
        {
            FillupEntry lastFillup = vehicle.Fillups.OrderByDescending(f => f.Date).FirstOrDefault();

            if (lastFillup == null)
            {
                return;
            }

            Reminder reminder;

            // create overdue by mileage reminder
            reminder = new Reminder {
                DueDate = null, DueDistance = lastFillup.Odometer - 10, IsFulfilled = false, Remarks = "Check air filter when oil is changed", Title = "Oil Change", VehicleId = vehicle.VehicleId
            };
            vehicle.Reminders.Add(reminder);

            // create overdue by date reminder
            reminder = new Reminder {
                DueDate = lastFillup.Date.AddDays(-10), DueDistance = null, IsFulfilled = false, Remarks = "Check condition of the wipers", Title = "Check Wiper Fluid", VehicleId = vehicle.VehicleId
            };
            vehicle.Reminders.Add(reminder);

            // create to be done soon by mileage reminder
            reminder = new Reminder {
                DueDate = null, DueDistance = lastFillup.Odometer + 400, IsFulfilled = false, Remarks = "Check air pressure", Title = "Rotate Tires", VehicleId = vehicle.VehicleId
            };
            vehicle.Reminders.Add(reminder);

            // create to be done soon by date reminder
            reminder = new Reminder {
                DueDate = DateTime.Now.AddDays(+10), DueDistance = null, IsFulfilled = false, Remarks = "Check air freshener", Title = "Vacuum Car", VehicleId = vehicle.VehicleId
            };
            vehicle.Reminders.Add(reminder);
        }
 /// <summary>
 /// Creates the fillups.
 /// </summary>
 /// <param name="odometer">The initial odometer reading</param>
 /// <param name="date">The first date to create a fill up for</param>
 /// <param name="vehicle">The vehicle object to create the fill ups for</param>
 /// <param name="unitsModifier">The units modifier is applied to the total gallons calculation.
 ///   By supplying a different value for each vehicle, the data will be different for each vehicle.
 /// </param>
 /// <param name="distanceModifier">The distance modifier is applied to the distance calculation.
 ///   By supplying a different value for each vehicle, the data will be different for each vehicle.
 /// </param>
 /// <remarks>
 /// Creates random fill up sample data for the vehicle. 
 /// Consumes the data arrays at the top of this class.
 /// Randomizes the index used to access data arrays by creating an array then randomly sorting the array elements.
 /// The "while" loop runs while calculated date is less than the current date.
 /// The date is recalculated each cycle of the while loop, adding a random number of days between 3 and 18 days to the previous value.
 /// </remarks>
 void CreateFillups(Int32 odometer, DateTime date, Vehicle vehicle, Double unitsModifier, Double distanceModifier)
 {
     var randomArray = RandomizeArray( new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
     var currentIndex = 0;
     var random = new Random();
     var isFirst = true;
     while(date < DateTime.Now) {
         var dataIndex = randomArray[currentIndex];
         var distance = (Int32)(_distance[dataIndex] * distanceModifier);
         var fillup = new FillupEntry();
         fillup.Date = date;
         if(isFirst) {
             isFirst = false;
             fillup.Distance = null;
         } else {
             fillup.Distance = distance;
         }
         fillup.Odometer = odometer;
         fillup.PricePerUnit = _price[dataIndex];
         fillup.TotalUnits = _units[dataIndex] * unitsModifier;
         fillup.TransactionFee = _fee[dataIndex];
         fillup.VehicleId = vehicle.VehicleId;
         fillup.Vendor = _vendor[dataIndex];
         odometer += distance;
         vehicle.Fillups.Add(fillup);
         currentIndex += 1;
         if(currentIndex > 11) {
             currentIndex = 0;
         }
         date = date.AddDays(random.Next(3, 14));
     }
     SaveChanges();
 }
 private static void CalculateInterFillupStatistics(FillupEntry fillup, FillupEntry priorFillup)
 {
     if (priorFillup != null && fillup != null)
     {
         fillup.Distance = Math.Abs(fillup.Odometer - priorFillup.Odometer);
     }
 }
示例#7
0
        public void WhenGettingAllFillups_ThenReturnsAllFillups()
        {
            var repository = new FillupRepository();

            var fillupEntry1 = new FillupEntry
            {
                Date         = DateTime.Now,
                Odometer     = 3000,
                PricePerUnit = 3.24d,
                TotalUnits   = 13.01d,
            };

            repository.Create(defaultTestUser.UserId, defaultVehicle.VehicleId, fillupEntry1);

            var fillupEntry2 = new FillupEntry
            {
                Date         = DateTime.Now,
                Odometer     = 3001,
                PricePerUnit = 3.24d,
                TotalUnits   = 13.01d,
            };

            repository.Create(defaultTestUser.UserId, defaultVehicle.VehicleId, fillupEntry2);


            var fillups = repository.GetFillups(defaultVehicle.VehicleId);

            Assert.NotNull(fillups);
            Assert.Equal(2, fillups.Count());
        }
示例#8
0
        public void WhenAddingFirstFillup_ThenDoesNotCalculatesDistance()
        {
            _vehicleRepo
            .Setup(r => r.GetVehicle(DefaultUserId, DefaultVehicleId))
            .Returns(_vehicle);

            var fillups = new FillupEntry[] {};

            var fillupForm = new FillupEntryFormModel
            {
                FillupEntryId  = 3,
                Date           = new DateTime(2011, 3, 25),
                Odometer       = 1000,
                PricePerUnit   = 0,
                TotalUnits     = 0,
                TransactionFee = 0
            };

            _fillupRepositoryMock
            .Setup(x => x.GetFillups(DefaultVehicleId))
            .Returns(fillups);

            var handler = new AddFillupToVehicle(_vehicleRepo.Object, _fillupRepositoryMock.Object);

            handler.Execute(DefaultUserId, DefaultVehicleId, fillupForm);

            Assert.Null(fillupForm.Distance);
        }
        public void WhenAddingFillupEntryWithAllData_ThenPersistsToTheDatabase()
        {
            var repository = new FillupRepository(this.GetUnitOfWork());

            var fillupEntry = new FillupEntry()
                                  {
                                      Date = DateTime.Now,
                                      Odometer = 3000,
                                      PricePerUnit = 3.24d,
                                      TotalUnits = 13.01d,
                                      Remarks = "Remarkable",
                                      TransactionFee = 1.25d,
                                      Vendor = "Ideal Vendor"
                                  };

            repository.Create(this.defaultTestUser.UserId, this.defaultVehicle.VehicleId, fillupEntry);

            // Verification
            var repositoryForVerification = new FillupRepository(this.GetUnitOfWork());
            var enteredFillup = repositoryForVerification.GetFillups(this.defaultVehicle.VehicleId).First();
            Assert.NotNull(enteredFillup);
            Assert.Equal(fillupEntry.Date.ToShortDateString(), enteredFillup.Date.ToShortDateString());
            // We only care that the dates map.
            AssertExtensions.PropertiesAreEqual(fillupEntry, enteredFillup, "Odometer", "PricePerUnit", "TotalUnits",
                                                "Remarks", "TransactionFee", "Vendor");
        }
示例#10
0
 public FillupViewModel(FillupEntry entry)
 {
     FillupEntryId  = entry.FillupEntryId;
     Date           = String.Format("{0:MMMM d, yyyy}", entry.Date);
     DateShort      = String.Format("{0:MM/dd/yyyy}", entry.Date);
     TotalUnits     = String.Format("{0:#00.000}", entry.TotalUnits);
     Odometer       = String.Format("{0:N0}", entry.Odometer);
     TransactionFee = String.Format("{0:C}", entry.TransactionFee);
     PricePerUnit   = String.Format("{0:C}", entry.PricePerUnit);
     Remarks        = entry.Remarks;
     Vendor         = entry.Vendor;
     TotalCost      = String.Format("{0:C}", entry.TotalCost);
 }
示例#11
0
        public void WhenAddingFillupEntry_ThenFillupAssignedNewId()
        {
            var repository = new FillupRepository();

            var fillupEntry = new FillupEntry
            {
                Date         = DateTime.Now,
                Odometer     = 3000,
                PricePerUnit = 3.24d,
                TotalUnits   = 13.01d,
            };

            repository.Create(defaultTestUser.UserId, defaultVehicle.VehicleId, fillupEntry);

            Assert.NotEqual(0, fillupEntry.FillupEntryId);
        }
示例#12
0
        private void CreateFillups(Vehicle vehicle, FillupData data)
        {
            int      odometer         = data.Mileage;
            DateTime date             = data.Date;
            Double   unitsModifier    = data.Units;
            Double   distanceModifier = data.Distance;

            int[] randomArray  = RandomizeArray(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
            int   currentIndex = 0;
            var   random       = new Random();
            bool  isFirst      = true;

            while (date < DateTime.Now)
            {
                int dataIndex = randomArray[currentIndex];
                var distance  = (int)(_distance[dataIndex] * distanceModifier);
                var fillup    = new FillupEntry
                {
                    Date           = date,
                    Odometer       = odometer,
                    PricePerUnit   = _price[dataIndex],
                    TotalUnits     = _units[dataIndex] * unitsModifier,
                    TransactionFee = _fee[dataIndex],
                    VehicleId      = vehicle.VehicleId,
                    Vendor         = _vendor[dataIndex]
                };
                if (isFirst)
                {
                    isFirst         = false;
                    fillup.Distance = null;
                }
                else
                {
                    fillup.Distance = distance;
                }
                odometer += distance;

                currentIndex += 1;
                if (currentIndex > 11)
                {
                    currentIndex = 0;
                }
                date = date.AddDays(random.Next(3, 14));
                _fillups.Create(vehicle.UserId, vehicle.VehicleId, fillup);
            }
        }
 private static void ParseField(FillupEntry fillupEntry, string fieldName, string fieldValue)
 {
     switch (fieldName)
     {
         case "Date":
             fillupEntry.Date = ParseDate(fieldValue);
             break;
         case "Odometer":
             var odometer = (int) ParseDouble(fieldValue);
             fillupEntry.Odometer = odometer;
             break;
         case "Trip":
         {
             var distance = (int) ParseDouble(fieldValue);
             if (distance != 0)
             {
                 fillupEntry.Distance = distance;
             }
             break;
         }
         case "Quantity":
             fillupEntry.TotalUnits = ParseDouble(fieldValue);
             break;
         case "Total price":
             // This is a calculated field in FillupEntry, so we'll just update PricePerUnit
             fillupEntry.PricePerUnit = ParseDouble(fieldValue)/fillupEntry.TotalUnits;
             break;
         case "Currency":
             break;
         case "Type":
             break;
         case "Tires":
             break;
         case "Roads":
             break;
         case "Driving style":
             break;
         case "Fuel":
             break;
         case "Note":
             fillupEntry.Remarks = fieldValue.Trim(new[] {'\"'});
             break;
         case "Consumption":
             break;
     }
 }
        /// <summary>
        /// Creates the fillups.
        /// </summary>
        /// <param name="odometer">The initial odometer reading</param>
        /// <param name="date">The first date to create a fill up for</param>
        /// <param name="vehicle">The vehicle object to create the fill ups for</param>
        /// <param name="unitsModifier">The units modifier is applied to the total gallons calculation.
        ///   By supplying a different value for each vehicle, the data will be different for each vehicle.
        /// </param>
        /// <param name="distanceModifier">The distance modifier is applied to the distance calculation.
        ///   By supplying a different value for each vehicle, the data will be different for each vehicle.
        /// </param>
        /// <remarks>
        /// Creates random fill up sample data for the vehicle.
        /// Consumes the data arrays at the top of this class.
        /// Randomizes the index used to access data arrays by creating an array then randomly sorting the array elements.
        /// The "while" loop runs while calculated date is less than the current date.
        /// The date is recalculated each cycle of the while loop, adding a random number of days between 3 and 18 days to the previous value.
        /// </remarks>
        void CreateFillups(Int32 odometer, DateTime date, Vehicle vehicle, Double unitsModifier, Double distanceModifier)
        {
            var randomArray  = RandomizeArray(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 });
            var currentIndex = 0;
            var random       = new Random();
            var isFirst      = true;

            while (date < DateTime.Now)
            {
                var dataIndex = randomArray[currentIndex];
                var distance  = (Int32)(_distance[dataIndex] * distanceModifier);
                var fillup    = new FillupEntry();
                fillup.Date = date;
                if (isFirst)
                {
                    isFirst         = false;
                    fillup.Distance = null;
                }
                else
                {
                    fillup.Distance = distance;
                }
                fillup.Odometer       = odometer;
                fillup.PricePerUnit   = _price[dataIndex];
                fillup.TotalUnits     = _units[dataIndex] * unitsModifier;
                fillup.TransactionFee = _fee[dataIndex];
                fillup.VehicleId      = vehicle.VehicleId;
                fillup.Vendor         = _vendor[dataIndex];
                odometer += distance;
                vehicle.Fillups.Add(fillup);
                currentIndex += 1;
                if (currentIndex > 11)
                {
                    currentIndex = 0;
                }
                date = date.AddDays(random.Next(3, 14));
            }
            SaveChanges();
        }
        public void WhenAddingMinimalFillupEntry_ThenPersistsToTheDatabase()
        {
            var repository = new FillupRepository(this.GetUnitOfWork());

            var fillupEntry = new FillupEntry()
                                  {
                                      Date = DateTime.Now,
                                      Odometer = 3000,
                                      PricePerUnit = 3.24d,
                                      TotalUnits = 13.01d,
                                  };

            repository.Create(this.defaultTestUser.UserId, this.defaultVehicle.VehicleId, fillupEntry);

            // Verification
            var repositoryForVerification = new FillupRepository(this.GetUnitOfWork());
            var retrievedFillup = repositoryForVerification.GetFillups(this.defaultVehicle.VehicleId).First();

            Assert.NotNull(retrievedFillup);
            Assert.Equal(fillupEntry.Date.ToShortDateString(), retrievedFillup.Date.ToShortDateString());
            // We only care that the dates map.
            AssertExtensions.PropertiesAreEqual(fillupEntry, retrievedFillup, "Odometer", "PricePerUnit", "TotalUnits");
        }
示例#16
0
        public void WhenAddingMinimalFillupEntry_ThenPersistsToTheDatabase()
        {
            var repository = new FillupRepository();

            var fillupEntry = new FillupEntry
            {
                Date         = DateTime.Now,
                Odometer     = 3000,
                PricePerUnit = 3.24d,
                TotalUnits   = 13.01d,
            };

            repository.Create(defaultTestUser.UserId, defaultVehicle.VehicleId, fillupEntry);

            // Verification
            var repositoryForVerification = new FillupRepository();
            var retrievedFillup           = repositoryForVerification.GetFillups(defaultVehicle.VehicleId).First();

            Assert.NotNull(retrievedFillup);
            Assert.Equal(fillupEntry.Date.ToShortDateString(), retrievedFillup.Date.ToShortDateString());
            // We only care that the dates map.
            AssertExtensions.PropertiesAreEqual(fillupEntry, retrievedFillup, "Odometer", "PricePerUnit", "TotalUnits");
        }
示例#17
0
        private void AdjustSurroundingFillupEntries(FillupEntry newFillup)
        {
            if (newFillup == null)
            {
                throw new ArgumentNullException("newFillup");
            }

            var fillups = _fillupRepository.GetFillups(newFillup.VehicleId);

            // Prior fillups are ordered descending so that FirstOrDefault() chooses the one closest to the new fillup.
            // Secondary ordering is by entry ID ensure a consistent ordering/
            var priorFillup = fillups
                              .OrderByDescending(f => f.Date).ThenByDescending(f => f.FillupEntryId)
                              .FirstOrDefault(f => (f.Date <= newFillup.Date) && (f.FillupEntryId != newFillup.FillupEntryId));

            // Prior fillups are ordered ascending that FirstOrDefault() chooses the one closest to the new fillup.
            // Secondary ordering is by entry ID ensure a consistent ordering.
            var nextFillup = fillups
                             .OrderBy(f => f.Date).ThenBy(f => f.FillupEntryId)
                             .FirstOrDefault(f => (f.Date >= newFillup.Date) && (f.FillupEntryId != newFillup.FillupEntryId));

            CalculateInterFillupStatistics(newFillup, priorFillup);
            CalculateInterFillupStatistics(nextFillup, newFillup);
        }
示例#18
0
        private static FillupEntry ToEntry(ICreateFillupEntryCommand source)
        {
            if (source == null)
            {
                return(null);
            }

            var fillup = new FillupEntry
            {
                FillupEntryId  = source.FillupEntryId,
                Date           = source.Date.Value,
                Distance       = source.Distance,
                Odometer       = source.Odometer,
                PricePerUnit   = source.PricePerUnit.Value,
                Remarks        = source.Remarks,
                TotalUnits     = source.TotalUnits.Value,
                TransactionFee = source.TransactionFee.Value,
                VehicleId      = source.VehicleId,
                Vendor         = source.Vendor ?? source.Location,
                UnitOfMeasure  = source.UnitOfMeasure
            };

            return(fillup);
        }
示例#19
0
        private static FillupEntry ToEntry(ICreateFillupEntryCommand source)
        {
            if (source == null)
            {
                return null;
            }

            var fillup = new FillupEntry
                             {
                                 FillupEntryId = source.FillupEntryId,
                                 Date = source.Date,
                                 Distance = source.Distance,
                                 Odometer = source.Odometer,
                                 PricePerUnit = source.PricePerUnit,
                                 Remarks = source.Remarks,
                                 TotalUnits = source.TotalUnits,
                                 TransactionFee = source.TransactionFee,
                                 VehicleId = source.VehicleId,
                                 Vendor = source.Vendor,
                                 UnitOfMeasure = source.UnitOfMeasure
                             };
            return fillup;
        }
 public void Create(int userId, int vehicleId, FillupEntry fillup)
 {
     fillup.VehicleId = vehicleId;
     this.GetDbSet <FillupEntry>().Add(fillup);
     this.UnitOfWork.SaveChanges();
 }
 public void Create(int userId, int vehicleId, FillupEntry fillup)
 {
     fillup.VehicleId = vehicleId;
     this.GetDbSet<FillupEntry>().Add(fillup);
     this.UnitOfWork.SaveChanges();
 }
        public void WhenAddingFillupEntry_ThenFillupAssignedNewId()
        {
            var repository = new FillupRepository(this.GetUnitOfWork());

            var fillupEntry = new FillupEntry()
                                  {
                                      Date = DateTime.Now,
                                      Odometer = 3000,
                                      PricePerUnit = 3.24d,
                                      TotalUnits = 13.01d,
                                  };

            repository.Create(this.defaultTestUser.UserId, this.defaultVehicle.VehicleId, fillupEntry);

            Assert.NotEqual(0, fillupEntry.FillupEntryId);
        }
示例#23
0
        private void AdjustSurroundingFillupEntries(FillupEntry newFillup)
        {
            if (newFillup == null) throw new ArgumentNullException("newFillup");

            IEnumerable<FillupEntry> fillups = _fillupRepository.GetFillups(newFillup.VehicleId);

            // Prior fillups are ordered descending so that FirstOrDefault() chooses the one closest to the new fillup.
            // Secondary ordering is by entry ID ensure a consistent ordering/
            FillupEntry priorFillup = fillups
                .OrderByDescending(f => f.Date).ThenByDescending(f => f.FillupEntryId)
                .Where(f => (f.Date <= newFillup.Date) && (f.FillupEntryId != newFillup.FillupEntryId)).FirstOrDefault();

            // Prior fillups are ordered ascending that FirstOrDefault() chooses the one closest to the new fillup.
            // Secondary ordering is by entry ID ensure a consistent ordering.
            FillupEntry nextFillup = fillups
                .OrderBy(f => f.Date).ThenBy(f => f.FillupEntryId)
                .Where(f => (f.Date >= newFillup.Date) && (f.FillupEntryId != newFillup.FillupEntryId)).FirstOrDefault();

            CalculateInterFillupStatistics(newFillup, priorFillup);
            CalculateInterFillupStatistics(nextFillup, newFillup);
        }
        public void WhenGettingAllFillups_ThenReturnsAllFillups()
        {
            var repository = new FillupRepository(this.GetUnitOfWork());

            var fillupEntry1 = new FillupEntry()
                                   {
                                       Date = DateTime.Now,
                                       Odometer = 3000,
                                       PricePerUnit = 3.24d,
                                       TotalUnits = 13.01d,
                                   };
            repository.Create(this.defaultTestUser.UserId, this.defaultVehicle.VehicleId, fillupEntry1);

            var fillupEntry2 = new FillupEntry()
                                   {
                                       Date = DateTime.Now,
                                       Odometer = 3001,
                                       PricePerUnit = 3.24d,
                                       TotalUnits = 13.01d,
                                   };
            repository.Create(this.defaultTestUser.UserId, this.defaultVehicle.VehicleId, fillupEntry2);

            var fillups = repository.GetFillups(this.defaultVehicle.VehicleId);

            Assert.NotNull(fillups);
            Assert.Equal(2, fillups.Count());
        }