Пример #1
0
        public void NamesWithListManipulate()
        {
            var list = new List <string>();

            while (true)
            {
                string name = ch.AskForString("Enter a name:");
                if (name.ToLower() == "quit")
                {
                    list.Sort();

                    if (list.Count == 1)
                    {
                        list.RemoveAt(0);
                    }

                    else if (list.Count >= 2)
                    {
                        list.RemoveAt(0);
                        list.RemoveAt(list.Count - 1);
                    }
                    DisplayPeople(list);
                    return;
                }
                list.Add(name);
            }
        }
Пример #2
0
    private void AskUserAndRespond_ToUpper()
    {
        string input  = ch.AskForString($"Enter a string to convert:");
        string answer = ToUpper(input);

        Console.WriteLine($"Here is the result:{answer}\n");
    }
Пример #3
0
        private void ThreeNamesWithArray()
        {
            var list = new string[3];

            for (int i = 0; i < 3; i++)
            {
                string name = ch.AskForString("Enter a name:");
                list[i] = name;
            }

            DisplayPeople(list);
        }
Пример #4
0
        private Dictionary <int, string> BuildProductDictionary()
        {
            var productDic = new Dictionary <int, string>();

            while (true)
            {
                string answer = ch.AskForString("Enter product id and name:");
                answer = answer.Trim();

                if (answer == "")
                {
                    return(productDic);
                }

                if (!ValidInput(answer))
                {
                    ch.WriteLineRed("Invalid input");
                    continue;
                }

                int    productId   = int.Parse(answer.Split(',')[0]);
                string productName = answer.Split(',')[1];

                if (productDic.ContainsKey(productId))
                {
                    ch.WriteLineRed($"The product list already contains the id {productId}");
                }
                else
                {
                    productDic.Add(productId, productName);
                }
            }
        }
Пример #5
0
        static void Main(string[] args)
        {
            var    service = new ShippingService();
            Letter letter;

            while (true)
            {
                try
                {
                    bool rek   = false;
                    bool bulky = false;

                    int weight = ch.AskForInteger("Enter weight:");

                    string input = ch.AskForString("Recommended (y/n)");
                    if (input == "y")
                    {
                        rek = true;
                    }

                    input = ch.AskForString("Bulky (y/n)");
                    if (input == "y")
                    {
                        bulky = true;
                    }

                    letter = new Letter(weight, rek, bulky);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                    continue;
                }
                break;
            }
            Console.WriteLine();
            Console.WriteLine(letter.Weight);
            Console.WriteLine(letter.RecommendedLetter);
            Console.WriteLine(letter.Bulky);

            Console.WriteLine();
            decimal shipping = service.CalculateShipping(letter, "2019");

            Console.WriteLine(shipping);
        }
Пример #6
0
        public static void Run()
        {
            ConsoleHelper ch = new ConsoleHelper();
            string        filename;

            do
            {
                filename = ch.AskForString("Enter a file name:");
            } while (!CreateTextHandleExceptionDetail(filename));
        }
Пример #7
0
        static void Main(string[] args)
        {
            string filename;

            while (true)
            {
                filename = ch.AskForString("Enter a file name:");

                try
                {
                    var newFile = File.CreateText(filename);
                    ch.WriteLine($"The file \'{filename}\' was created");
                    newFile.Close();
                    break;
                }
                catch (ArgumentException)
                {
                    ch.WriteLineRed("The filename is not valid");
                }
                catch (NotSupportedException)
                {
                    ch.WriteLineRed("Invalid input");
                }
                catch (UnauthorizedAccessException)
                {
                    ch.WriteLineRed("You are not autorrized to create this file");
                }
                catch (DirectoryNotFoundException)
                {
                    ch.WriteLineRed("Can't find the folder");
                }
                catch (Exception)
                {
                    ch.WriteLineRed("Something went wrong");
                }
            }

            string text = ch.AskForString("Enter text to add to your file:");

            File.WriteAllText(filename, text);
            ch.WriteLineGreen("Added text to file");
        }
Пример #8
0
        public static void Run()
        {
            ConsoleHelper ch = new ConsoleHelper();

            while (true)
            {
                string filename = ch.AskForString("Enter a file name:");
                bool   success  = CreateTextfile(filename);

                if (success)
                {
                    break;
                }
            }
        }
Пример #9
0
        private bool AskForAnimalsAndReportTheAmount()
        {
            string animalsString = ch.AskForString("Enter a list of animals:");

            try
            {
                string[] animals = ParseAnimals(animalsString);
                Console.WriteLine($"There are {animals.Length} animals in the list");
                return(true);
            }
            catch (ArgumentException ex)
            {
                ch.WriteLineRed(ex.Message);
            }
            catch (Exception)
            {
                ch.WriteLineRed("Unexpected error");
            }
            return(false);
        }
Пример #10
0
        static void Main(string[] args)
        {
            while (true)
            {
                string inputAnimals = ch.AskForString("Enter a list of animals:");

                try
                {
                    string[] listOfAnimals = ParseAnimals(inputAnimals);
                    ch.WriteLineDark($"There are {listOfAnimals.Length} animals in the list");
                    break;
                }
                catch (ArgumentException ex)
                {
                    ch.WriteLineRed(ex.Message);
                }
                catch (Exception)
                {
                    ch.WriteLineRed("Unexpected error");
                }
            }
        }
Пример #11
0
    private Dictionary <int, string> BuildProductDictionary()
    {
        string answer = "";

        var productDic = new Dictionary <int, string>();

        while (true)
        {
            answer = ch.AskForString("Enter product id and name:");

            if (string.IsNullOrWhiteSpace(answer))
            {
                break;
            }
            answer = answer.Trim();

            if (!ValidInput(answer))
            {
                ch.ErrorMessage("Invalid input");
                continue;
            }

            int    productId   = int.Parse(answer.Split(',')[0]);
            string productName = answer.Split(',')[1];

            if (productDic.ContainsKey(productId))
            {
                ch.ErrorMessage($"The product list already contains the id {productId}");
            }
            else
            {
                productDic.Add(productId, productName);
            }
        }
        return(productDic);
    }