Ejemplo n.º 1
0
        }//Write list of vehicles to a txt file

        private void ReadTextFile()
        {
            try
            {
                //Pass the file path and file name to the StreamReader constructor
                StreamReader sr = new StreamReader(fileName);

                //Continue to read until you reach end of file
                while (!sr.EndOfStream)
                {
                    bool duplicate = false;

                    string line = sr.ReadLine();

                    string[] fields = line.Split(",");

                    if (fields[0] == "Car")
                    {
                        var temp = new Car()
                        {
                            ID         = int.Parse(fields[1]),
                            Make       = fields[2],
                            Model      = fields[3],
                            Colour     = fields[4],
                            Year       = int.Parse(fields[5]),
                            Price      = float.Parse(fields[6]),
                            EngineSize = float.Parse(fields[7]),
                            BodyType   = fields[8]
                        };

                        foreach (var c in cars)
                        {
                            if (temp.ID == c.ID)
                            {
                                duplicate = true;
                                break;
                            }
                            else
                            {
                                duplicate = false;
                            }
                        } //Duplicate check

                        if (duplicate == false)
                        {
                            cars.Add(temp);
                        }
                    }
                    else if (fields[0] == "Van")
                    {
                        var temp = new Van()
                        {
                            ID         = int.Parse(fields[1]),
                            Make       = fields[2],
                            Model      = fields[3],
                            Colour     = fields[4],
                            Year       = int.Parse(fields[5]),
                            Price      = float.Parse(fields[6]),
                            EngineSize = float.Parse(fields[7]),
                            Wheelbase  = fields[8]
                        };

                        foreach (var v in vans)
                        {
                            if (temp.ID == v.ID)
                            {
                                duplicate = true;
                                break;
                            }
                            else
                            {
                                duplicate = false;
                            }
                        } //Duplicate check

                        if (duplicate == false)
                        {
                            vans.Add(temp);
                        }
                    }
                    else if (fields[0] == "Motorbike")
                    {
                        var temp = new Motorbike()
                        {
                            ID         = int.Parse(fields[1]),
                            Make       = fields[2],
                            Model      = fields[3],
                            Colour     = fields[4],
                            Year       = int.Parse(fields[5]),
                            Price      = float.Parse(fields[6]),
                            EngineSize = float.Parse(fields[7]),
                            Type       = fields[8]
                        };

                        foreach (var m in motorbikes)
                        {
                            if (temp.ID == m.ID)
                            {
                                duplicate = true;
                                break;
                            }
                            else
                            {
                                duplicate = false;
                            }
                        } //Duplicate check

                        if (duplicate == false)
                        {
                            motorbikes.Add(temp);
                        }
                    }
                }

                //close the file
                sr.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                Console.Clear();
                Console.WriteLine("FILE READ.");
                PrintTable();
            }
        }//Reads txt file and converts to list of vehicles
Ejemplo n.º 2
0
        }//Prints tables of the three different types of vehicles

        private void AddVehicle()
        {
            Console.WriteLine("What type of vehicle do you want to add?\n1.Car  2.Van   3.Motorbike");

            Console.Write("\nOption: ");

            int option = int.Parse(Console.ReadLine());

            Console.Clear();

            if (option == 1)
            {
                Console.WriteLine("--CAR--\n");

                Car c = new Car();

                c.ID = cars.Count + 1;

                Console.Write("Make: ");
                c.Make = Console.ReadLine();

                Console.Write("Model: ");
                c.Model = Console.ReadLine();

                Console.Write("Colour: ");
                c.Colour = Console.ReadLine();

                Console.Write("Year: ");
                c.Year = int.Parse(Console.ReadLine());

                Console.Write("Price: ");
                c.Price = float.Parse(Console.ReadLine());

                Console.Write("Engine-Size: ");
                c.EngineSize = float.Parse(Console.ReadLine());

                Console.Write("Body-Type: ");
                c.BodyType = Console.ReadLine();

                cars.Add(c);
            }
            else if (option == 2)
            {
                Console.WriteLine("--VAN--\n");

                Van v = new Van();

                v.ID = vans.Count + 1;

                Console.Write("Make: ");
                v.Make = Console.ReadLine();

                Console.Write("Model: ");
                v.Model = Console.ReadLine();

                Console.Write("Colour: ");
                v.Colour = Console.ReadLine();

                Console.Write("Year: ");
                v.Year = int.Parse(Console.ReadLine());

                Console.Write("Price: ");
                v.Price = float.Parse(Console.ReadLine());

                Console.Write("Engine-Size: ");
                v.EngineSize = float.Parse(Console.ReadLine());

                Console.Write("Wheel-base: ");
                v.Wheelbase = Console.ReadLine();

                vans.Add(v);
            }
            else if (option == 3)
            {
                Console.WriteLine("--MOTORBIKE--\n");

                Motorbike m = new Motorbike();

                m.ID = vans.Count + 1;

                Console.Write("Make: ");
                m.Make = Console.ReadLine();

                Console.Write("Model: ");
                m.Model = Console.ReadLine();

                Console.Write("Colour: ");
                m.Colour = Console.ReadLine();

                Console.Write("Year: ");
                m.Year = int.Parse(Console.ReadLine());

                Console.Write("Price: ");
                m.Price = float.Parse(Console.ReadLine());

                Console.Write("Engine-Size: ");
                m.EngineSize = float.Parse(Console.ReadLine());

                Console.Write("Type: ");
                m.Type = Console.ReadLine();

                motorbikes.Add(m);
            }

            Console.Clear();

            PrintTable();
        }//Adds new vehicle to list