예제 #1
0
        }//end constructor

        /// <summary>
        /// creates a new vehicle and adds it to systemVehicles
        /// </summary>
        /// <param name="type"></param>
        /// <param name="make"></param>
        /// <param name="model"></param>
        /// <param name="colour"></param>
        /// <param name="priceRange"></param>
        /// <param name="kilometers"></param>
        /// <param name="imagePath"></param>
        /// <param name="loadRating"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool AddVehicle(string type, string make, string model, string colour, string priceRange, int kilometers, string imagePath, string loadRating, string user)
        {
            try
            {
                int newVID;

                //shouldnt happen, but if we dont have an admin
                //we can give them the first id
                if (systemVehicles.Count == 0)
                {
                    newVID = 100000;
                }
                else
                {
                    newVID = (int)systemVehicles.Keys.Last();
                    while (systemVehicles.ContainsKey(newVID))
                    {
                        newVID++;
                    }
                }

                //put new user in local scope
                Vehicle newVehicle;

                //check what user type we are creating
                if (type == "Light Truck")
                {
                    newVehicle = new LightTruck(newVID, type, make, model, colour, priceRange, kilometers, false, null, imagePath, loadRating);
                }
                //otherwise we have a car
                else
                {
                    newVehicle = new Car(newVID, type, make, model, colour, priceRange, kilometers, false, null, imagePath, "0");
                }

                string logFile = new DateTime().ToLongDateString() + " - New " + type + " created by " + user + ": " + newVehicle.ToString();

                //add the new vehicle to our systemVehicles dictionary
                systemVehicles.Add(newVID, newVehicle);

                //resave the vehicles file
                if (WriteSerializedFile(this.filePath, this.fileDir, systemVehicles))
                {
                    WriteLogFile(this.programDir + @"\logs\log.txt", this.programDir + @"\logs\", logFile);
                    //all good return true
                    return(true);
                }
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine("{0} Argument Null Exception caught: ", ex);
                return(false);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine("{0} Arguement Exception caught: ", ex);
                return(false);
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} An unknown exception was caught: ", ex);
                return(false);
            }//end try/catch


            //we shouldnt be here, so something went wrong
            return(false);
        }//end AddVehicle()
예제 #2
0
        }     //end SearchVehiclesBtn_Click()

        /// <summary>
        /// Load the search results in the vehicle table
        /// </summary>
        /// <param name="resultSet"></param>
        private void loadSearchResults(SortedDictionary <int, Entity> resultSet)
        {
            while (SearchVehiclesResultTable.Controls.Count > 0)
            {
                SearchVehiclesResultTable.Controls[0].Dispose();
                SearchVehiclesResultTable.RowCount = 0;
            }

            foreach (KeyValuePair <int, Entity> vehicleSet in resultSet)
            {
                string  labelText = "";
                Vehicle t         = (Vehicle)vehicleSet.Value;

                //we cant downcast implicitly, so we need to just instantiate new vehicle type objects
                if (vehicleSet.Value.GetEntityType() == "Light Truck")
                {
                    LightTruck currentVehicle = new LightTruck(t.GetVehicleID(),
                                                               t.GetVehicleType(),
                                                               t.GetVehicleMake(),
                                                               t.GetVehicleModel(),
                                                               t.GetVehicleColour(),
                                                               t.GetVehiclePriceRange(),
                                                               t.GetVehicleKilometers(),
                                                               t.GetVehicleSoldStatus(),
                                                               t.GetVehicleSoldDate(),
                                                               t.GetImagePath(),
                                                               t.GetVehicleLoadRating()
                                                               );
                    labelText = currentVehicle.GetVehicleDetails();
                }
                else if (vehicleSet.Value.GetEntityType() == "Car")
                {
                    Car currentVehicle = new Car(t.GetVehicleID(),
                                                 t.GetVehicleType(),
                                                 t.GetVehicleMake(),
                                                 t.GetVehicleModel(),
                                                 t.GetVehicleColour(),
                                                 t.GetVehiclePriceRange(),
                                                 t.GetVehicleKilometers(),
                                                 t.GetVehicleSoldStatus(),
                                                 t.GetVehicleSoldDate(),
                                                 t.GetImagePath(),
                                                 "0"
                                                 );
                    labelText = currentVehicle.GetVehicleDetails();
                }

                //get a reference to the previous existent
                // RowStyle temp = SearchVehiclesResultTable.RowStyles[SearchVehiclesResultTable.RowCount - 1];
                //increase panel rows count by one
                SearchVehiclesResultTable.RowCount++;
                //add a new RowStyle as a copy of the previous one
                SearchVehiclesResultTable.RowStyles.Add(new RowStyle(SizeType.Absolute, 45));
                //add your three controls
                SearchVehiclesResultTable.Controls.Add(new Label()
                {
                    Text = vehicleSet.Key.ToString()
                }, 0, SearchVehiclesResultTable.RowCount - 1);
                SearchVehiclesResultTable.Controls.Add(new Label()
                {
                    Text = labelText, AutoSize = true, AutoEllipsis = true
                }, 1, SearchVehiclesResultTable.RowCount - 1);

                //Button panel
                Panel buttonPanel = new FlowLayoutPanel()
                {
                    Dock = DockStyle.Fill
                };

                //view button
                Button ViewButton = new Button()
                {
                    Text = "View", Name = vehicleSet.Key.ToString(), BackColor = Color.DodgerBlue, FlatStyle = FlatStyle.Standard
                };
                ViewButton.Click += ViewVehicleButton_Click;

                //edit button
                Button EditButton = new Button()
                {
                    Text = "Edit", Name = vehicleSet.Key.ToString(), BackColor = Color.Aquamarine, FlatStyle = FlatStyle.Standard
                };
                EditButton.Click += EditVehicleButton_Click;

                buttonPanel.Controls.Add(ViewButton);
                buttonPanel.Controls.Add(EditButton);

                SearchVehiclesResultTable.Controls.Add(buttonPanel, 2, SearchVehiclesResultTable.RowCount - 1);
            }
        }//end loadSearchResults()