Advertisement
mch_pastebin

GrubasC#stoper

Jul 13th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CSHARP
  8. {
  9.     class Stopwatch
  10.     {
  11.         public bool Active { get; set; }
  12.  
  13.         public bool EverStarted { get; set; }
  14.  
  15.         public DateTime DateStart { get; set; }
  16.  
  17.         private readonly List<decimal> _laps = new List<decimal>();
  18.  
  19.         public void Start()
  20.         {
  21.             if (!EverStarted)
  22.             {
  23.                 EverStarted = true;
  24.                 Active = true;
  25.                 DateStart = DateTime.Now;
  26.             }
  27.             else
  28.                 WrongButton("start");
  29.         }
  30.  
  31.         public void Stop()
  32.         {
  33.             if (Active)
  34.             {
  35.                 Active = false;
  36.                 var diffrence = DateTime.Now - DateStart;
  37.                 Console.WriteLine("{0} - Stopping the watch.", diffrence.TotalSeconds);
  38.             }
  39.             else
  40.                 WrongButton("stop");
  41.         }
  42.  
  43.         public void ClockContinue()
  44.         {
  45.             if (EverStarted && !Active)
  46.                 Active = true;
  47.             else
  48.                 WrongButton("continue");
  49.         }
  50.  
  51.         public void Reset()
  52.         {
  53.             if (EverStarted)
  54.             {
  55.                 _laps.Clear();
  56.                 Active = false;
  57.                 EverStarted = false;
  58.             }
  59.             else
  60.                 WrongButton("reset");
  61.         }
  62.  
  63.         public void Lap()
  64.         {
  65.             if (EverStarted)
  66.             {
  67.                 var diffrence = DateTime.Now - DateStart;
  68.                 _laps.Add(Convert.ToDecimal(diffrence.TotalSeconds));
  69.                 Console.WriteLine("{0} - New lap added.", diffrence.TotalSeconds);
  70.             }
  71.             else
  72.                 WrongButton("lap");
  73.         }
  74.  
  75.         public void PrintLap()
  76.         {
  77.             Console.WriteLine("\nLaps:");
  78.  
  79.             foreach (var lap in _laps)
  80.                 Console.WriteLine(lap);
  81.         }
  82.  
  83.         private void WrongButton(string functionName)
  84.         {
  85.             System.Threading.Thread.Sleep(1000);
  86.             Console.WriteLine($"Wrong button fella ({functionName})");
  87.         }
  88.     }
  89.  
  90.     class Program
  91.     {
  92.         static void Main(string[] args)
  93.         {
  94.             var watch = new Stopwatch();
  95.             var run = true;
  96.  
  97.             while (run)
  98.             {
  99.                 Console.WriteLine("1. Start\n2. Lap\n3. Continue\n4. Stop\n5. Finish");
  100.  
  101.                 switch (Console.ReadKey().KeyChar)
  102.                 {
  103.                     case '1':
  104.                         watch.Start();
  105.                         break;
  106.                     case '2':
  107.                         watch.Lap();
  108.                         break;
  109.                     case '3':
  110.                         watch.ClockContinue();
  111.                         break;
  112.                     case '4':
  113.                         watch.Stop();
  114.                         break;
  115.                     case '5':
  116.                         run = false;
  117.                         break;
  118.                 }
  119.  
  120.                 Console.Clear();
  121.             }
  122.  
  123.             watch.PrintLap();
  124.             Console.ReadKey();
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement