static async Task Main(string[] args) { string stationName = "Munkkiniemen aukio"; int bikeCount = -1; if (args.Length == 0) { throw new ArgumentException("No argument provided"); } ICityBikeDataFetcher dataFetcher = null; if (args[0] == "online") { dataFetcher = new RealTimeCityBikeDataFetcher(); } else if (args[0] == "offline") { dataFetcher = new OfflineCityBikeDataFetcher(); } else { throw new ArgumentException("Argument must be 'online' or 'offline'"); } try { bikeCount = await dataFetcher.GetBikeCountInStation(stationName); } catch { throw new NotFoundException("Not found: " + stationName); } Console.WriteLine(bikeCount); }
static void Main(string[] args) { if (args.Length == 0) { throw new System.ArgumentException("No command line parameters"); } else if (args[0].Any(char.IsDigit)) { throw new System.ArgumentException("Command line parameter should not have a number in it"); } try { ICityBikeDataFetcher fetcher = null; if (args.Length == 2 && args[1] == "realtime") { fetcher = new RealTimeCityBikeDataFetcher(); Console.WriteLine("Realtime fetcher used"); } else if (args.Length == 2 && args[1] == "offline") { fetcher = new OfflineCityBikeDataFetcher(); Console.WriteLine("Offline fetcher used"); } else { fetcher = new RealTimeCityBikeDataFetcher(); Console.WriteLine("No fetcher specified, used Realtime fetcher"); } Task <int> fetchingOperation = fetcher.GetBikeCountInStation(args[0]); //automatically calls Wait() int tulos = fetchingOperation.Result; Console.WriteLine(tulos + " bikes available"); } catch (Exception ex) //Apparently this would work if the function wasn't asynchronous: //catch (NotFoundException ex) { Console.WriteLine("Not found: " + ex.Message); } }
static async Task Main(string[] args) { if (args.Length == 0) { Console.WriteLine($"Please enter station name [and '{realTimeArgument}'/'{offlineArgument}']"); return; } string requestedStationName = args [0].ToLower(); bool realTime = true; if (args.Length > 1) { if (args[1] == offlineArgument) { realTime = false; } else if (args[1] != realTimeArgument) { Console.WriteLine($"Invalid Argument: {args[1]}"); return; } } try { ICityBikeDataFetcher bikeDataFetcher = realTime ? (ICityBikeDataFetcher) new RealTimeBikeDataFetcher() : (ICityBikeDataFetcher) new OfflineBikeDataFetcher(); int bikeCount = await bikeDataFetcher.GetBikeCountInStation(requestedStationName); Console.WriteLine($"{bikeCount} bikes available at {requestedStationName}"); } catch (ArgumentException exception) { Console.WriteLine($"Invalid argument: {exception.Message}"); } catch (NotFoundException exception) { Console.WriteLine($"Station not found: {exception.Message}"); } }
static void Main(string[] args) { try { ICityBikeDataFetcher fetcher = null; if (args[1] == "realtime") { fetcher = new RealTimeCityBikeDataFetcher(); } else if (args[1] == "offline") { fetcher = new OfflineCityBikeDataFetcher(); } else { Console.WriteLine("Invalid argument: " + args[1]); Console.ReadKey(); return; } Task <int> task = fetcher.GetBikeCountInStation(args[0]); task.Wait(); Console.WriteLine(task.Result); }catch (AggregateException e) { if (e.InnerException.GetType() == typeof(ArgumentException)) { Console.WriteLine("Invalid argument: " + e.InnerException.Message); } else if (e.InnerException.GetType() == typeof(NotFoundException)) { Console.WriteLine("Not found: " + e.InnerException.Message); } else { Console.WriteLine(e); } } Console.ReadKey(); }