-
Notifications
You must be signed in to change notification settings - Fork 0
/
Meteo.cs
158 lines (120 loc) · 4.38 KB
/
Meteo.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
namespace PrimitiveTypes
{
[TestClass]
public class Meteo
{
[TestMethod]
[TestCategory("14_Weather_Station")]
public void Test_Add_New_Reading() {
ReadingsList readingsList = new ReadingsList();
readingsList.Add(21.73);
double reading = readingsList.GetReading(0);
Assert.AreEqual(21.73, reading);
}
[TestMethod]
[TestCategory("14_Weather_Station")]
public void Test_Add_A_List_Readings() {
var readings = new List<double> { -5, -10, 0.23, 12.53, 17.05 };
ReadingsList readingsList = new ReadingsList();
readingsList.Add(readings);
double reading = readingsList.GetReading(4);
Assert.AreEqual(17.05, reading);
}
[TestMethod]
[TestCategory("14_Weather_Station")]
public void Test_Average_Of_The_Readings() {
var readings = new List<double> { 7.15, 10, 9.23, 12.53, 17.05 };
ReadingsList readingsList = new ReadingsList();
readingsList.Add(readings);
double average = readingsList.AverageTemperature;
Assert.AreEqual(11.192, average, 3);
}
[TestMethod]
[TestCategory("14_Weather_Station")]
public void Test_Minimum_Temperature_Of_The_Readings() {
var readings = new List<double> { 10, 9.23, 12.53, 7.15, 17.05 };
ReadingsList readingsList = new ReadingsList();
readingsList.Add(readings);
double minimumTemperature = readingsList.MinimumTemperature;
Assert.AreEqual(7.15, minimumTemperature);
}
[TestMethod]
[TestCategory("14_Weather_Station")]
public void Test_Maximum_Temperature_Of_The_Readings() {
var readings = new List<double> { 10, 9.23, 12.53, 7.15, 17.05 };
ReadingsList readingsList = new ReadingsList();
readingsList.Add(readings);
double maximumTemperature = readingsList.MaximumTemperature;
Assert.AreEqual(17.05, maximumTemperature);
}
[TestMethod]
[TestCategory("14_Weather_Station")]
public void Test_Maximum_Temperature_Difference_Of_The_Readings() {
var readings = new List<double> { 10, 9.23, 12.53, 7.15, 17.05 };
ReadingsList readingsList = new ReadingsList();
readingsList.Add(readings);
double maximumTemperature = readingsList.MaximumDiference;
Assert.AreEqual(9.9, maximumTemperature);
}
}
public struct ReadingsList
{
public List<double> dailyReadings;
public void Add(double temperature) {
if (dailyReadings == null) dailyReadings = new List<double>();
dailyReadings.Add(temperature);
}
public void Add(List<double> temperatures) {
if (dailyReadings == null) dailyReadings = new List<double>();
foreach (var value in temperatures) dailyReadings.Add(value);
}
public double GetReading(uint day) {
ValidateInput(day);
return dailyReadings[(int)day];
}
private void ValidateInput(uint day) {
if (day > dailyReadings.Count) throw new ArgumentException("Requested reading is not in list.");
}
public double AverageTemperature {
get
{
double sum = 0;
foreach (var value in dailyReadings) {
sum += value;
}
return sum / dailyReadings.Count;
}
}
public int Readings {
get {
return dailyReadings.Count;
}
}
public double MinimumTemperature
{
get
{
double[] result = dailyReadings.ToArray();
return result.Min();
}
}
public double MaximumTemperature
{
get {
double[] result = dailyReadings.ToArray();
return result.Max();
}
}
public double MaximumDiference
{
get
{
return this.MaximumTemperature - this.MinimumTemperature;
}
}
}
}