Ejemplo n.º 1
0
            static void Main(string[] args)
            {
                Auto car1    = new Car();
                Auto car2    = new Car();
                Auto raceCar = new RaceCar();
                Auto bike    = new Bike();

                setOn += car1.setdistance;
                setOn += car2.setdistance;
                setOn += raceCar.setdistance;
                setOn += bike.setdistance;
                Console.WriteLine("set the distance ");
                int distance = Convert.ToInt32(Console.ReadLine());

                setOn(distance);
                start          += car1.Start;
                start          += car2.Start;
                start          += raceCar.Start;
                start          += bike.Start;
                car1.finish    += OnFinishHandler;
                car2.finish    += OnFinishHandler;
                bike.finish    += OnFinishHandler;
                raceCar.finish += OnFinishHandler;
                Go             += car1.go;
                Go             += car2.go;
                Go             += bike.go;
                Go             += raceCar.go;
                List <Auto> list = new List <Auto>();

                list.Add(car1);
                list.Add(car2);
                list.Add(bike);
                list.Add(raceCar);
                List <Auto> FinishingList = new List <Auto>();

                if (start != null)
                {
                    start(null, EventArgs.Empty);
                }
                if (Go != null)
                {
                    do
                    {
                        Go(null, EventArgs.Empty);
                        Thread.Sleep(1000);
                        for (int i = 0; i < list.Count; i++)
                        {
                            if (list[i].isFinished == true)
                            {
                                list[i].finish -= OnFinishHandler;
                                Go             -= list[i].go;
                                if (!FinishingList.Contains(list[i]))
                                {
                                    FinishingList.Add(list[i]);
                                }
                            }
                        }
                    }while (countFinished < list.Count);
                }
                Console.WriteLine("On finish: ");
                for (int i = 0; i < FinishingList.Count; i++)
                {
                    Console.WriteLine(FinishingList[i].ToString() + " finished at " + (i + 1) + " position at " + FinishingList[i].dateOfFinish);
                }
            }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            string        url       = "http://www.fuelly.com/motorcycle/";
            string        xPath     = "//a[@class='make-header-link']";
            string        attribute = "href";
            List <string> List1     = scraperLoop(url, xPath, attribute);

            Console.WriteLine("Specify partial/full MAKE (e.g. Honda) of bike OR press enter to continue:");
            var inputBikeMake = Console.ReadLine().ToLower();

            List1 = inputBikeMake != "" ? List1.Where(stringToCheck => stringToCheck.ToLower().Contains(inputBikeMake)).ToList <string>() : List1;

            List <string> List2             = new List <string>();
            List <Bike>   makeAndModelStats = new List <Bike>();

            foreach (string nextUrl in List1)
            {
                url   = nextUrl;
                xPath = "//ul[@class='models-list']//a";
                List <string> urlList = scraperLoop(url, xPath, attribute);
                foreach (string bikeUrl in urlList)
                {
                    List2.Add(bikeUrl);
                }
            }
            //at this point, List2 is a list of urls for each bike make (or the given make) and each bike model.

            List1.Clear();
            Console.WriteLine("Specify partial/full MODEL (e.g. CBR) of bike OR press enter to continue:");
            var inputBikeModel = Console.ReadLine().ToLower();

            List2 = inputBikeModel != "" ? List2.Where(stringToCheck => stringToCheck.ToLower().Contains(inputBikeModel)).ToList <string>() : List2;
            List <Bike> allBikes = new List <Bike>();

            //Process each bike page and compile a list of bikes with their stats
            foreach (string nextUrl in List2)
            {
                string xPathDiv = "//div[@class='model-year-item']";
                var    web      = new HtmlAgilityPack.HtmlWeb();
                var    doc      = web.Load(nextUrl);
                Bike   bike     = new Bike(nextUrl);
                try
                {
                    var scraped = doc.DocumentNode.SelectNodes(xPathDiv);
                    foreach (var bikeYear in scraped)
                    {
                        BikeStatsPerYear bikeStatsPerYear = new BikeStatsPerYear();
                        float            thisOutMpg       = 0;
                        int    thisOutTotalBikes          = 0;
                        int    thisOutYear   = 0;
                        string thisBikeYear  = bikeYear.SelectSingleNode(".//li[@class='summary-year']/span").InnerText.ToString();
                        string thisBikeMpg   = bikeYear.SelectSingleNode(".//li[@class='summary-avg']/span[@class='summary-avg-data']").InnerText.ToString();
                        string thisBikeTotal = Regex.Replace(bikeYear.SelectSingleNode(".//li[@class='summary-total']").InnerText.ToString(), "[^0-9.]", "");
                        if (float.TryParse(thisBikeMpg, out thisOutMpg) &&
                            Int32.TryParse(thisBikeTotal, out thisOutTotalBikes) &&
                            Int32.TryParse(thisBikeYear, out thisOutYear) &&
                            thisOutMpg > 0)
                        {
                            bikeStatsPerYear.Total = thisOutTotalBikes;
                            bikeStatsPerYear.Mpg   = thisOutMpg;
                            bikeStatsPerYear.Year  = thisOutYear;
                            bike.Totals.Add(bikeStatsPerYear);
                        }
                        else
                        {
                            Console.WriteLine("Count and/or rounded MPG did not parse to int.");
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                allBikes.Add(bike);
            }

            //display bike information
            foreach (Bike bike in allBikes)
            {
                Console.WriteLine(bike.MakeAndModel);
                Console.WriteLine("Average MPG across all years: " + bike.CalculateTotalMpg().ToString() + " MPG, With a range of: " + bike.GetMpgRange());
                Console.WriteLine("Total bikes used in calculation: " + bike.GetTotalBikes().ToString());
                Console.WriteLine("Model years span from: " + bike.GetYearsRange());
                Console.ReadLine();
            }
        }