# C# Intermediate course Exercise-1: Please give feedback on my finished solution : Design a Stopwatch

**URL:** <https://forum.codewithmosh.com/t/c-intermediate-course-exercise-1-please-give-feedback-on-my-finished-solution-design-a-stopwatch/14334>\
**Category:** C#\
**Created:** [August 12, 2022, 10:44am UTC](https://forum.codewithmosh.com/t/c-intermediate-course-exercise-1-please-give-feedback-on-my-finished-solution-design-a-stopwatch/14334 "2022-08-12T10:44:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LL12](https://avatars.discourse-cdn.com/v4/letter/l/9de053/32.png) [@LL12](https://forum.codewithmosh.com/u/LL12)\
**Post date:** [August 12, 2022, 10:44am UTC](https://forum.codewithmosh.com/t/c-intermediate-course-exercise-1-please-give-feedback-on-my-finished-solution-design-a-stopwatch/14334/1 "2022-08-12T10:44:16Z")

</div>

It’s pretty frustrating that Josh stopped showing his solutions. Now we have nothing to compare our solution to the ‘ideal’ solution. Could people please review my solution and ideally provide their own, so I can see how others have implemented this solution. Please also feel free to feedback to me on feedbacks, how to make the code clearer and more optimized.

```auto
namespace exe1
{
    public class StopWatch
    {
        private TimeSpan _start;
        private TimeSpan _stop;
        private TimeSpan _started;
        private TimeSpan _stopped;
        private TimeSpan _difference;
        private string? _input;
        private bool IsTimerRunning;

        public StopWatch() 
        {
            IsTimerRunning = false;
            
        }

        public void Interface(string button)
        {
            _input = button.ToLower();

            if (string.IsNullOrEmpty(_input))
            {
                throw new ArgumentException(nameof(_input));
            }
            if(_input == "start" && IsTimerRunning == false)
            {
                IsTimerRunning = true;
                _started = StartTimer();
                Console.WriteLine("Timer Started.");
            }
            else if(_input == "stop" && IsTimerRunning == true)
            {
                IsTimerRunning = false;
                _stopped = StopTimer();
                Console.WriteLine("Timer Stopped.");
                Console.WriteLine("Duration: " + Duration(_started, _stopped));
            } 
            else if(IsTimerRunning)
            {
                throw new InvalidOperationException(nameof(IsTimerRunning));
            }
        }

        private TimeSpan StartTimer()
        {
            _start = new TimeSpan(DateTime.Now.Ticks);
            return _start;
        }

        private TimeSpan StopTimer()
        {
            _stop = new TimeSpan(DateTime.Now.Ticks);
            return _stop;
        }

        private TimeSpan Duration(TimeSpan Start, TimeSpan Stop)
        {
            _difference = Stop.Subtract(Start);
            return _difference;
        }

    }
}

```

```auto

namespace exe1
{

    internal class Program
    {
        static void Main(string[] args)
        {

            var timer = new StopWatch();
            while (true)
            {
                Console.WriteLine("Enter the Word \"Start\" to begin timer, and \"Stop\" to stop timer.");
                var input = Console.ReadLine();
                timer.Interface(input);
            }

        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![LL12](https://avatars.discourse-cdn.com/v4/letter/l/9de053/32.png) [@LL12](https://forum.codewithmosh.com/u/LL12)\
**Post date:** [August 18, 2022, 2:48pm UTC](https://forum.codewithmosh.com/t/c-intermediate-course-exercise-1-please-give-feedback-on-my-finished-solution-design-a-stopwatch/14334/2 "2022-08-18T14:48:00Z")

</div>

No Feedback from anyone?

---

<div class="post-metadata">

**Author:** ![rlared](https://avatars.discourse-cdn.com/v4/letter/r/e5b9ba/32.png) [@rlared](https://forum.codewithmosh.com/u/rlared)\
**Post date:** [March 17, 2023, 2:06pm UTC](https://forum.codewithmosh.com/t/c-intermediate-course-exercise-1-please-give-feedback-on-my-finished-solution-design-a-stopwatch/14334/3 "2023-03-17T14:06:00Z")

</div>

I’m learning coding too so take my feedback with a grain of salt.

I don’t think you need the constructor because I believe bools are initialized to a false value by default.

I made start and stop assign a DateTime (DateTime.Now) instead, since subtracting 2 DateTimes returns a TimeSpan.

I did mine as a console app, here’s my code

````auto
var timer = new Stopwatch();

while(true)
{
    Console.Write("Enter a Command (start, stop, duration, quit): ");
    var input = Console.ReadLine().ToLower();
    if (input == "start")
        timer.Start();
    else if (input == "stop")
        timer.Stop();
    else if (input == "duration")
        timer.Duration();
    else if (input == "quit")
        return;
    else
    {
        Console.WriteLine("Invalid Input, Try Again");        
    }
}

public class Stopwatch
{
    private bool _isRunning;
    private DateTime _startTime;
    private DateTime _endTime;
    private TimeSpan _duration;

    public void Start()
    {
        if (_isRunning) throw new InvalidOperationException("_isRunning");
        _startTime = DateTime.Now;
        _isRunning = true;
    }

    public void Stop()
    {
        if (!_isRunning) throw new InvalidOperationException("_isRunning");
        _endTime = DateTime.Now;
        _isRunning = false;
    }

    public void Duration()
    {
        if (_isRunning) throw new InvalidOperationException("Stopwatch is running");
        _duration = _endTime - _startTime;
        Console.WriteLine("Duration is: " + _duration.ToString(@"hh\:mm\:ss\:FF"));
    }
}```
````
