-
Notifications
You must be signed in to change notification settings - Fork 617
/
WeatherMonitor.cs
61 lines (53 loc) · 1.5 KB
/
WeatherMonitor.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
using System;
namespace ObserverPattern
{
sealed class WeatherMonitor : IObserver<Weather>
{
private IDisposable _cancellation;
private readonly string _name;
public void Subscribe(WeatherSupplier provider)
{
_cancellation = provider.Subscribe(this);
}
public void Unsubscribe()
{
_cancellation.Dispose();
}
public WeatherMonitor(string name)
{
_name = name;
}
public void OnCompleted()
{
throw new NotImplementedException();
}
public void OnError(Exception error)
{
Console.WriteLine("Error has occured");
}
public void OnNext(Weather value)
{
Console.WriteLine(_name);
if (_name.Contains("T"))
{
string op = $"| Temperature : {value.Temperature} Celsius |";
Console.Write(op);
}
if (_name.Contains("P"))
{
string op = $"| Pressure : {value.Pressure} atm |";
Console.Write(op);
}
if (_name.Contains("H"))
{
string op = $"| Humidity : {value.Humidity * 100} % |";
Console.Write(op);
}
if (!(_name.Contains("T") || _name.Contains("P") || _name.Contains("H")))
{
OnError(new Exception());
}
Console.WriteLine();
}
}
}