예제 #1
0
        /// <summary>
        /// This method just adds a new Package object to the context and syncs it to the db.
        /// </summary>
        private void buttonAddDelivery_Click(object sender, EventArgs e)
        {
            BackgroundActivity backgroundAct = new BackgroundActivity();

            try
            {
                context.Packages.Add(new Package
                {
                    CustomerName    = textBoxCustomerName.Text,
                    Address         = richTextBoxAddress.Text,
                    AreaId          = listBoxAreaNames.SelectedIndex + 1,
                    Weight          = decimal.Parse(textBoxWeight.Text),
                    DeliveryDate    = dateTimePickerDeliveryDate.Value,
                    StatusDelivered = false
                                      //by default a new package is added as not delivered
                });
                //updating the driver earnings, areaId is the same as driverId
                //our drivers get $10 per delivery
                backgroundAct.UpdateDriverEarnings(listBoxAreaNames.SelectedIndex + 1);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please check input data and try again!");
            }
            context.SaveChanges();
            MessageBox.Show("Delivery added successfully!");
            this.Close();
        }
예제 #2
0
        /// <summary>
        /// This method clears all tables in the db and then adds fresh data
        /// from the given csv files, to bring back the db to initial state.
        /// </summary>
        private void ReloadData()
        {
            // reset ident for all tables
            context.Database.ExecuteSqlCommand($"DBCC CHECKIDENT('Areas', RESEED, 0)");
            context.Database.ExecuteSqlCommand($"DBCC CHECKIDENT('Drivers', RESEED, 0)");
            context.Database.ExecuteSqlCommand($"DBCC CHECKIDENT('Packages', RESEED, 0)");
            context.Database.ExecuteSqlCommand($"DBCC CHECKIDENT('Trucks', RESEED, 0)");

            //clearing data in the tables
            context.Areas.RemoveRange(context.Areas);
            context.Drivers.RemoveRange(context.Drivers);
            context.Packages.RemoveRange(context.Packages);
            context.Trucks.RemoveRange(context.Trucks);
            context.SaveChanges();

            //creating blank lists for data
            List <Area>    areaList    = new List <Area>();
            List <Driver>  driverList  = new List <Driver>();
            List <Package> packageList = new List <Package>();
            List <Truck>   truckList   = new List <Truck>();

            String             tempLine;
            BackgroundActivity backgroundAct = new BackgroundActivity();

            //opening 'Data_Areas.csv' to populate areaList
            try
            {
                StreamReader areaFile = File.OpenText("Data_Areas.csv");
                while (!areaFile.EndOfStream)
                {
                    tempLine = areaFile.ReadLine();
                    areaList.Add(new Area
                    {
                        AreaName = tempLine
                    });
                }
                areaFile.Close();
            }
            catch (FileNotFoundException ex)
            {
                MessageBox.Show("Error: Data_Areas.csv doesn't exist, contact DB admin.");
            }
            context.Areas.AddRange(areaList);
            context.SaveChanges();

            //opening 'Data_Drivers.csv' to populate driverList
            try
            {
                StreamReader driverFile = File.OpenText("Data_Drivers.csv");
                while (!driverFile.EndOfStream)
                {
                    tempLine = driverFile.ReadLine();
                    string[] temp = tempLine.Split(',');
                    driverList.Add(new Driver
                    {
                        DriverName    = temp[0],
                        TotalEarnings = decimal.Parse(temp[1])
                    });
                }
                driverFile.Close();
            }
            catch (FileNotFoundException ex)
            {
                MessageBox.Show("Error: Data_Drivers.csv doesn't exist, contact DB admin.");
            }
            context.Drivers.AddRange(driverList);
            context.SaveChanges();

            //opening 'Data_Trucks.csv' to populate truckList
            try
            {
                StreamReader truckFile = File.OpenText("Data_Trucks.csv");
                while (!truckFile.EndOfStream)
                {
                    tempLine = truckFile.ReadLine();
                    string[] temp = tempLine.Split(',');
                    truckList.Add(new Truck
                    {
                        MaxLoad     = decimal.Parse(temp[0]),
                        CurrentLoad = decimal.Parse(temp[1]),
                        AreaId      = int.Parse(temp[2]),
                        DriverId    = int.Parse(temp[3])
                    });
                }
                truckFile.Close();
            }
            catch (FileNotFoundException ex)
            {
                MessageBox.Show("Error: Data_Trucks.csv doesn't exist, contact DB admin.");
            }
            context.Trucks.AddRange(truckList);
            context.SaveChanges();

            //opening 'Data_Packages.csv' to populate packageList
            try
            {
                StreamReader packageFile = File.OpenText("Data_Packages.csv");
                while (!packageFile.EndOfStream)
                {
                    tempLine = packageFile.ReadLine();
                    string[] temp = tempLine.Split(',');

                    //checking delivery status by date
                    bool packageDelivered = false;
                    if (DateTime.Parse(temp[4]).CompareTo(DateTime.Now) <= 0)
                    {
                        packageDelivered = true;
                    }

                    packageList.Add(new Package
                    {
                        CustomerName    = temp[0],
                        Address         = temp[1],
                        AreaId          = int.Parse(temp[2]),
                        Weight          = decimal.Parse(temp[3]),
                        DeliveryDate    = DateTime.Parse(temp[4]),
                        StatusDelivered = packageDelivered
                    });
                    //updating the driver earnings, initially the areaId is the same as driverId
                    //our drivers get $10 per delivery
                    backgroundAct.UpdateDriverEarnings(int.Parse(temp[2]));
                }
                packageFile.Close();
            }
            catch (FileNotFoundException ex)
            {
                MessageBox.Show("Error: Data_Packages.csv doesn't exist, contact DB admin.");
            }
            context.Packages.AddRange(packageList);
            context.SaveChanges();

            //populating DataGridView
            InitialiseDataGridView();
        }