Advertisement
TzvetanIG

Average-Load-Time-Calculator

Apr 1st, 2014
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Threading;
  5.  
  6. class AverageLoadTimeCalculator
  7. {
  8.     static void Main()
  9.     {
  10.         Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  11.  
  12.         //string[] inputStrings =
  13.         //{
  14.         //    "2014-Mar-02 11:33 http://softuni.bg 8.37725",
  15.         //    "2014-Mar-02 11:34 http://www.google.com 1.335",
  16.         //    "2014-Mar-03 21:03 http://softuni.bg 7.25",
  17.         //    "2014-Mar-03 22:00 http://www.google.com 2.44",
  18.         //    "2014-Mar-03 22:01 http://www.google.com 2.45",
  19.         //    "2014-Mar-03 22:01 http://www.google.com 2.77"
  20.         //};
  21.  
  22.         string[] inputStrings =
  23.         {
  24.             "2014-Apr-01 02:01 http://softuni.bg 8.37725",
  25.             "2014-Apr-01 02:05 http://www.nakov.com 11.622",
  26.             "2014-Apr-01 02:06 http://softuni.bg 4.33",
  27.             "2014-Apr-01 02:11 http://www.google.com 1.94",
  28.             "2014-Apr-01 02:11 http://www.google.com 2.011",
  29.             "2014-Apr-01 02:12 http://www.google.com 4.882",
  30.             "2014-Apr-01 02:34 http://softuni.bg 4.885",
  31.             "2014-Apr-01 02:36 http://www.nakov.com 10.74",
  32.             "2014-Apr-01 02:36 http://www.nakov.com 11.75",
  33.             "2014-Apr-01 02:38 http://softuni.bg 3.886",
  34.             "2014-Apr-01 02:44 http://www.google.com 1.04",
  35.             "2014-Apr-01 02:48 http://www.google.com 1.4555",
  36.             "2014-Apr-01 02:55 http://www.google.com 1.977"
  37.         };
  38.  
  39.         var sites = new List<Site>();
  40.      
  41.         foreach (var str in inputStrings)
  42.         {
  43.             string[] inputData = str.Split(' ');
  44.  
  45.             if (!sites.Exists(s => s.url == inputData[2]))
  46.             {
  47.                 sites.Add(new Site(inputData[2]));
  48.             }
  49.  
  50.             var site = sites.Find(s => s.url == inputData[2]);
  51.             site.GetDateTime(inputData[0] + " " + inputData[1]);
  52.             site.SumLoadTime(double.Parse(inputData[3]));
  53.         }
  54.  
  55.         foreach (var item in sites)
  56.         {
  57.             item.PrintInfo();
  58.         }
  59.  
  60.     }
  61. }
  62.  
  63. class Site
  64. {
  65.     public List<DateTime> date = new List<DateTime>();
  66.     public string url = "";
  67.     private double totalLoadTime = 0;
  68.     private int count = 0;
  69.  
  70.     public Site(string url)
  71.     {
  72.         this.url = url;
  73.     }
  74.  
  75.     public double AverageLoadTime
  76.     {
  77.         get { return this.totalLoadTime / count; }
  78.     }//end AverageLoadTime
  79.  
  80.     public void PrintInfo()
  81.     {
  82.         Console.WriteLine("{0} -> {1}", this.url, this.AverageLoadTime);
  83.     }//end PrintInfo
  84.  
  85.     public void GetDateTime(string dateAsString)
  86.     {
  87.         this.date.Add(DateTime.Parse(dateAsString));
  88.     }// end GetDateTime
  89.  
  90.     public void SumLoadTime(double loadTime)
  91.     {
  92.         this.totalLoadTime += loadTime;
  93.         this.count++;
  94.     }//end SumLoadTime
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement