//FUNCTION        :readFile
        //DESCRIPTION     :read content of the database.txt and generate instance according to the content. and save the instance to the arraylist.
        //
        //PARAMETERS      :
        //                ref ArrayList motorcycleArry: The reference of the arrarylist save the motorcycle instances.
        //                ref ArrayList autoMobileArray: The reference of the arrarylist save the automobile instances.
        //                ref ArrayList truckArray:       The reference of the arrarylist save the truck instances.
        //RETURNS         :void
        private static void readFile(ref ArrayList motorcycleArry, ref ArrayList autoMobileArray, ref ArrayList truckArray)
        {
            // int motor_id = 0;
            int auto_id = 0;
            int truck_id = 0;
            int motor_id = 0;

            if (!File.Exists("Database.txt"))
            {
                File.Create("Database.txt").Close();
                return;
            }

            System.IO.StreamReader file = new System.IO.StreamReader("Database.txt");

            string line;

            char[] delimiterChars = { '|' };

            while ((line = file.ReadLine()) != null)
            {

                string[] words = line.Split(delimiterChars);

                if (words[0] == "automobile")
                {
                    //new instance according to the information from the database.txt file.
                    Automobile temp = new Automobile(words[1], words[2], Convert.ToInt32(words[3]), Convert.ToInt32(words[4]), words[5], Convert.ToInt32(words[6]), Convert.ToInt32(words[7]), Convert.ToInt32(words[8]), words[9]);
                    auto_id++;
                    //generate the vehicle id.
                    string strId = "AM" + auto_id;
                    temp.Vehicle_Id = strId;
                    //add a instance to the arraylist.
                    autoMobileArray.Add(temp);

                }
                else if (words[0] == "truck")
                {
                    Truck temp = new Truck(words[1], words[2], Convert.ToInt32(words[3]), Convert.ToInt32(words[4]), words[5], Convert.ToInt32(words[6]), Convert.ToInt32(words[7]), (float)Convert.ToDecimal(words[8]), (float)Convert.ToDecimal(words[9]));
                    truck_id++;
                    string strId2 = "ST" + truck_id;
                    temp.Vehicle_Id = strId2;
                    truckArray.Add(temp);
                }
                else if (words[0] == "motorcycle")
                {
                    Motorcycle temp = new Motorcycle(words[1], words[2], Convert.ToInt32(words[3]), Convert.ToInt32(words[4]), words[5], Convert.ToInt32(words[6]), Convert.ToInt32(words[7]), words[8]);
                    motor_id++;
                    string strId3 = "MC" + motor_id;
                    temp.Vehicle_Id = strId3;
                    motorcycleArry.Add(temp);
                }

            }
            //close file
            file.Close();
        }
Пример #2
0
        static void Main(string[] args)
        {
            Automobile[] arr = new Automobile[10];

            for (int i = 0; i < arr.Length; i++)
            {
                (arr[i] as Car).Name = 231;
                ((Car)arr[i]).
            }
        }
        // FUNCTION NAME : validate_vehicle_data(string vehicle_type)
        //
        // PARAMETER     : string vehicle_type: contains the type of the vehicle as a string.
        //
        // RETURN        : void
        //
        // DESCRIPTION   : It gets the input from the user and validates before adding to the list.
        private void validate_vehicle_data(string vehicle_type)
        {
            //local variables.
               string manufacturer="";
               string model="";
               string model_year="";
               string initial_purchase_price="";
               string purchase_date="";
               string current_odometer="";
               string size_of_engine="";
               string type_of_motorcycle="";
               string number_of_doors="";
               string type_of_fuel="";
               string cargo_capacity="";
               string towing_capacity="";
               string vehicle_id = "";

               int m_year = 0, i_purchase_price=0, odometer=0, s_engine=0, n_doors=0, c_capacity=0, t_capacity = 0;

               //if validation fails , then it will be used to ask again.
               bool ask_again = true;

               //get the manufacturer from the user.
               while (ask_again)
               {
               System.Console.Write("Enter the manufacturer\t\t   : ");
               manufacturer = System.Console.ReadLine();

               //validate the data in other function which returns the bool value to ask again or not from the user.
               ask_again = validate_string(manufacturer);
               }

               //get the vehicle model from the user.
               ask_again = true;
               while (ask_again)
               {
               System.Console.Write("Enter the vehicle model\t\t   : ");
               model = System.Console.ReadLine();

               //validate the data in other function which returns the bool value to ask again or not from the user.
               ask_again= validate_as_num_aplha(model);
               }

               //get the vehicle model year from the user.
               ask_again = true;
               while (ask_again)
               {
               System.Console.Write("Enter the vehicle model year\t   : ");
               model_year = System.Console.ReadLine();

               //validate the data as int in other function which returns the bool value to ask again or not from the user.
               ask_again = validate_as_int(model_year, ref m_year);

               }

               //get the initial purchase price of motorcycle from the user.
               if (vehicle_type == "motorcycle")
               {
               ask_again = true;
               while (ask_again)
               {
                   System.Console.Write("Enter the initial purchase price \n\t (minimum value is $1500 ) : ");
                   initial_purchase_price = System.Console.ReadLine();

                   //validate the data as int in other function which returns the bool value to ask again or not from the user.
                   ask_again = validate_as_int(initial_purchase_price, ref i_purchase_price);

                   //compare it with minimum value.
                   if (i_purchase_price < 1500)
                   {
                       Console.WriteLine("\t\t\t( ERROR - Enter a value greater than $1500... )\n");
                       ask_again = true;
                   }
               }
               }

               //get the initial purchase price of automobile from the user.
               if (vehicle_type == "automobile")
               {
               ask_again = true;
               while (ask_again)
               {
                   System.Console.Write("Enter the initial purchase price \n\t (minimum value is $500 )  : ");
                   initial_purchase_price = System.Console.ReadLine();

                   //validate the data as int in other function which returns the bool value to ask again or not from the user.
                   ask_again = validate_as_int(initial_purchase_price, ref i_purchase_price);

                   //compare it with minimum value.
                   if(i_purchase_price < 500)
                   {
                       Console.WriteLine("\t\t\t( ERROR - Enter a value greater than $500... )\n");
                       ask_again = true;
                   }
               }
               }

               //get the initial purchase price of truck from the user.
               if (vehicle_type == "truck")
               {
               ask_again = true;
               while (ask_again)
               {
                   System.Console.Write("Enter the initial purchase price\n\t(minimum value is $0 )\t   : ");
                   initial_purchase_price = System.Console.ReadLine();

                   //validate the data as int in other function which returns the bool value to ask again or not from the user.
                   ask_again = validate_as_int(initial_purchase_price, ref i_purchase_price);

                   //compare it with minimum value.
                   if (i_purchase_price < 0)
                   {
                       Console.WriteLine("\t\t\t( ERROR - Enter a value greater than $0... )\n");
                       ask_again = true;
                   }
               }
               }

               //get the initial purchase date of vehicle from the user.
               ask_again = true;
               while (ask_again)
               {
               System.Console.Write("Enter the purchase date\t\t   : ");
               purchase_date = System.Console.ReadLine();

               //validate the data as datetime type in other function which returns the bool value to ask again or not from the user.
               ask_again = validate_as_date_time(purchase_date);
               }

               //get the odometer reading of vehicle from the user.
               ask_again = true;
               while (ask_again)
               {
               System.Console.Write("Enter the current odometer reading : ");
               current_odometer = System.Console.ReadLine();

               //validate the data as int in other function which returns the bool value to ask again or not from the user.
               ask_again = validate_as_int(current_odometer, ref odometer);
               }

               //get the size of engine of vehicle from the user.
               ask_again = true;
               while (ask_again)
               {
               System.Console.Write("Enter the size of engine\t   : ");
               size_of_engine = System.Console.ReadLine();

               //validate the data as int in other function which returns the bool value to ask again or not from the user.
               ask_again = validate_as_int(size_of_engine,ref s_engine);
               }

               //get the type of motorcycle from the user.
               if (vehicle_type == "motorcycle")
               {
               ask_again = true;
               while (ask_again)
               {
                   System.Console.Write("Enter the type of motorcycle \t   : ");
                   type_of_motorcycle = System.Console.ReadLine();

                   //validate the data as string in other function which returns the bool value to ask again or not from the user.
                   ask_again = validate_string(type_of_motorcycle);
               }

               //concatenate the motorcycle id to the vehicle id.
               vehicle_id = "MC" + motorcycle_id;

               //create an instance of motorcycle and call the constructor to store the data in the properties.
               Motorcycle obj = new Motorcycle(manufacturer,model,m_year,i_purchase_price,purchase_date,odometer,s_engine,type_of_motorcycle);

               //calls the accessor method of vehicle id property to set it a value.
               obj.Vehicle_Id = vehicle_id;

               //add the motorcycle object in list.
               list_motorcycle.Add(obj);

               }

               if(vehicle_type=="automobile")
               {
               //get the number of doors from the user.
               ask_again = true;
               while (ask_again)
               {

                   System.Console.Write("Enter the number of doors(2,3, or 5): ");
                   number_of_doors = System.Console.ReadLine();

                   //validate the data as int in other function which returns the bool value to ask again or not from the user.
                   ask_again = validate_as_int(number_of_doors, ref n_doors);

                   //number of doors has to be between 2-5.
                   if(n_doors >=2 && n_doors<=5)
                   {
                       ask_again = false;
                   }
                   else
                   {
                       Console.WriteLine("\n\t\t\t( ERROR - Number of doors can be 2,3,4 or 5...)");
                       ask_again = true;
                   }
               }

               //get the type of fuel from the user.
               ask_again = true;
               while (ask_again)
               {
                   System.Console.Write("Enter the type of fuel\t\t   : ");
                   type_of_fuel = System.Console.ReadLine();
                   ask_again = validate_string(type_of_fuel);

                   if( (string.Compare(type_of_fuel, "Electric", true) == 0) || (string.Compare(type_of_fuel, "Diesel", true)==0) || (string.Compare(type_of_fuel, "Gas", true) == 0))
                   {
                       ask_again = false;
                   }
                   else
                   {
                       Console.WriteLine("\n\t\t\t(ERROR - Fuel type can be Electric,Diesel or Gas... )\n");
                       ask_again = true;
                   }
               }

               //concatenate the motorcycle id to the vehicle id.
               vehicle_id = "AM" + automobile_id;

               //create an instance of automobile and call the constructor to store the data in the properties.
               Automobile obj1 = new Automobile(manufacturer,model,m_year,i_purchase_price,purchase_date,odometer,s_engine,n_doors,type_of_fuel);

               //calls the accessor method of vehicle id property to set it a value.
               obj1.Vehicle_Id = vehicle_id ;

               //add the automobile object in list.
               list_automobile.Add(obj1);

               }

               if(vehicle_type=="truck")
               {
               //get the cargo capacity from the user.
               ask_again = true;
               while (ask_again)
               {
                   System.Console.Write("Enter the cargo capacity\t   : ");
                   cargo_capacity = System.Console.ReadLine();

                   //validate the data as int in other function which returns the bool value to ask again or not from the user.
                   ask_again = validate_as_int(cargo_capacity, ref c_capacity);
               }

               //get the towing capacity from the user.
               ask_again = true;
               while (ask_again)
               {
                   System.Console.Write("Enter the towing capacity\t   : ");
                   towing_capacity = System.Console.ReadLine();
                   ask_again = validate_as_int(towing_capacity, ref t_capacity);
               }

               //concatenate the truck id to the vehicle id.
               vehicle_id = "ST" + truck_id;

               //create an instance of truck and call the constructor to store the data in the properties.
               Truck obj2 = new Truck(manufacturer, model, m_year, i_purchase_price,purchase_date, odometer, s_engine,c_capacity,t_capacity);

               //calls the accessor method of vehicle id property to set it a value.
               obj2.Vehicle_Id = vehicle_id;

               //add the automobile object in list.
               list_truck.Add(obj2);
               }
        }