Skip to content

Commit ed688cc

Browse files
committed
Add newConfirmed, newDeaths, newRecovered columns to data.
1 parent 49d5f5f commit ed688cc

1 file changed

Lines changed: 28 additions & 20 deletions

File tree

Program.cs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -216,23 +216,34 @@ static void ReadData(string url, DataSet dataSet, DataType dataType)
216216
}
217217
}
218218

219+
static readonly DataRecord s_zeroRecord = new DataRecord();
220+
219221
static void WriteData(DataSet dataSet, string path)
220222
{
221223
using (var writer = new StreamWriter(path, false, s_UTF8_No_BOM))
222224
{
223225
writer.NewLine = "\n";
224-
writer.WriteLine("\"Date\",\"ProvinceState\",\"CountryRegion\",\"Lat\",\"Long\",\"Confirmed\",\"Deaths\",\"Recovered\"");
226+
writer.WriteLine("\"Date\",\"ProvinceState\",\"CountryRegion\",\"Lat\",\"Long\",\"Confirmed\",\"Deaths\",\"Recovered\",\"NewConfirmed\",\"NewDeaths\",\"NewRecovered\"");
225227

226228
var dateList = new List<KeyValuePair<DateTime, Dictionary<DataKey, DataRecord>>>(dataSet);
227229
dateList.Sort((a, b) => a.Key.CompareTo(b.Key));
230+
231+
Dictionary<DataKey, DataRecord> prevDate = null;
228232
foreach(var datePair in dateList)
229233
{
230234
var recordList = new List<KeyValuePair<DataKey, DataRecord>>(datePair.Value);
231235
recordList.Sort((a, b) => a.Key.CompareTo(b.Key));
232236
foreach(var recordPair in recordList)
233237
{
234-
writer.WriteLine(ToString(recordPair));
238+
DataRecord prevRecord = null;
239+
if (prevDate == null || !prevDate.TryGetValue(recordPair.Key, out prevRecord))
240+
{
241+
prevRecord = s_zeroRecord;
242+
}
243+
writer.WriteLine(ToString(datePair.Key, recordPair.Key, recordPair.Value, prevRecord));
235244
}
245+
246+
prevDate = datePair.Value;
236247
}
237248
}
238249
}
@@ -288,32 +299,33 @@ private static void AddData(DataSet dataset, DateTime date, string provinceState
288299
}
289300
}
290301

291-
static string ToString(KeyValuePair<DataKey, DataRecord> pair)
302+
static string ToString(DateTime date, DataKey key, DataRecord record, DataRecord prevRecord)
292303
{
293304
return String.Join(",",
294-
pair.Key.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
295-
string.Concat("\"", pair.Key.ProvinceState, "\""),
296-
string.Concat("\"", pair.Key.CountryRegion, "\""),
297-
pair.Key.Latitude,
298-
pair.Key.Longitude,
299-
pair.Value.Confirmed,
300-
pair.Value.Deaths,
301-
pair.Value.Recovered);
305+
date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
306+
string.Concat("\"", key.ProvinceState, "\""),
307+
string.Concat("\"", key.CountryRegion, "\""),
308+
key.Latitude,
309+
key.Longitude,
310+
record.Confirmed,
311+
record.Deaths,
312+
record.Recovered,
313+
record.Confirmed - prevRecord.Confirmed,
314+
record.Deaths - prevRecord.Deaths,
315+
record.Recovered - prevRecord.Recovered);
302316
}
303317

304318
} // Class Program
305319

306320
class DataKey : IComparable<DataKey>
307321
{
308-
public DateTime Date { get; private set; }
309322
public string ProvinceState { get; private set; }
310323
public string CountryRegion { get; private set; }
311324
public string Latitude { get; private set; }
312325
public string Longitude { get; private set; }
313326

314327
public DataKey(DateTime datex, string provinceState, string countryRegion, string latitude, string longitude)
315328
{
316-
Date = datex;
317329
ProvinceState = provinceState;
318330
CountryRegion = countryRegion;
319331
Latitude = latitude;
@@ -322,8 +334,7 @@ public DataKey(DateTime datex, string provinceState, string countryRegion, strin
322334

323335
public override int GetHashCode()
324336
{
325-
return Date.GetHashCode()
326-
^ ProvinceState.GetHashCode()
337+
return ProvinceState.GetHashCode()
327338
^ CountryRegion.GetHashCode();
328339
}
329340

@@ -332,16 +343,13 @@ public override bool Equals(object obj)
332343
var other = obj as DataKey;
333344
if (obj == null) return false;
334345

335-
return Date.Equals(other.Date)
336-
&& ProvinceState.Equals(other.ProvinceState)
346+
return ProvinceState.Equals(other.ProvinceState)
337347
&& CountryRegion.Equals(other.CountryRegion);
338348
}
339349

340350
public int CompareTo(DataKey other)
341351
{
342-
int i = Date.CompareTo(other.Date);
343-
if (i != 0) return i;
344-
i = CountryRegion.CompareTo(other.CountryRegion);
352+
int i = CountryRegion.CompareTo(other.CountryRegion);
345353
if (i != 0) return i;
346354
return ProvinceState.CompareTo(other.ProvinceState);
347355
}

0 commit comments

Comments
 (0)