-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
107 lines (88 loc) · 4.02 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Excercise_1
{
class Program
{
public static void RunMissions(IList<IMission> missions, double val)
{
foreach (var m in missions)
{
Console.WriteLine($"{m.Name}({val}) = {m.Calculate(val)}\n");
}
}
public static void PrintAvailableFunctions(FunctionsContainer container)
{
var fuctionListNames = container.getAllMissions();
Console.WriteLine("All Available Functions:");
foreach (var funcName in fuctionListNames)
{
Console.WriteLine(funcName);
}
Console.WriteLine("####################################\n");
}
public static void Main(string[] args)
{
FunctionsContainer funcList = new FunctionsContainer(); // Creating the mission conatiner
funcList["Double"] = val => val * 2; // Double the Value
funcList["Triple"] = val => val * 3; // Triple the Value
funcList["Square"] = val => val * val; // Square the Value
funcList["Sqrt"] = val => Math.Sqrt(val); // Taking the square root
funcList["Plus2"] = val => val + 2; // Double the Value
PrintAvailableFunctions(funcList);
// This handler will output the screen every mission that was activated and it's value
EventHandler<double> LogHandler = (sender, val) =>
{
IMission mission = sender as IMission;
if (mission != null)
{
Console.WriteLine($"Mission of Type: {mission.Type} with the Name {mission.Name} returned {val}");
}
};
EventHandler<double> SqrtHandler = (sender, val) =>
{
// This function will Create a sqrt mission and will continue to sqrt until a number less than 2
SingleMission sqrtMission = new SingleMission(funcList["Sqrt"], "SqrtMission");
double newVal;
do
{
newVal = sqrtMission.Calculate(val); // getting the new Val
Console.WriteLine($"sqrt({val}) = {newVal}");
val = newVal; // Storing the new Val;
} while (val > 2);
Console.WriteLine("----------------------------------------");
};
ComposedMission mission1 = new ComposedMission("mission1")
.Add(funcList["Square"])
.Add(funcList["Sqrt"]);
ComposedMission mission2 = new ComposedMission("mission2")
.Add(funcList["Triple"])
.Add(funcList["Plus2"])
.Add(funcList["Square"]);
SingleMission mission3 = new SingleMission(funcList["Double"], "mission3");
ComposedMission mission4 = new ComposedMission("mission4")
.Add(funcList["Triple"])
.Add(funcList["Stam"]) // Notice that this function does not exist and still it works
.Add(funcList["Plus2"]);
PrintAvailableFunctions(funcList);
funcList["Stam"] = val => val + 100;
SingleMission mission5 = new SingleMission(funcList["Stam"], "mission5");
var missionList = new List<IMission>() { mission1, mission2, mission3, mission4, mission5 };
foreach (var m in missionList)
{
m.OnCalculate += LogHandler;
m.OnCalculate += SqrtHandler;
}
missionList.Add(mission2);
missionList.Add(mission1);
missionList.Add(mission3);
missionList.Add(mission5);
RunMissions(missionList, 100);
RunMissions(missionList, 2);
PrintAvailableFunctions(funcList);
}
}
}