Skip to content

Commit

Permalink
Feat: Add more interface methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cvpfus committed Sep 17, 2023
1 parent b4d464e commit 9ba2786
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 17 deletions.
2 changes: 2 additions & 0 deletions MyPractice/Youtube/IWatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ namespace Youtube;
public interface IWatch
{
void WatchVideo(int videoIndex, int times);
void GetVideoList();
void GetTrendingVideo();
}
9 changes: 8 additions & 1 deletion MyPractice/Youtube/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ static void Main()

server.notificationHandler += sub1.GetNotification;
sub1.trendingHandler += server.ViewReached;
yt1.trendingHandler += server.ViewReached;

server.notificationHandler += sub2.GetNotification;
sub2.trendingHandler += server.ViewReached;
yt2.trendingHandler += server.ViewReached;

yt1.UploadVideo("Game Horror");
yt1.UploadVideo("Game Bocil");
Expand All @@ -28,8 +30,13 @@ static void Main()
sub1.WatchVideo(3, 1000000);
sub1.GetTrendingVideo();

sub2.GetVideoList();
sub2.WatchVideo(4, 110000);
sub2.GetTrendingVideo();

yt1.WatchVideo(2, 100000000);
yt1.GetTrendingVideo();

yt2.WatchVideo(2, 100000000);
yt2.GetTrendingVideo();
}
}
2 changes: 2 additions & 0 deletions MyPractice/Youtube/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ public class Server
private int _trendingThreshold = 1000000;
public DataEventArgs data = new();
public EventHandler<DataEventArgs>? notificationHandler;

public int TrendingThreshold { get => _trendingThreshold; }

public List<Trending> GetTrendingList()
{
Expand Down
26 changes: 17 additions & 9 deletions MyPractice/Youtube/Subscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@ public Subscriber(string name, Server server)

public void WatchVideo(int videoIndex, int times)
{
_server.data.videoIndex = videoIndex;
Console.WriteLine($"[Subscriber] {_name} is watching {Youtuber.videoDict.GetValueOrDefault(videoIndex)} {times} times.");
if (_server.data.viewCount.ContainsKey(videoIndex))
bool success = Youtuber.videoDict.TryGetValue(videoIndex, out string? title);
if (success)
{
_server.data.viewCount[videoIndex] += times;
_server.data.videoIndex = videoIndex;
Console.WriteLine($"[Subscriber] {_name} is watching {title} {times} times.");
if (_server.data.viewCount.ContainsKey(videoIndex))
{
_server.data.viewCount[videoIndex] += times;
}
else
{
_server.data.viewCount.Add(videoIndex, times);
}
if (_server.data.viewCount.GetValueOrDefault(videoIndex) >= _trendingThreshold && trendingHandler != null)
{
trendingHandler.Invoke(this, _server.data);
}
}
else
{
_server.data.viewCount.Add(videoIndex, times);
}
if (_server.data.viewCount.GetValueOrDefault(videoIndex) >= _trendingThreshold && trendingHandler != null)
{
trendingHandler.Invoke(this, _server.data);
Console.WriteLine("The video is unavailable!");
}
}

Expand Down
53 changes: 46 additions & 7 deletions MyPractice/Youtube/Youtuber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
public class Youtuber : IUpload, IWatch
{
public static int videoIndex;
private string? _name;
private string _name;
private Server _server = new();
public static Dictionary<int, string> videoDict = new();
public static Dictionary<int, int> trendingDict = new();
public EventHandler<DataEventArgs>? uploadHandler;
public EventHandler<DataEventArgs>? trendingHandler;

public Youtuber(string name, Server server)
{
Expand All @@ -33,15 +34,53 @@ private void UploadToServer(int videoIndex, string videoTitle)

public void WatchVideo(int videoIndex, int times)
{
_server.data.videoIndex = videoIndex;
Console.WriteLine($"[Youtuber] {_name} is watching {videoDict.GetValueOrDefault(videoIndex)} {times} times.");
if (_server.data.viewCount.ContainsKey(videoIndex))
bool success = videoDict.TryGetValue(videoIndex, out string? title);
if (success)
{
_server.data.viewCount[videoIndex] += times;
}
Console.WriteLine($"[Youtuber] {_name} is watching {title} {times} times.");
if (!title.Contains(_name))
{
_server.data.videoIndex = videoIndex;
if (_server.data.viewCount.ContainsKey(videoIndex))
{
_server.data.viewCount[videoIndex] += times;
}
else
{
_server.data.viewCount.Add(videoIndex, times);
}
if (_server.data.viewCount.GetValueOrDefault(videoIndex) >= _server.TrendingThreshold && trendingHandler != null)
{
trendingHandler.Invoke(this, _server.data);
}
}
}
else
{
_server.data.viewCount.Add(videoIndex, times);
Console.WriteLine("The video is unavailable!");
}
}

public void GetVideoList()
{
Console.WriteLine($"\n[Youtuber] {_name} is looking at the Video List to watch..");
Console.WriteLine("[Video List]");
foreach (var kvp in videoDict) {
Console.WriteLine($"{kvp.Key}. {kvp.Value}");
}
Console.WriteLine();
}

public void GetTrendingVideo()
{
Console.WriteLine("\n[Trending Video]");
List<Trending> trendingList = _server.GetTrendingList();
int trendingNumber = 0;
foreach (var item in trendingList)
{
trendingNumber += 1;
Console.WriteLine($"{trendingNumber}. {videoDict.GetValueOrDefault(item.VideoIndex)} - {item.ViewCount} views");
}
Console.WriteLine();
}
}

0 comments on commit 9ba2786

Please sign in to comment.