-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
151 lines (131 loc) · 4.53 KB
/
Copy pathProgram.cs
File metadata and controls
151 lines (131 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Threading;
namespace ConsoleStopwatch
{
// Delegate for stopwatch events
public delegate void StopwatchEventHandler(string message);
public class Stopwatch
{
private int _timeElapsedSeconds; // Time in seconds
private int _timeElapsedMilliseconds; // Time in milliseconds
private bool _isRunning;
private Timer _timer;
// Events
public event StopwatchEventHandler OnStarted;
public event StopwatchEventHandler OnStopped;
public event StopwatchEventHandler OnReset;
public Stopwatch()
{
_timeElapsedSeconds = 0;
_timeElapsedMilliseconds = 0;
_isRunning = false;
}
// Method to start the stopwatch
public void Start()
{
if (!_isRunning)
{
_isRunning = true;
_timer = new Timer(Tick, null, 0, 10); // Tick every 10 milliseconds
OnStarted?.Invoke("Stopwatch Started!");
}
else
{
Console.WriteLine("Stopwatch is already running!");
}
}
// Method to stop the stopwatch
public void Stop()
{
if (_isRunning)
{
_isRunning = false;
_timer?.Dispose();
OnStopped?.Invoke("Stopwatch Stopped!");
}
else
{
Console.WriteLine("Stopwatch is not running!");
}
}
// Method to reset the stopwatch
public void Reset()
{
_isRunning = false;
_timer?.Dispose();
_timeElapsedSeconds = 0;
_timeElapsedMilliseconds = 0;
OnReset?.Invoke("Stopwatch Reset!");
}
// Tick method to increment time
private void Tick(object state)
{
_timeElapsedMilliseconds += 10;
if (_timeElapsedMilliseconds >= 1000)
{
_timeElapsedMilliseconds = 0;
_timeElapsedSeconds++;
}
}
// Get the formatted elapsed time
public string GetFormattedTime()
{
int minutes = _timeElapsedSeconds / 60;
int seconds = _timeElapsedSeconds % 60;
return $"{minutes}:{seconds:D2}:{_timeElapsedMilliseconds / 10:D2}";
}
}
class Program
{
static void Main(string[] args)
{
Stopwatch stopwatch = new Stopwatch();
// Subscribe to events
stopwatch.OnStarted += HandleStopwatchEvent;
stopwatch.OnStopped += HandleStopwatchEvent;
stopwatch.OnReset += HandleStopwatchEvent;
bool exit = false;
Console.WriteLine("Console Stopwatch Application");
Console.WriteLine("Press S to Start, T to Stop, R to Reset, Q to Quit");
while (!exit)
{
if (stopwatch.GetFormattedTime() != "0:00:00")
{
Console.Write($"\rTime Elapsed: {stopwatch.GetFormattedTime()}");
}
if (Console.KeyAvailable)
{
var key = Console.ReadKey(intercept: true).Key;
switch (key)
{
case ConsoleKey.S:
stopwatch.Start();
break;
case ConsoleKey.T:
stopwatch.Stop();
break;
case ConsoleKey.R:
stopwatch.Reset();
break;
case ConsoleKey.Q:
exit = true;
stopwatch.Stop();
break;
default:
// Display invalid input message and prompt user to enter a valid option
Console.WriteLine("\nInvalid input! Please enter S, T, R, or Q.");
Console.WriteLine("Press S to Start, T to Stop, R to Reset, Q to Quit");
break;
}
}
Thread.Sleep(10); // Small delay to allow the timer to increment
}
Console.WriteLine("\nExiting Stopwatch...");
}
// Event handler method
static void HandleStopwatchEvent(string message)
{
Console.WriteLine($"\n{message}");
}
}
}