Exemple #1
0
        static void CarAdd(Cars cars)
        {
            double CheckType;

            for (int spec = 0; spec < 4; spec++)
            {
                Console.WriteLine(cars.CarTextArray[spec]);

                if (spec == 3)
                {
                    while (true)
                    {
                        try
                        {
                            CheckType = Convert.ToDouble(Console.ReadLine());
                            cars.CarNewArray[spec] = Convert.ToString(CheckType);
                            break;
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("\nЦена (в долларах) должна содержать только ");
                            Console.WriteLine("положительные числа без букв, символов и пробелов.");
                            Console.WriteLine("Введите цену автомобиля еще раз:\n");
                            continue;
                        }
                    }
                }
                else
                {
                    cars.CarNewArray[spec] = Console.ReadLine();
                }

                cars.CarAllArray[cars.CarsCounter, spec] = cars.CarNewArray[spec];
            }
            cars.CarsCounter++;
        }
        private void addCar()
        {
            using (var transaction = db.Database.BeginTransaction())
            {
                if (add)
                {
                    if (MarkCB.SelectedItem != null && Color_TB.Text != "" && YearCB.SelectedItem != null && CostTB.Text != "")
                    {
                        Cars a = new Cars();
                        a.CarID    = db.Cars.Count() + 1;
                        a.mark     = MarkCB.SelectedItem.ToString();
                        a.producer = ProducerTB.Text;
                        a.color    = Color_TB.Text;
                        a.cost     = Convert.ToInt32(CostTB.Text);
                        a.year     = Convert.ToInt32(YearCB.SelectedItem.ToString());

                        if (FilePathTB.Text != "")
                        {
                            a.photo = FilePathTB.Text;
                        }

                        db.Cars.Add(a);
                        db.SaveChanges();
                        transaction.Commit();

                        MessageBox.Show("Автомобиль успешно добавлен");


                        Close();
                    }
                    else
                    {
                        MessageBox.Show("Заполните все поля!!!");
                    }
                }
                else
                {
                    if (MarkCB.SelectedItem != null && Color_TB.Text != "" && YearCB.SelectedItem != null && CostTB.Text != "")
                    {
                        var a = db.Cars
                                .Where(c => c.CarID == index)
                                .FirstOrDefault();

                        a.CarID    = index;
                        a.mark     = MarkCB.SelectedItem.ToString();
                        a.producer = ProducerTB.Text;
                        a.color    = Color_TB.Text;
                        a.cost     = Convert.ToInt32(CostTB.Text);
                        a.year     = Convert.ToInt32(YearCB.SelectedItem.ToString());

                        if (FilePathTB.Text != "")
                        {
                            a.photo = FilePathTB.Text;
                        }


                        db.SaveChanges();
                        transaction.Commit();

                        MessageBox.Show("Автомобиль успешно добавлен");

                        MainWindow main = this.Owner as MainWindow;
                        main.InUpdatePage();
                        main.updateAfterChange();
                        Close();
                    }
                    else
                    {
                        MessageBox.Show("Заполните все поля!!!");
                    }
                }
            }
        }
Exemple #3
0
 /// <summary>
 /// Adds car in list of cars
 /// </summary>
 /// <param name="car">Car</param>
 public void AddCar(Car car)
 {
     Cars.Add(car);
 }