예제 #1
0
        public Vehicle POST(Vehicle vehicle)
        {
            Vehicle v = vehicle;

            Dealer.AddVehicle(v);
            return(v);
        }
예제 #2
0
        /// <summary>
        /// Creates a vehicle of a given state dependent on selected customer type(null,pri,bus). Has inputbox fault logic to stop the creation of bugged inputs
        /// </summary>
        private void create_vehicle_click(object sender, RoutedEventArgs e)
        {
            bool car_null_exception     = false;
            bool truck_null_exception   = false;
            bool car_format_exception   = false;
            bool truck_format_exception = false;
            bool error_found_truck      = false;
            bool error_found_car        = false;

            string car_boxes_empty_string    = "";
            string truck_boxes_empty_string  = "";
            string car_wrong_format_string   = "";
            string truck_wrong_format_string = "";

            //selected private
            #region
            if (select_combobox_customer.SelectedValue is Private || select_combobox_customer.SelectedValue == null)
            {
                //Test if text boxes in the car section are empty.
                if (string.IsNullOrEmpty(textbox_car_model.Text) == true ||
                    string.IsNullOrEmpty(textbox_car_license.Text) == true ||
                    string.IsNullOrEmpty(textbox_car_price.Text) == true ||
                    string.IsNullOrEmpty(textbox_car_colour.Text) == true)
                {
                    car_boxes_empty_string = "You have forgotten to fill in informationboxes in your car informations\n";
                    car_null_exception     = true;
                }

                //Test if text boxes in the car section are in the right format
                if (IsALLnumeric(textbox_car_price.Text, false) == false)
                {
                    car_wrong_format_string = "Your have format errors in your car informations\n";
                    car_format_exception    = true;
                }
                if ((car_null_exception ||
                     car_format_exception) == true)
                {
                    MessageBox.Show(car_boxes_empty_string +
                                    car_wrong_format_string);
                    error_found_car = true;
                }
            }

            //selected business
            if (select_combobox_customer.SelectedValue is Business || select_combobox_customer.SelectedValue == null)
            {
                //Test if text boxes in the truck section are empty
                if (string.IsNullOrEmpty(textbox_truck_model.Text) == true ||
                    string.IsNullOrEmpty(textbox_truck_license.Text) == true ||
                    string.IsNullOrEmpty(textbox_truck_rent.Text) == true ||
                    string.IsNullOrEmpty(textbox_truck_colour.Text) == true)
                {
                    truck_boxes_empty_string = "You have forgotten to fill in informationboxes in your truck informations \n";
                    truck_null_exception     = true;
                }
                //Test if text boxes in the truck section are in the right format
                if (IsALLnumeric(textbox_truck_rent.Text, false) == false)
                {
                    truck_wrong_format_string = "Your have format errors in your truck informations\n";
                    truck_format_exception    = true;
                }
            }
            //If any of the boxes were in the wrong format or empty,
            //then show the messagebox notifying the user and break the finalize action.
            if ((truck_null_exception ||
                 truck_format_exception) == true)
            {
                MessageBox.Show(truck_boxes_empty_string +
                                truck_wrong_format_string);
                error_found_truck = true;
            }

            #endregion
            // if no errors found then complete the finalize action and bring up the finalize window.

            //check if private customer and create contract.
            if (error_found_car == false)
            {
                if (select_combobox_customer.SelectedValue == null)
                {
                    if (combo_veh_size_small_item.IsSelected) //Remember to add size parameter
                    {
                        Small myveh = new Small(textbox_car_colour.Text,
                                                textbox_car_model.Text,
                                                Convert.ToInt32(textbox_car_price.Text),
                                                "in stock",
                                                textbox_car_license.Text);

                        mycardealer.AddVehicle(myveh);
                    }
                    if (combo_veh_size_large_item.IsSelected)
                    {
                        Large myveh = new Large(textbox_car_colour.Text,
                                                textbox_car_model.Text,
                                                Convert.ToInt32(textbox_car_price.Text),
                                                "in stock",
                                                textbox_car_license.Text);

                        mycardealer.AddVehicle(myveh);
                    }


                    MessageBox.Show("Car Added");
                    this.comboBox_Del_Vehicle.ItemsSource = mycardealer.VehicleList;
                }

                if (select_combobox_customer.SelectedValue is Private)
                {
                    if (combo_veh_size_small_item.IsSelected) //Remember to add size parameter
                    {
                        Small myveh = new Small(textbox_car_colour.Text,
                                                textbox_car_model.Text,
                                                Convert.ToInt32(textbox_car_price.Text),
                                                "sold",
                                                textbox_car_license.Text);

                        mycardealer.AddVehicle(myveh);
                        Private b = (Private)select_combobox_customer.SelectedValue;
                        mycardealer.DeleteCustomer(b.Address);
                        Contract gui_contract = new Contract(myveh, "contract");

                        b.AddContract(gui_contract);
                        mycardealer.AddCustomer(b);
                        //MessageBox.Show("ADDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD contract\n");
                    }

                    if (combo_veh_size_large_item.IsSelected)
                    {
                        Large myveh = new Large(textbox_car_colour.Text,
                                                textbox_car_model.Text,
                                                Convert.ToInt32(textbox_car_price.Text),
                                                "sold",
                                                textbox_car_license.Text);

                        mycardealer.AddVehicle(myveh);
                        Private b = (Private)select_combobox_customer.SelectedValue;
                        //Delete the customer without the contract
                        mycardealer.DeleteCustomer(b.Address);
                        Contract gui_contract = new Contract(myveh, "contract");
                        b.AddContract(gui_contract);
                        //Update the new customer with the contract
                        mycardealer.AddCustomer(b);
                    }
                    //       mycardealer.SaveCustomersToFile();

                    this.comboBox_del_customer.ItemsSource = mycardealer.LoadCustomers();
//__________
                    this.select_combobox_customer.ItemsSource = mycardealer.CustomerList;

                    MessageBox.Show(mycardealer.ToString());
                }

                mycardealer.SaveVehiclesToFile();
            }
            //check if business customer and create lease.
            if (error_found_truck == false)
            {
                if (select_combobox_customer.SelectedValue == null)
                {
                    Truck myveh = new Truck(textbox_truck_colour.Text,
                                            textbox_truck_model.Text,
                                            Convert.ToInt32(textbox_truck_rent.Text),
                                            "in stock",
                                            textbox_truck_license.Text);

                    mycardealer.AddVehicle(myveh);
                    MessageBox.Show(mycardealer.ToString());
                    //this.comboBox_Del_Vehicle.ItemsSource = mycardealer.VehicleList;
                }

                if (select_combobox_customer.SelectedValue is Business)
                {
                    Truck myveh = new Truck(textbox_truck_colour.Text,
                                            textbox_truck_model.Text,
                                            Convert.ToInt32(textbox_truck_rent.Text),
                                            "leased",
                                            textbox_truck_license.Text);

                    mycardealer.AddVehicle(myveh);
                    Business b = (Business)select_combobox_customer.SelectedValue;
                    mycardealer.DeleteCustomer(b.Address);
                    Leasing gui_contract = new Leasing(myveh,
                                                       "truckContract",
                                                       Convert.ToInt32(textbox_truck_rent.Text),
                                                       datepicker_truck_start.SelectedDate,
                                                       datepicker_truck_end.SelectedDate);
                    b.AddLease(gui_contract);
                    mycardealer.AddCustomer(b);
                    MessageBox.Show(mycardealer.ToString());
                }

                mycardealer.SaveVehiclesToFile();
                this.comboBox_Del_Vehicle.ItemsSource = mycardealer.VehicleList;
                MessageBox.Show(mycardealer.ToString());
            }
        }
        public void Execute(CarDealer dealer)
        {
            var vehicleTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.BaseType == typeof(Vehicle))
                               .ToArray();

            Vehicle v;
            IEnumerable <PropertyInfo> vehicleProperties;

            while (true)
            {
                Console.Write("Select vehicle kind(");
                for (int i = 0; i < vehicleTypes.Length; ++i)
                {
                    Console.Write($"{i.ToString()} {vehicleTypes[i].Name}");
                    if (i < vehicleTypes.Length - 1)
                    {
                        Console.Write(", ");
                    }
                }

                Console.Write("): ");
                string input = Console.ReadLine();
                int    index;

                if (!int.TryParse(input, out index))
                {
                    continue;
                }
                if (index < 0 || index >= vehicleTypes.Length)
                {
                    continue;
                }

                v = (Vehicle)Activator.CreateInstance(vehicleTypes[index]);
                vehicleProperties = vehicleTypes[index].GetProperties()
                                    .Where(x => Attribute.IsDefined(x, typeof(VehicleProperty)));

                break;
            }

            foreach (var vehicleProperty in vehicleProperties)
            {
                while (true)
                {
                    Console.Write($"Please enter {vehicleProperty.Name}: ");
                    string input = Console.ReadLine();
                    if (vehicleProperty.PropertyType.Equals(typeof(string)))
                    {
                        vehicleProperty.SetValue(v, input);
                        break;
                    }
                    else
                    {
                        try
                        {
                            vehicleProperty.SetValue(v, parseToPrimitiveType(input, vehicleProperty.PropertyType));
                            break;
                        }
                        catch (FormatException e)
                        {
                        }
                    }
                }
            }

            /*while (true)
             * {
             *  Console.Write("Enter price: ");
             *  string input = Console.ReadLine();
             *  double price;
             *
             *  if (!double.TryParse(input, out price)) continue;
             *  v.Price = price;
             *  break;
             * }
             *
             * Console.Write("Enter license plate: ");
             * v.LicensePlate = Console.ReadLine();
             * Console.Write("Enter vendor: ");
             * v.Vendor = Console.ReadLine();*/

            dealer.AddVehicle(v);
        }