public void GetBirdWatchData() { List <BirdWatchItem> birdList = BirdWatchDataFile.Read(); JavaScriptSerializer jsonString = new JavaScriptSerializer(); Context.Response.Write(jsonString.Serialize(birdList)); }
public void IncrementBirdObservations(string birdName) { List <BirdWatchItem> birdList = BirdWatchDataFile.Read(); for (int i = 0; i < birdList.Count; i++) { if (birdList[i].BirdName == birdName) { birdList[i].IncrementBirdObservations(); break; } } BirdWatchLogFile.Write(birdName, birdList); BirdWatchDataFile.Write(birdList); }
/// <summary> /// Read all recorded bird types and observation counts from data file. /// If file does not exist or file is empty, two default items are added. /// </summary> /// <returns> /// The list of birds and corresponding observation counts. /// </returns> public static List <BirdWatchItem> Read() { List <BirdWatchItem> birdList = new List <BirdWatchItem>(); try { string filePath = new Uri(Path.GetDirectoryName( Assembly.GetExecutingAssembly().CodeBase)).LocalPath; StreamReader reader = new StreamReader(filePath + "\\BirdWatch.dat"); string line; do { line = reader.ReadLine(); if (line != null) { birdList.Add(new BirdWatchItem( BirdWatchDataFile.ParseBirdName(line), BirdWatchDataFile.ParseBirdObservations(line))); } }while (line != null); reader.Close(); } catch (FileNotFoundException) { // File was not found, add Harakka and Varis items to the list birdList.Add(new BirdWatchItem("Harakka", 0)); birdList.Add(new BirdWatchItem("Varis", 0)); } if (birdList.Count == 0) { // File was empty, add Harakka and Varis items to the list birdList.Add(new BirdWatchItem("Harakka", 0)); birdList.Add(new BirdWatchItem("Varis", 0)); } return(birdList); }
public string AddNewBirdItem(string birdName) { List <BirdWatchItem> birdList = BirdWatchDataFile.Read(); JavaScriptSerializer jsonStream = new JavaScriptSerializer(); // Validate if (string.IsNullOrEmpty(birdName) || !Regex.IsMatch(birdName, @"^[a-öA-Ö]+$")) { return("Virhe: virheellinen linnun nimi"); } for (int i = 0; i < birdList.Count; i++) { if (birdList[i].BirdName == birdName) { return("Virhe: yritit lisätä duplikaattia"); } } birdList.Add(new BirdWatchItem(birdName, 0)); BirdWatchLogFile.Write(birdName); BirdWatchDataFile.Write(birdList); return(string.Empty); }