示例#1
0
        public List <Vehicle> ReadVehicle()
        {
            //open the Vehicles.dat file
            StreamReader   reader   = File.OpenText(@"Vehicles.dat");
            List <Vehicle> tempList = new List <Vehicle>();

            string row = null;

            string[] columns;
            //read each line from the file
            while ((row = reader.ReadLine()) != null)
            {
                //Splits each row of strings from the text with a \t and :
                columns = row.Split('\t', ':');

                //if ..
                //the string contains the string "Car"
                if (row.Contains("Car"))
                {
                    //Create a Car object and insert the split strings into its allocated properties
                    Car car = new Car(columns[2], columns[4], columns[6], Int32.Parse(columns[8]), Double.Parse(columns[10]), Int32.Parse(columns[12]));
                    //add it to the tempList
                    tempList.Add(car);
                }
                //the string contains the string "QuadBike"
                else if (row.Contains("QuadBike"))
                {
                    //Create a QuadBike object and insert the split strings into its allocated properties
                    QuadBike quad = new QuadBike(columns[2], columns[4], columns[6], Int32.Parse(columns[8]), Double.Parse(columns[10]), bool.Parse(columns[12]));
                    //add it to the tempList
                    tempList.Add(quad);
                }
                //the string contains the string "MotorBike"
                else if (row.Contains("MotorBike"))
                {
                    //Create a Motor object and insert the split strings into its allocated properties
                    MotorBike motor = new MotorBike(columns[2], columns[4], columns[6], Int32.Parse(columns[8]), Double.Parse(columns[10]), Int32.Parse(columns[12]));
                    //add it to the tempList
                    tempList.Add(motor);
                }
            }
            //set the vehicles list to the tempList
            vehicles = tempList;
            reader.Close();
            return(vehicles);
        }
示例#2
0
        private void Edit_Vehicle()
        {
            Car       updatedCar;
            QuadBike  updatedQuad;
            MotorBike updatedMotor;

            //if the selected Vehicle is a Car
            if (listBoxView.SelectedItem.ToString().Contains("Car"))
            {
                //create another Car object to store the edited information into it's contructor
                updatedCar = new Car(txtMake.Text, txtModel.Text, txtRegNum.Text, Int32.Parse(txtNumWheels.Text), Double.Parse(txtPrice.Text), Int32.Parse(txtNumDoorsValves.Text));
                //replace the original Car object with the new one
                fileStreamer.Edit(vehicleSelected, updatedCar);
            }
            //if the selected Vehicle is a QuadBike
            else if (listBoxView.SelectedItem.ToString().Contains("QuadBike"))
            {
                //if the radioButtonTrue is checked
                if (radioButtonTrue.Checked == true)
                {
                    //create another QuadBike object to store the edited information into it's constructor with ChildProof as true
                    updatedQuad = new QuadBike(txtMake.Text, txtModel.Text, txtRegNum.Text, Int32.Parse(txtNumWheels.Text), Double.Parse(txtPrice.Text), true);
                }
                //if radioButtonFalse is checked
                else
                {
                    //create another QuadBike object to store the edited information into it's constructor with ChildProof as false
                    updatedQuad = new QuadBike(txtMake.Text, txtModel.Text, txtRegNum.Text, Int32.Parse(txtNumWheels.Text), Double.Parse(txtPrice.Text), false);
                }
                //replace the original QuadBike object with the new one
                fileStreamer.Edit(vehicleSelected, updatedQuad);
            }
            //if the selected Vehicle is a MotorBike
            else if (listBoxView.SelectedItem.ToString().Contains("MotorBike"))
            {
                //create another MotorBike object to store the edited information into it's contructor
                updatedMotor = new MotorBike(txtMake.Text, txtModel.Text, txtRegNum.Text, Int32.Parse(txtNumWheels.Text), Double.Parse(txtPrice.Text), Int32.Parse(txtNumDoorsValves.Text));
                //replace the original MotorBike object with the new one
                fileStreamer.Edit(vehicleSelected, updatedMotor);
            }
            //repopulate the listBox
            this.Populate_ListBox();
            //hide all edit fields
            this.Hide_EditFields();
        }
