forked from oferzad/EventsAndThreadsLecture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassesForExercise.cs
More file actions
101 lines (89 loc) · 2.65 KB
/
ClassesForExercise.cs
File metadata and controls
101 lines (89 loc) · 2.65 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
using CoreCollectionsAsync;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CoreCollectionsAsync
{
public class Battery
{
const int MAX_CAPACITY = 1000;
private static Random r = new Random();
//Add events to the class to notify upon threshhold reached and shut down!
#region events
#endregion
public event Action ReachedThreshhold;
public event Action Shutdown;
public int Threshold { get; }
public int Capacity { get; set; }
public int Percent
{
get
{
return 100 * Capacity / MAX_CAPACITY;
}
}
public Battery()
{
Capacity = MAX_CAPACITY;
Threshold = 400;
}
public void Usage()
{
Capacity -= r.Next(50, 150);
if(ReachedThreshhold != null&&Capacity<Threshold&& Capacity > 0)
ReachedThreshhold();
if(Shutdown != null&&Capacity<=0)
Shutdown();
//Add calls to the events based on the capacity and threshhold
#region Fire Events
#endregion
}
}
class ElectricCar
{
public Battery Bat { get; set; }
private int id;
//Add event to notify when the car is shut down
public event Action OnCarShutDown;
public ElectricCar(int id)
{
this.id = id;
Bat = new Battery();
Bat.ReachedThreshhold += BThrash;
Bat.Shutdown += BShut;
#region Register to battery events
#endregion
}
public void StartEngine()
{
while (Bat.Capacity > 0)
{
Console.WriteLine($"{this} {Bat.Percent}% Thread: {Thread.CurrentThread.ManagedThreadId}");
Thread.Sleep(1000);
Bat.Usage();
}
}
//Add code to Define and implement the battery event implementations
public void BThrash()
{
if (Bat.Capacity < Bat.Threshold)
{
Console.WriteLine($"threshhold reached, fuck ! fuck! fuck! please do something! we are going to die! car {id} only have {Bat.Percent} remaining");
}
}
public void BShut()
{
Console.WriteLine($"omiya mou shindeiru");
}
#region events implementation
#endregion
public override string ToString()
{
return $"Car: {id}";
}
}
}