Advertisement
zamcsaba

CleanCode Erettsegi LOL

Apr 21st, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace Erettsegi1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<Record> records = new List<Record>();
  14.  
  15.             // 1. Read file contents
  16.             foreach (string line in File.ReadAllLines("jarmu.txt"))
  17.                 records.Add(new Record(line.Split(' ')));
  18.  
  19.             // 2. Worktime
  20.             Console.WriteLine("2. feladat:\n{0}", records[records.Count - 1].Hour - records[0].Hour);
  21.  
  22.             // 3. First occ. by hours
  23.             Console.WriteLine("\n3. feladat:");
  24.             int currentHour = records[0].Hour;
  25.             foreach (KeyValuePair<int, Record> kp in records.Where(i => { if (i.Hour == currentHour) { currentHour++; return true; } return false; }).ToDictionary(i => i.Hour))
  26.                 Console.WriteLine("{0} óra: {1}", kp.Key.ToString(), kp.Value.PlateNumber);
  27.  
  28.             // 4. Categories
  29.             Console.WriteLine("\n4. feladat:");
  30.             Console.WriteLine("Autóbusz: {0}", records.Where(i => i.PlateNumber.ToCharArray()[0] == 'B').Count());
  31.             Console.WriteLine("Kamion: {0}", records.Where(i => i.PlateNumber.ToCharArray()[0] == 'K').Count());
  32.             Console.WriteLine("Motor: {0}", records.Where(i => i.PlateNumber.ToCharArray()[0] == 'M').Count());
  33.  
  34.             // 5. Longest quiet period
  35.             Console.WriteLine("\n5. feladat:");
  36.             Console.WriteLine(getLongestQuietPeriod(records));
  37.  
  38.             // 6. Search
  39.             Console.WriteLine("\n6. feladat:");
  40.             string search = Console.ReadLine();
  41.             foreach (Record r in records.Where(getMatchingCars(search)))
  42.                 Console.WriteLine(r.PlateNumber);
  43.  
  44.             // 7. Get observed cars
  45.             Console.WriteLine("\n7. feladat:");
  46.             File.WriteAllLines("vizsgalt.txt", getRecordListData(getObservedCars(records)));
  47.  
  48.             Console.ReadLine();
  49.         }
  50.  
  51.         static string getLongestQuietPeriod(List<Record> records)
  52.         {
  53.             long maxdiff = 0;
  54.             DateTime minTime = DateTime.MinValue, maxTime = DateTime.MinValue;
  55.             for (int i = 1; i < records.Count; i++)
  56.             {
  57.                 var diff = records[i].GetTime().Ticks - records[i - 1].GetTime().Ticks;
  58.  
  59.                 if (diff > maxdiff)
  60.                 {
  61.                     minTime = records[i - 1].GetTime();
  62.                     maxTime = records[i].GetTime();
  63.                     maxdiff = diff;
  64.                 }
  65.             }
  66.  
  67.             return String.Format("{0} - {1}", minTime.ToString("HH:mm:ss"), maxTime.ToString("HH:mm:ss"));
  68.         }
  69.  
  70.         static Func<Record, Boolean> getMatchingCars(string query)
  71.         {
  72.             return new Func<Record, bool>(i => {
  73.                 return new Regex(Regex.Escape(query).Replace(@"\*", ".*")).IsMatch(i.PlateNumber);
  74.             });
  75.         }
  76.  
  77.         static List<Record> getObservedCars(List<Record> records)
  78.         {
  79.             List<Record> results = new List<Record>();
  80.             results.Add(records[0]);
  81.  
  82.             long lastObservedTime = records[0].GetTime().Ticks;
  83.             long fiveMins = DateTime.MinValue.AddMinutes(5).Ticks;
  84.             for(int i = 1; i < records.Count; i++)
  85.             {
  86.                 if (records[i].GetTime().Ticks - lastObservedTime >= fiveMins)
  87.                 {
  88.                     results.Add(records[i]);
  89.                     lastObservedTime = records[i].GetTime().Ticks;
  90.                 }
  91.             }
  92.  
  93.             return results;
  94.         }
  95.        
  96.         static List<String> getRecordListData(List<Record> records)
  97.         {
  98.             List<String> results = new List<string>();
  99.             foreach (Record r in records)
  100.                 results.Add(String.Format("{0} {1}", r.GetTime().ToString("HH mm ss"), r.PlateNumber));
  101.             return results;
  102.         }
  103.     }
  104.  
  105.     public class Record
  106.     {
  107.         public int Hour;
  108.         public int Min;
  109.         public int Sec;
  110.         public string PlateNumber;
  111.        
  112.         public Record(string[] data)
  113.         {
  114.             Hour = int.Parse(data[0]);
  115.             Min = int.Parse(data[1]);
  116.             Sec = int.Parse(data[2]);
  117.             PlateNumber = data[3];
  118.         }
  119.  
  120.         public DateTime GetTime()
  121.         {
  122.             return new DateTime(1970, 1, 1, Hour, Min, Sec);
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement