From 70f502763f2512f915b60d70df959838806f4f07 Mon Sep 17 00:00:00 2001 From: Yusuf Praditya <47532266+yusufpraditya@users.noreply.github.com> Date: Wed, 13 Sep 2023 11:29:24 +0700 Subject: [PATCH] Add W2 D3 projects (not finished yet) --- FormulatrixBootcamp.sln | 30 ++++++++ W2 D2/DelegatePubSub/DelegatePubSub.csproj | 10 +++ W2 D2/DelegatePubSub/Program.cs | 15 ++++ W2 D2/DelegatePubSub/Subscriber.cs | 8 +++ W2 D2/DelegatePubSub/Youtuber.cs | 35 ++++++++++ .../DelegateReturnValue.csproj | 10 +++ W2 D2/DelegateReturnValue/Program.cs | 43 ++++++++++++ W2 D2/EventHandler/EventHandler.csproj | 10 +++ W2 D2/EventHandler/Program.cs | 2 + W2 D3/EventHandler/EventHandler.csproj | 10 +++ W2 D3/EventHandler/Program.cs | 12 ++++ W2 D3/EventHandler/Subscriber.cs | 16 +++++ W2 D3/EventHandler/Youtuber.cs | 54 ++++++++++++++ W2 D3/EventHandlerGeneric/EventData.cs | 6 ++ .../EventHandlerGeneric.csproj | 10 +++ W2 D3/EventHandlerGeneric/Program.cs | 16 +++++ W2 D3/EventHandlerGeneric/Subscriber.cs | 27 +++++++ W2 D3/EventHandlerGeneric/Youtuber.cs | 70 +++++++++++++++++++ 18 files changed, 384 insertions(+) create mode 100644 W2 D2/DelegatePubSub/DelegatePubSub.csproj create mode 100644 W2 D2/DelegatePubSub/Program.cs create mode 100644 W2 D2/DelegatePubSub/Subscriber.cs create mode 100644 W2 D2/DelegatePubSub/Youtuber.cs create mode 100644 W2 D2/DelegateReturnValue/DelegateReturnValue.csproj create mode 100644 W2 D2/DelegateReturnValue/Program.cs create mode 100644 W2 D2/EventHandler/EventHandler.csproj create mode 100644 W2 D2/EventHandler/Program.cs create mode 100644 W2 D3/EventHandler/EventHandler.csproj create mode 100644 W2 D3/EventHandler/Program.cs create mode 100644 W2 D3/EventHandler/Subscriber.cs create mode 100644 W2 D3/EventHandler/Youtuber.cs create mode 100644 W2 D3/EventHandlerGeneric/EventData.cs create mode 100644 W2 D3/EventHandlerGeneric/EventHandlerGeneric.csproj create mode 100644 W2 D3/EventHandlerGeneric/Program.cs create mode 100644 W2 D3/EventHandlerGeneric/Subscriber.cs create mode 100644 W2 D3/EventHandlerGeneric/Youtuber.cs diff --git a/FormulatrixBootcamp.sln b/FormulatrixBootcamp.sln index 7b2df5d..50befb4 100644 --- a/FormulatrixBootcamp.sln +++ b/FormulatrixBootcamp.sln @@ -71,6 +71,16 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NonGenericCollection", "W2 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Delegate", "W2 D2\Delegate\Delegate.csproj", "{8CBF3A9D-D792-4ECC-92A3-0E2782673234}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DelegatePubSub", "W2 D2\DelegatePubSub\DelegatePubSub.csproj", "{22303F7C-8C80-4381-AA31-D1A260E37F2F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DelegateReturnValue", "W2 D2\DelegateReturnValue\DelegateReturnValue.csproj", "{48A4309F-4E43-419D-8ECD-E8A441359E83}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "W2 D3", "W2 D3", "{82AE1F6C-47FF-4067-89F3-87EBEFDA524C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventHandler", "W2 D3\EventHandler\EventHandler.csproj", "{D5027177-A81C-44F1-9F12-7C7CDFB6C458}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventHandlerGeneric", "W2 D3\EventHandlerGeneric\EventHandlerGeneric.csproj", "{6A2EF45B-F734-4A5F-8D67-90443EB39056}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -189,6 +199,22 @@ Global {8CBF3A9D-D792-4ECC-92A3-0E2782673234}.Debug|Any CPU.Build.0 = Debug|Any CPU {8CBF3A9D-D792-4ECC-92A3-0E2782673234}.Release|Any CPU.ActiveCfg = Release|Any CPU {8CBF3A9D-D792-4ECC-92A3-0E2782673234}.Release|Any CPU.Build.0 = Release|Any CPU + {22303F7C-8C80-4381-AA31-D1A260E37F2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {22303F7C-8C80-4381-AA31-D1A260E37F2F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {22303F7C-8C80-4381-AA31-D1A260E37F2F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {22303F7C-8C80-4381-AA31-D1A260E37F2F}.Release|Any CPU.Build.0 = Release|Any CPU + {48A4309F-4E43-419D-8ECD-E8A441359E83}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {48A4309F-4E43-419D-8ECD-E8A441359E83}.Debug|Any CPU.Build.0 = Debug|Any CPU + {48A4309F-4E43-419D-8ECD-E8A441359E83}.Release|Any CPU.ActiveCfg = Release|Any CPU + {48A4309F-4E43-419D-8ECD-E8A441359E83}.Release|Any CPU.Build.0 = Release|Any CPU + {D5027177-A81C-44F1-9F12-7C7CDFB6C458}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D5027177-A81C-44F1-9F12-7C7CDFB6C458}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D5027177-A81C-44F1-9F12-7C7CDFB6C458}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D5027177-A81C-44F1-9F12-7C7CDFB6C458}.Release|Any CPU.Build.0 = Release|Any CPU + {6A2EF45B-F734-4A5F-8D67-90443EB39056}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6A2EF45B-F734-4A5F-8D67-90443EB39056}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6A2EF45B-F734-4A5F-8D67-90443EB39056}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6A2EF45B-F734-4A5F-8D67-90443EB39056}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -222,5 +248,9 @@ Global {367138CF-7021-4354-B364-6A674AA8E4D5} = {747633C8-D663-4492-9F60-E01A9F09A1CA} {51504E3A-4A98-45D9-B64B-C657E7CC95B9} = {747633C8-D663-4492-9F60-E01A9F09A1CA} {8CBF3A9D-D792-4ECC-92A3-0E2782673234} = {747633C8-D663-4492-9F60-E01A9F09A1CA} + {22303F7C-8C80-4381-AA31-D1A260E37F2F} = {747633C8-D663-4492-9F60-E01A9F09A1CA} + {48A4309F-4E43-419D-8ECD-E8A441359E83} = {747633C8-D663-4492-9F60-E01A9F09A1CA} + {D5027177-A81C-44F1-9F12-7C7CDFB6C458} = {82AE1F6C-47FF-4067-89F3-87EBEFDA524C} + {6A2EF45B-F734-4A5F-8D67-90443EB39056} = {82AE1F6C-47FF-4067-89F3-87EBEFDA524C} EndGlobalSection EndGlobal diff --git a/W2 D2/DelegatePubSub/DelegatePubSub.csproj b/W2 D2/DelegatePubSub/DelegatePubSub.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/W2 D2/DelegatePubSub/DelegatePubSub.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/W2 D2/DelegatePubSub/Program.cs b/W2 D2/DelegatePubSub/Program.cs new file mode 100644 index 0000000..5c31360 --- /dev/null +++ b/W2 D2/DelegatePubSub/Program.cs @@ -0,0 +1,15 @@ +using DelegatePubSub; +public delegate void Publish(string x); +public class Program +{ + static void Main() + { + Youtuber yt = new(); + Subscriber sub1 = new(); + + yt.AddSubscriber(sub1.Notify); + yt.SendNotification(); + + yt.RemoveSubscriber(sub1.Notify); + } +} \ No newline at end of file diff --git a/W2 D2/DelegatePubSub/Subscriber.cs b/W2 D2/DelegatePubSub/Subscriber.cs new file mode 100644 index 0000000..b3f7129 --- /dev/null +++ b/W2 D2/DelegatePubSub/Subscriber.cs @@ -0,0 +1,8 @@ +namespace DelegatePubSub; +public class Subscriber +{ + public void Notify(string message) + { + Console.WriteLine(message); + } +} diff --git a/W2 D2/DelegatePubSub/Youtuber.cs b/W2 D2/DelegatePubSub/Youtuber.cs new file mode 100644 index 0000000..da8d937 --- /dev/null +++ b/W2 D2/DelegatePubSub/Youtuber.cs @@ -0,0 +1,35 @@ +namespace DelegatePubSub; +public class Youtuber +{ + private Publish? _sub; + public void SendNotification() + { + if (_sub != null) + { + _sub.Invoke("Notify"); + } + } + + public void AddSubscriber(Publish sub) + { + if (_sub == null || !_sub.GetInvocationList().Contains(sub)) + { + _sub += sub; + } + else + { + Console.WriteLine("Error: Subscriber already exist."); + } + } + + public void RemoveSubscriber(Publish sub) + { + if (_sub != null) + { + _sub -= sub; + } else + { + Console.WriteLine("Error: No subscriber."); + } + } +} diff --git a/W2 D2/DelegateReturnValue/DelegateReturnValue.csproj b/W2 D2/DelegateReturnValue/DelegateReturnValue.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/W2 D2/DelegateReturnValue/DelegateReturnValue.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/W2 D2/DelegateReturnValue/Program.cs b/W2 D2/DelegateReturnValue/Program.cs new file mode 100644 index 0000000..82208b5 --- /dev/null +++ b/W2 D2/DelegateReturnValue/Program.cs @@ -0,0 +1,43 @@ +public delegate decimal Calculator(decimal a, decimal b); +public class Program { + static void Main() + { + Calculator calc = Add; + calc += Subtract; + calc += Multiply; + calc += Divide; + + Delegate[] del = calc.GetInvocationList(); + List results = new List(); + + foreach (Calculator d in del) + { + results.Add(d.Invoke(5.1M, 5.1M)); + } + + foreach (decimal result in results) + { + Console.WriteLine(result); + } + } + + static decimal Add(decimal a, decimal b) + { + return a + b; + } + + static decimal Subtract(decimal a, decimal b) + { + return a - b; + } + + static decimal Multiply(decimal a, decimal b) + { + return a * b; + } + + static decimal Divide(decimal a, decimal b) + { + return a / b; + } +} \ No newline at end of file diff --git a/W2 D2/EventHandler/EventHandler.csproj b/W2 D2/EventHandler/EventHandler.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/W2 D2/EventHandler/EventHandler.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/W2 D2/EventHandler/Program.cs b/W2 D2/EventHandler/Program.cs new file mode 100644 index 0000000..3751555 --- /dev/null +++ b/W2 D2/EventHandler/Program.cs @@ -0,0 +1,2 @@ +// See https://aka.ms/new-console-template for more information +Console.WriteLine("Hello, World!"); diff --git a/W2 D3/EventHandler/EventHandler.csproj b/W2 D3/EventHandler/EventHandler.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/W2 D3/EventHandler/EventHandler.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/W2 D3/EventHandler/Program.cs b/W2 D3/EventHandler/Program.cs new file mode 100644 index 0000000..29a3566 --- /dev/null +++ b/W2 D3/EventHandler/Program.cs @@ -0,0 +1,12 @@ +using EventHandlerExample; +public class Program +{ + static void Main() + { + Youtuber yt1 = new Youtuber("Windah Basudara"); + Subscriber sub1 = new Subscriber("Yusuf"); + + yt1.AddSubscriber(sub1.GetNotification); + yt1.UploadVideo(); + } +} \ No newline at end of file diff --git a/W2 D3/EventHandler/Subscriber.cs b/W2 D3/EventHandler/Subscriber.cs new file mode 100644 index 0000000..7e3822d --- /dev/null +++ b/W2 D3/EventHandler/Subscriber.cs @@ -0,0 +1,16 @@ +namespace EventHandlerExample; +public class Subscriber +{ + private string? _name; + + public Subscriber(string name) + { + _name = name; + } + + public void GetNotification(object? sender, EventArgs e) + { + + Console.WriteLine($"{_name} is getting notification from Youtuber ({sender})."); + } +} diff --git a/W2 D3/EventHandler/Youtuber.cs b/W2 D3/EventHandler/Youtuber.cs new file mode 100644 index 0000000..970685b --- /dev/null +++ b/W2 D3/EventHandler/Youtuber.cs @@ -0,0 +1,54 @@ +namespace EventHandlerExample; +public class Youtuber +{ + private EventHandler? _sub; + private string _name; + + public Youtuber(string name) + { + _name = name; + } + + public void AddSubscriber(EventHandler sub) + { + if (_sub == null || !_sub.GetInvocationList().Contains(sub)) + { + _sub += sub; + } + else + { + Console.WriteLine("Error: Subscriber already exist."); + } + } + + public void RemoveSubscriber(EventHandler sub) + { + if (_sub != null) + { + _sub -= sub; + } + else + { + Console.WriteLine("Error: No subscriber."); + } + } + + public void UploadVideo() + { + Console.WriteLine("Video is being uploaded.."); + Console.WriteLine("Video uploaded."); + SendNotification(); + } + + private void SendNotification() + { + if (_sub != null) + { + _sub.Invoke(this, EventArgs.Empty); + } + } + public override string ToString() + { + return _name; + } +} diff --git a/W2 D3/EventHandlerGeneric/EventData.cs b/W2 D3/EventHandlerGeneric/EventData.cs new file mode 100644 index 0000000..72a1d90 --- /dev/null +++ b/W2 D3/EventHandlerGeneric/EventData.cs @@ -0,0 +1,6 @@ +namespace EventHandlerGeneric; +public class EventData : EventArgs +{ + public string? message; + public int viewCount; +} diff --git a/W2 D3/EventHandlerGeneric/EventHandlerGeneric.csproj b/W2 D3/EventHandlerGeneric/EventHandlerGeneric.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/W2 D3/EventHandlerGeneric/EventHandlerGeneric.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/W2 D3/EventHandlerGeneric/Program.cs b/W2 D3/EventHandlerGeneric/Program.cs new file mode 100644 index 0000000..2715ba4 --- /dev/null +++ b/W2 D3/EventHandlerGeneric/Program.cs @@ -0,0 +1,16 @@ +using EventHandlerGeneric; + +public class Program +{ + static void Main() + { + Youtuber yt = new Youtuber("Windah Basudara"); + Subscriber sub1 = new Subscriber("Yusuf"); + + yt.AddSubscriber(sub1.GetNotification); + yt.UploadVideo("Game Horror"); + + yt.AddViewCount(sub1.WatchVideo); + + } +} \ No newline at end of file diff --git a/W2 D3/EventHandlerGeneric/Subscriber.cs b/W2 D3/EventHandlerGeneric/Subscriber.cs new file mode 100644 index 0000000..5a87886 --- /dev/null +++ b/W2 D3/EventHandlerGeneric/Subscriber.cs @@ -0,0 +1,27 @@ +namespace EventHandlerGeneric; +public class Subscriber +{ + private string? _name; + EventData data = new(); + + public Subscriber(string name) + { + _name = name; + } + + public void WatchVideo(object? sender, EventData e) + { + Console.WriteLine($"{_name} is watching youtube video"); + data.viewCount += 1; + } + + public void GetNotification(object? sender, EventData e) + { + Console.WriteLine(e.message); + } + + // public void GetViewCount(object? sender, EventData e) + // { + // Console.WriteLine($"Youtube view count: {e.viewCount}"); + // } +} diff --git a/W2 D3/EventHandlerGeneric/Youtuber.cs b/W2 D3/EventHandlerGeneric/Youtuber.cs new file mode 100644 index 0000000..df1665f --- /dev/null +++ b/W2 D3/EventHandlerGeneric/Youtuber.cs @@ -0,0 +1,70 @@ +namespace EventHandlerGeneric; +public class Youtuber +{ + private EventHandler? _sub; + private EventHandler? _viewCount; + private string? _name; + + public Youtuber(string name) + { + _name = name; + } + + public void GetViewCount() + { + + } + + + + public void UploadVideo(string title) + { + Console.WriteLine($"Video with title {title} is being uploaded.."); + Console.WriteLine("Video uploaded."); + SendNotification(title); + } + + private void SendNotification(string title) + { + if (_sub != null) + { + _sub.Invoke(this, new EventData {message = $"Youtube Notification: {this} - {title}"}); + } + } + + public void AddSubscriber(EventHandler? sub) + { + if (_sub == null || !_sub.GetInvocationList().Contains(sub)) + { + _sub += sub; + } + else + { + Console.WriteLine("Error: Subscriber already exist."); + } + } + + public void RemoveSubscriber(EventHandler? sub) + { + if (_sub != null) + { + _sub -= sub; + } + else + { + Console.WriteLine("Error: No subscriber."); + } + } + + public void AddViewCount(EventHandler? viewCount) + { + if (_viewCount == null || !_viewCount.GetInvocationList().Contains(viewCount)) + { + _viewCount += viewCount; + } + } + public override string? ToString() + { + return _name; + } +}