Skip to content

Commit 7fc090f

Browse files
authored
Merge pull request #13 from MicrosoftLearning/lp6-m2-eric
Lp6 m2 eric
2 parents 52a18f0 + 088c5a5 commit 7fc090f

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class Counter
2+
{
3+
public int Total { get; private set; }
4+
public int Threshold { get; set; }
5+
6+
public Counter(int threshold)
7+
{
8+
Threshold = threshold;
9+
Total = 0;
10+
}
11+
12+
public void Add(int value)
13+
{
14+
Total += value;
15+
Console.WriteLine($"Current Total: {Total}"); // Debugging output
16+
if (Total >= Threshold)
17+
{
18+
var args = new ThresholdReachedEventArgs
19+
{
20+
Threshold = Threshold,
21+
TimeReached = DateTime.Now
22+
};
23+
OnThresholdReached(args);
24+
}
25+
}
26+
27+
public event EventHandler<ThresholdReachedEventArgs> ThresholdReached = delegate { };
28+
29+
protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
30+
{
31+
ThresholdReached?.Invoke(this, e);
32+
}
33+
}
34+
35+
public class ThresholdReachedEventArgs : EventArgs
36+
{
37+
public int Threshold { get; set; }
38+
public DateTime TimeReached { get; set; }
39+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var counter = new Counter(10);
2+
3+
// Subscribe to the ThresholdReached event
4+
counter.ThresholdReached += Counter_ThresholdReached;
5+
6+
// Increment the counter interactively
7+
Console.WriteLine("Press 'a' to add 1 to the counter or 'q' to quit.");
8+
while (true)
9+
{
10+
var key = Console.ReadKey(true).KeyChar;
11+
if (key == 'a')
12+
{
13+
counter.Add(1);
14+
}
15+
else if (key == 'q')
16+
{
17+
break;
18+
}
19+
}
20+
21+
// Unsubscribe from the ThresholdReached event
22+
counter.ThresholdReached -= Counter_ThresholdReached;
23+
24+
static void Counter_ThresholdReached(object? sender, ThresholdReachedEventArgs e)
25+
{
26+
Console.WriteLine($"Threshold of {e.Threshold} reached at {e.TimeReached}.");
27+
}

0 commit comments

Comments
 (0)