Exemplo n.º 1
0
    public RegionConverter(IRegionData data)
    {
        foreach (IRegionInfo regionInfo in data.GetRegions())
        {
            int intVal = (int)Convert.ChangeType(regionInfo.EnumValue, typeof(int), NumberFormatInfo.InvariantInfo);

            _enumMap.Add(intVal, regionInfo);
            _stringMap.Add(regionInfo.Code, regionInfo);
        }
    }
Exemplo n.º 2
0
    private IRegionInfo GetRegion()
    {
        Console.WriteLine();
        Console.WriteLine("Choose the default region. You can choose it by index or region code");

        HashSet <string> validRegionId = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
        int counter = 0; //used for validation further down

        Console.WriteLine("{0,-8}{1,-20}{2}", "Index", "Region Code", "Region Name");
        foreach (IRegionInfo regionInfo in _regionData.GetRegions())
        {
            validRegionId.Add(regionInfo.Code);
            Console.WriteLine("{0,-8}{1,-20}{2}", Convert.ChangeType(regionInfo.EnumValue, typeof(int), NumberFormatInfo.InvariantInfo), regionInfo.Code, regionInfo.Name);

            counter++;
        }

        start2:
        string?enteredRegion = Console.ReadLine();

        if (enteredRegion != null)
        {
            if (int.TryParse(enteredRegion, out int index) && index >= 0 && index <= counter)
            {
                return(_regionConverter.GetRegion(index));
            }

            if (validRegionId.Contains(enteredRegion))
            {
                return(_regionConverter.GetRegion(enteredRegion));
            }
        }

        Console.Error.WriteLine("Invalid region. Try again.");
        goto start2;
    }