示例#1
0
    static void Main(string[] args)
    {
        Phonebook phonebook = new Phonebook();

        using (StreamReader inputReader = new StreamReader("phones.txt"))
        {
            String line = inputReader.ReadLine();
            while (line != null)
            {
                string[] lineWords = line.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                string   names     = lineWords[0].Trim();
                string   town      = lineWords[1].Trim();
                string   phone     = lineWords[2].Trim();

                PhoneRecord record = new PhoneRecord(names, town, phone);
                phonebook.Add(record);
                line = inputReader.ReadLine();
            }
        }

        using (StreamReader inputReader = new StreamReader("commands.txt"))
        {
            String line = inputReader.ReadLine();
            while (line != null)
            {
                string[] comandParameters = line.Split(new string[] { "find(", ")", ",", " " }, StringSplitOptions.RemoveEmptyEntries);

                List <PhoneRecord> results;
                if (comandParameters.Length == 2)
                {
                    string names = comandParameters[0].Trim();
                    string town  = comandParameters[1].Trim();

                    results = phonebook.Find(names, town);
                }
                else
                {
                    string names = comandParameters[0].Trim();
                    results = phonebook.Find(names);
                }

                Console.WriteLine(new String('*', 30));
                Console.WriteLine("Result from command {0}", line);
                PrintPhoneRecordList(results);

                line = inputReader.ReadLine();
            }
        }
    }
        public static void Main()
        {
            var allContacts = File.ReadAllLines(phonesPath);
            var commands = File.ReadAllLines(commandsPath);

            var phonebook = new Phonebook();

            foreach (var line in allContacts)
            {
                phonebook.Add(line);
            }

            foreach (var command in commands)
            {
                Console.WriteLine("Current command " + command);
                var checkCommand = command.IndexOf("find", StringComparison.InvariantCultureIgnoreCase);

                if (checkCommand < 0)
                {
                    Console.WriteLine("Invalid command!");
                    continue;
                }

                if (command.IndexOf(", ", StringComparison.InvariantCultureIgnoreCase) < 0)
                {
                    var name = command.Substring(checkCommand + 4).Trim(new[] { '(', ')' });

                    Console.WriteLine(string.Join(Environment.NewLine, phonebook.Find(name)));
                }
                else
                {
                    var nameAndTown = command.Substring(checkCommand + 4).Trim(new[] { '(', ')' }).Split(',').Select(x => x.Trim()).ToList();

                    Console.WriteLine(string.Join(Environment.NewLine, phonebook.Find(nameAndTown[0], nameAndTown[1])));
                }

                Console.WriteLine(new string('-', 50));
            }
        }
示例#3
0
    // Executes each command and searches the phonebook
    private static void ProcessCommands(IEnumerable<string> commands, Phonebook phonebook)
    {
        foreach (var command in commands)
        {
            Console.WriteLine("> " + command);
            var args = command.Substring(5, command.Length - 6);
            var searchTerms = args.Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries);

            try
            {
                var entries = phonebook.Find(searchTerms);
                Console.WriteLine(string.Join("\n", entries));
            }
            catch (KeyNotFoundException)
            {
                Console.WriteLine("Not found: {0}", args);
            }

            Console.WriteLine("----------------------------------------------");
        }
    }