コード例 #1
0
        static void Main()
        {
            Ranklist      ranklist = new Ranklist();
            StringBuilder result   = new StringBuilder();

            string  input   = Console.ReadLine();
            Command command = Command.Parse(input);

            while (command.Name != "end")
            {
                switch (command.Name)
                {
                case "add":
                    Player currentPlayer = Player.Parse(command.Parameters);
                    ranklist.AddPlayer(currentPlayer);
                    result.AppendLine(string.Format("Added player {0} to position {1}", currentPlayer.Name, currentPlayer.Position));
                    break;

                case "find":
                    string type = command.Parameters[0];
                    IEnumerable <Player> topFifePlayersByType;

                    try
                    {
                        topFifePlayersByType = ranklist.FindTopFivePlayersByType(type);
                    }
                    catch (InvalidOperationException)
                    {
                        result.AppendLine(string.Format("Type {0}: ", type));
                        break;
                    }

                    result.AppendLine(string.Format("Type {0}: {1}", type, string.Join("; ", topFifePlayersByType)));
                    break;

                case "ranklist":
                    int startPosition = int.Parse(command.Parameters[0]);
                    int endPosition   = int.Parse(command.Parameters[1]);

                    var topPlayersInRanklist = ranklist.GetRanklistOnRange(startPosition, endPosition)
                                               .Select(p => new { Position = startPosition++, Player = p }).ToList();

                    result.AppendLine(string.Join("; ", topPlayersInRanklist.Select(p => string.Format("{0}. {1}", p.Position, p.Player))));
                    break;
                }

                input   = Console.ReadLine();
                command = Command.Parse(input);
            }

            Console.WriteLine(result.ToString().Trim());
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Ranklist      ranklist    = new Ranklist();
            StringBuilder finalResult = new StringBuilder();

            string line = Console.ReadLine();

            while (line != "end")
            {
                Command command = Command.Parse(line);

                switch (command.Name)
                {
                case "add":
                    Player player = Player.Parse(command.Arguments);
                    ranklist.Add(player);
                    finalResult.AppendLine(string.Format("Added player {0} to position {1}", player.Name, player.Position));

                    break;

                case "find":
                    string type = command.Arguments[0];
                    IEnumerable <Player> players = ranklist.FindByType(type);
                    finalResult.AppendLine(string.Format("Type {0}: {1}", type, string.Join("; ", players)));

                    break;

                case "ranklist":
                    int startPosition   = int.Parse(command.Arguments[0]) - 1;
                    int endPosition     = int.Parse(command.Arguments[1]) - 1;
                    var playersRanklist = ranklist.GetRanklist(startPosition, endPosition).Select(p => new { Position = ++startPosition, Player = p }).ToList();
                    finalResult.AppendLine(string.Join("; ", playersRanklist.Select(r => string.Format("{0}. {1}", r.Position, r.Player))));

                    break;

                default:
                    throw new InvalidOperationException();
                }

                line = Console.ReadLine();
            }

            Console.WriteLine(finalResult.ToString().Trim());
        }