Beispiel #1
0
        void SearchProperties()
        {
            Vehicle toSearch = null;

            Console.WriteLine("\r\n\r\nFill the fields you wanna search, leave rest empty... ");
            Console.Write("type: ");
            string type = Extra.NormalFormatting(Console.ReadLine()); //todo: make formatting correct

            Console.Write("regnr: ");
            string reg = Extra.CAPITALFormatting(Console.ReadLine()); //todo: check formatting

            Console.Write("color: ");
            string color = Extra.CAPITALFormatting(Console.ReadLine().ToUpper()); //todo: check formatting

            Console.Write("number of wheels: ");
            string wheels   = Console.ReadLine(); //todo: fix checking
            int    nowheels = -1;

            if (!string.IsNullOrEmpty(wheels))  //nowheels = Int32.tr(wheels); //todo:  formatting
            {
                if (!Int32.TryParse(wheels, out nowheels))
                {
                    Console.WriteLine("unable to parse");
                }
            }

            if (type == "Airplane") //jag gillar inte den här specifika koden ..
            {
                double maxalt = theGarage.AskForMaxAltitude();
                toSearch = new Airplane(reg, color, nowheels, maxalt); //todo: add with proper layering
            }
            else if (type == "Boat")
            {
                double weight = theGarage.AskForWeight();
                toSearch = new Boat(reg, color, nowheels, weight);
            }
            else if (type == "Bus")
            {
                int passengers = theGarage.AskForPassengers();
                toSearch = new Bus(reg, color, nowheels, passengers);
            }
            else if (type == "Car")
            {
                double gasolineconsumption = theGarage.AskForGasolineConsumption();
                toSearch = new Car(reg, color, nowheels, gasolineconsumption);
            }
            else if (type == "Motorcycle")
            {
                double topspeed = theGarage.AskForTopSpeed();
                toSearch = new Motorcycle(reg, color, nowheels, topspeed);
            }
            else
            {
                toSearch = new Vehicle(reg, color, nowheels);
            }

            var searchItems = new Queue <Vehicle>(theGarage.Lookup(toSearch.GetType().Name,
                                                                   toSearch.RegNumber,
                                                                   toSearch.Color,
                                                                   toSearch.NoWheels));

            while (searchItems.Count > 0)
            {
                var t = searchItems.Dequeue();
                Console.WriteLine("found: " + t.Color);
            }
        }
        public Queue <Vehicle> Lookup(string type, string reg, string color, int nowheels)
        {
            var lookedUp = new Queue <Vehicle>();

            //check if type exists in structure first
            //todo: format type properly
            bool validType = false;

            foreach (var ch in types)
            {
                if (type == ch.Name)
                {
                    validType = true;
                }
            }
            if (!validType)
            {
                type = "";
            }

            //todo: better formatting check
            if (string.IsNullOrEmpty(reg))
            {
                reg = "";
            }
            if (string.IsNullOrEmpty(color))
            {
                reg = "";
            }

            string wheels; //kan inte komma ihåg varför jag gjorde denna till string - möjligen hjärnsläpp

            if (nowheels > 0)
            {
                wheels = nowheels.ToString();
            }

            foreach (var ch in garage)
            {
                string searchType, searchReg, searchColor, searchWheels;

                if (type == "")
                {
                    searchType = ch.GetType().Name;
                }
                else
                {
                    searchType = type;
                }

                if (reg == "")
                {
                    searchReg = ch.RegNumber;
                }
                else
                {
                    searchReg = reg;
                }

                if (color == "")
                {
                    searchColor = ch.Color;
                }
                else
                {
                    searchColor = color;
                }

                if (nowheels <= 0)
                {
                    searchWheels = ch.NoWheels.ToString();
                }
                else
                {
                    //todo: formatting
                    searchWheels = nowheels.ToString(); //?
                }

                if ((ch.GetType().Name == searchType) &&
                    (ch.RegNumber == searchReg) &&
                    (ch.Color == searchColor) &&
                    (ch.NoWheels.ToString() == searchWheels))
                {
                    Vehicle forReturn;
                    switch (searchType) //could be generalised but time flies
                    {
                    case "Airplane":
                        //maxalt = double.Parse(toLookup.GetSpecific());//todo: formatting
                        forReturn = new Airplane(searchReg, searchColor, Int32.Parse(searchWheels), 0);     //todo: fix parse
                        break;

                    case "Boat":
                        //weight = double.Parse(toLookup.GetSpecific());
                        forReturn = new Boat(searchReg, searchColor, Int32.Parse(searchWheels), 0);     //todo: fix parse
                        break;

                    case "Bus":
                        //passengers = Int32.Parse(toLookup.GetSpecific());
                        forReturn = new Bus(searchReg, searchColor, Int32.Parse(searchWheels), 0);     //todo: fix parse
                        break;

                    case "Car":
                        //gasolineconsumption = Int32.Parse(toLookup.GetSpecific());
                        forReturn = new Car(searchReg, searchColor, Int32.Parse(searchWheels), 0);     //todo: fix parse
                        break;

                    case "Motorcycle":
                        //topspeed = Int32.Parse(toLookup.GetSpecific());
                        forReturn = new Motorcycle(searchReg, searchColor, Int32.Parse(searchWheels), 0);     //todo: fix parse
                        break;

                    default:
                        forReturn = new Vehicle(searchReg, searchColor, Int32.Parse(searchWheels));     //todo: fix parse
                        break;
                    }
                    //todo: implement all specific (as searchable) if time ...
                    if (forReturn != null)
                    {
                        lookedUp.Enqueue(forReturn);
                        Console.WriteLine("adding to search-result..." + forReturn.RegNumber);
                    }
                }
            }
            return(lookedUp);
        }