示例#3
0
        private void Add_Vehicle()
        {
            //if Car is selected
            if (comboBoxVehicle.SelectedItem.ToString() == "Car")
            {
                //Create a Car object and insert field values in to its constructor
                Car car = new Car(txtMake.Text, txtModel.Text, txtRegNum.Text, Int32.Parse(txtNumWheels.Text), Double.Parse(txtPrice.Text), Int32.Parse(txtNumDoorsValves.Text));
                //add the Car in to the vehicles list
                vehicles.Add(car);
            }
            //if Quad Bike is selected
            else if (comboBoxVehicle.SelectedItem.ToString() == "Quad Bike")
            {
                //Create a QuadBike object and insert field values in to its constructor
                QuadBike quad = null;
                if (radioButtonTrue.Checked == true)
                {
                    quad = new QuadBike(txtMake.Text, txtModel.Text, txtRegNum.Text, Int32.Parse(txtNumWheels.Text), Double.Parse(txtPrice.Text), true);
                }
                else if (radioButtonFalse.Checked == true)
                {
                    quad = new QuadBike(txtMake.Text, txtModel.Text, txtRegNum.Text, Int32.Parse(txtNumWheels.Text), Double.Parse(txtPrice.Text), false);
                }

                //add the QuadBike into the vehicles list
                vehicles.Add(quad);
            }
            //if Motor Bike is selected
            else if (comboBoxVehicle.SelectedItem.ToString() == "Motor Bike")
            {
                //Create a MotorBike object and insert field values in to its constructor
                MotorBike motor = new MotorBike(txtMake.Text, txtModel.Text, txtRegNum.Text, Int32.Parse(txtNumWheels.Text), Double.Parse(txtPrice.Text), Int32.Parse(txtNumDoorsValves.Text));
                //add the MotorBike into the vehicles list
                vehicles.Add(motor);
            }
            //write the vehicles list into the file
            fileStreamer.Add(vehicles);
            this.Close();
        }
示例#4
0
        private void listBoxView_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if nothing is selected
            if (listBoxView.SelectedItem == null)
            {
                //hide all edit fields
                this.Hide_EditFields();
            }
            else
            {
                //display all fields relevant to all Vehicles
                lblModel.Visible     = true;
                txtModel.Visible     = true;
                lblMake.Visible      = true;
                txtMake.Visible      = true;
                lblRegNum.Visible    = true;
                txtRegNum.Visible    = true;
                lblNumWheels.Visible = true;
                txtNumWheels.Visible = true;
                lblPrice.Visible     = true;
                txtPrice.Visible     = true;

                //if Car is selected
                if (listBoxView.SelectedItem.ToString().Contains("Car"))
                {
                    //display remaining fields relevant to Car
                    lblNumDoorsValves.Visible = true;
                    lblNumDoorsValves.Text    = "Number of Doors";
                    txtNumDoorsValves.Visible = true;
                    lblChildProof.Visible     = false;
                    radioButtonTrue.Visible   = false;
                    radioButtonFalse.Visible  = false;

                    //get selected Vehicle object
                    this.Selected_Vehicle();
                    //Cast Vehicle object as a Car
                    Car carSelected = (Car)vehicleSelected;

                    //populate fields with the selected Vehicle's information
                    txtMake.Text           = carSelected.Make;
                    txtModel.Text          = carSelected.Model;
                    txtRegNum.Text         = carSelected.RegNum;
                    txtNumWheels.Text      = carSelected.NumWheels.ToString();
                    txtPrice.Text          = carSelected.Price.ToString();
                    txtNumDoorsValves.Text = carSelected.NumDoors.ToString();
                }
                //if QuadBike is selected
                else if (listBoxView.SelectedItem.ToString().Contains("QuadBike"))
                {
                    //display remaining fields relevant to QuadBike
                    lblNumDoorsValves.Visible = false;
                    txtNumDoorsValves.Visible = false;
                    lblChildProof.Visible     = true;
                    radioButtonTrue.Visible   = true;
                    radioButtonFalse.Visible  = true;

                    //get selected Vehicle object
                    this.Selected_Vehicle();
                    //Cast Vehicle object as a QuadBike
                    QuadBike quadSelected = (QuadBike)vehicleSelected;

                    //populate fields with the selected Vehicle's information
                    txtMake.Text      = quadSelected.Make;
                    txtModel.Text     = quadSelected.Model;
                    txtRegNum.Text    = quadSelected.RegNum;
                    txtNumWheels.Text = quadSelected.NumWheels.ToString();
                    txtPrice.Text     = quadSelected.Price.ToString();

                    //if ChildProof is true
                    if (quadSelected.ChildProof == true)
                    {
                        //check radioButtonTrue
                        radioButtonTrue.Checked = true;
                    }
                    //if ChildProof is false
                    else
                    {
                        //check radioButtonFalse
                        radioButtonFalse.Checked = true;
                    }
                }
                //if MotorBike is selected
                else if (listBoxView.SelectedItem.ToString().Contains("MotorBike"))
                {
                    //display remaining fields relevant to MotorBike
                    lblNumDoorsValves.Visible = true;
                    lblNumDoorsValves.Text    = "Number of Valves";
                    txtNumDoorsValves.Visible = true;
                    lblChildProof.Visible     = false;
                    radioButtonTrue.Visible   = false;
                    radioButtonFalse.Visible  = false;

                    //get selected Vehicle object
                    this.Selected_Vehicle();
                    //Cast Vehicle object as a MotorBike
                    MotorBike motorSelected = (MotorBike)vehicleSelected;

                    //populate fields with the selected Vehicle's information
                    txtMake.Text           = motorSelected.Make;
                    txtModel.Text          = motorSelected.Model;
                    txtRegNum.Text         = motorSelected.RegNum;
                    txtNumWheels.Text      = motorSelected.NumWheels.ToString();
                    txtPrice.Text          = motorSelected.Price.ToString();
                    txtNumDoorsValves.Text = motorSelected.Valves.ToString();
                }
            }
        }