Advertisement
2629881

Untitled

Feb 22nd, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 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 Excercises
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var stopwatch = new Stopwatch();
  14.             Console.WriteLine("Type start\\stop\\clear to use the stopwatch, x to exit");
  15.  
  16.             while (true)
  17.             {
  18.                 var input = Console.ReadLine();
  19.  
  20.                 switch (input)
  21.                 {
  22.                     case "start":
  23.                         stopwatch.Start();                        
  24.                         break;
  25.                     case "stop":
  26.                         stopwatch.Stop();    
  27.                         break;
  28.                     case "x":
  29.                         return;
  30.                 }
  31.             }                  
  32.         }
  33.     }
  34.  
  35.     public class Stopwatch
  36.     {
  37.         private DateTime _startTime;
  38.         private DateTime _stopTime;
  39.         private TimeSpan _duration;
  40.         private string _StrDuration;
  41.         private bool _startOn = false;
  42.  
  43.         public Stopwatch()
  44.         {
  45.             Console.WriteLine($"Stopwatch displays: {_duration}");
  46.         }
  47.  
  48.         public DateTime Start()
  49.         {
  50.             if (this._startOn == true)
  51.             {
  52.                 throw new System.InvalidOperationException("Can not start twice in a row");
  53.             }
  54.             else
  55.             {
  56.                 this._startOn = true;
  57.                 this._startTime = DateTime.Now;
  58.                 Console.WriteLine("Stopwatch running, type \"stop\" to stop");
  59.                 return this._startTime;
  60.             }
  61.         }
  62.  
  63.         public void Stop()
  64.         {
  65.             this._stopTime = DateTime.Now;
  66.             this._duration = _stopTime - _startTime;
  67.             _StrDuration = _duration.ToString();
  68.             Console.WriteLine(_StrDuration);
  69.             this._startOn = false;
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement