Ejemplo n.º 1
0
        public async Task <int> GetBikeCountInStation(string stationName)
        {
            if (stationName.Any(char.IsDigit))
            {
                throw new ArgumentException("Given station name contains numbers");
            }

            var data = await _httpClient.GetStringAsync(URL);

            BikeRentalStationList bikeData = JsonConvert.DeserializeObject <BikeRentalStationList> (data);

            for (int i = 0; i < bikeData.stations.Count; i++)
            {
                if (bikeData.stations [i].name == stationName)
                {
                    bikeCount = bikeData.stations [i].bikesAvailable;
                    found     = true;
                }
            }

            if (found)
            {
                return(bikeCount);
            }
            else
            {
                throw new NotFoundException("Not Found");
            }
        }
Ejemplo n.º 2
0
        public async Task <int> GetBikeCountInStation(string stationName)
        {
            try
            {
                foreach (var character in stationName.ToCharArray())
                {
                    if (Char.IsDigit(character))
                    {
                        throw new ArgumentException();
                    }
                }

                if (stationName.Any(Char.IsDigit))
                {
                    throw new ArgumentException();
                }

                HttpResponseMessage message = await client.GetAsync("http://api.digitransit.fi/routing/v1/routers/hsl/bike_rental");

                string messageText = System.Text.Encoding.UTF8.GetString(message.Content.ReadAsByteArrayAsync().Result);

                BikeRentalStationList stationList = (BikeRentalStationList)JsonConvert.DeserializeObject <BikeRentalStationList>(messageText);

                int  bikes = 0;
                bool found = false;

                foreach (var station in stationList.stations)
                {
                    if (station.name == stationName)
                    {
                        bikes = station.bikesAvailable;
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    throw new NotFoundException();
                }

                return(bikes);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine("Invalid argument: " + ex.Message);
                return(0);
            }
            catch (NotFoundException ex)
            {
                Console.WriteLine(ex.Message);
                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(0);
            }
        }
        public async Task <int> GetBikeCountInStation(string stationName)
        {
            string apiUrl  = "http://api.digitransit.fi/routing/v1/routers/hsl/bike_rental";
            string numbers = "0123456789";

            for (int i = 0; i < stationName.Length; i++)
            {
                //if (Char.IsDigit(stationName[i]))
                //{
                //    throw new ArgumentException();
                //}

                if (numbers.Contains(stationName[i]))
                {
                    throw new ArgumentException(String.Format("\"{0}\"\nArgument cannot contain numbers\n", stationName));
                }
            }

            HttpClient HttpClient = new HttpClient();
            string     response   = await HttpClient.GetStringAsync(apiUrl);

            // Create BikeRentalStationList object from the JSON response
            BikeRentalStationList list = JsonConvert.DeserializeObject <BikeRentalStationList>(response);

            int  result = 0;
            bool found  = false;

            // Loop through all the stations in the "list" object
            foreach (Station station in list.stations)
            {
                if (station.name == stationName)
                {
                    result = station.bikesAvailable;
                    found  = true;
                    break;
                }
            }

            // If the corresponding station wasnt found throw an NotFoundException
            if (!found)
            {
                throw new NotFoundException("Station not found: ", stationName);
            }
            else
            {
                return(result);
            }
        }
Ejemplo n.º 4
0
        public async Task <int> GetBikeCountInStation(string stationName)
        {
            if (stationName.Any(c => char.IsDigit(c)))
            {
                throw new System.ArgumentException("Station name contained number(s)");
            }
            HttpClient client = new HttpClient();
            string     data   = await client.GetStringAsync("http://api.digitransit.fi/routing/v1/routers/hsl/bike_rental");

            BikeRentalStationList list = JsonConvert.DeserializeObject <BikeRentalStationList>(data);
            Station station            = list.stations.FirstOrDefault(x => x.name == stationName);

            if (station != null)
            {
                return(station.bikeCount);
            }
            else
            {
                throw new NotFoundException(stationName);
            }
        }