Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Excercises
- {
- class Program
- {
- static void Main(string[] args)
- {
- var stopwatch = new Stopwatch();
- Console.WriteLine("Type start\\stop\\clear to use the stopwatch, x to exit");
- while (true)
- {
- var input = Console.ReadLine();
- switch (input)
- {
- case "start":
- stopwatch.Start();
- break;
- case "stop":
- stopwatch.Stop();
- break;
- case "x":
- return;
- }
- }
- }
- }
- public class Stopwatch
- {
- private DateTime _startTime;
- private DateTime _stopTime;
- private TimeSpan _duration;
- private string _StrDuration;
- private bool _startOn = false;
- public Stopwatch()
- {
- Console.WriteLine($"Stopwatch displays: {_duration}");
- }
- public DateTime Start()
- {
- if (this._startOn == true)
- {
- throw new System.InvalidOperationException("Can not start twice in a row");
- }
- else
- {
- this._startOn = true;
- this._startTime = DateTime.Now;
- Console.WriteLine("Stopwatch running, type \"stop\" to stop");
- return this._startTime;
- }
- }
- public void Stop()
- {
- this._stopTime = DateTime.Now;
- this._duration = _stopTime - _startTime;
- _StrDuration = _duration.ToString();
- Console.WriteLine(_StrDuration);
- this._startOn = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement