Advertisement
dimipan80

Vladko's Notebook (using class Player - the better way)

May 29th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.45 KB | None | 0 0
  1. /* You are given a list of colored sheets given as text table with the following columns:
  2.  *     Option 01 – [color of the sheet]|[win/loss]|[opponent name]
  3.  *     Option 02 – [color of the sheet]|[name]|[player name]
  4.  *     Option 03 – [color of the sheet]|[age]|[player age]
  5.  * The different columns will always be separated only by 'I' (there won't be any whitespaces). The rank of each player is calculated by the formula rank = (wins+1) / (losses+1). If a certain color sheet has no information about the name or the age of the player, you should not print it in the output. If there is no information about the opponents, you must print "(empty)" where the opponents list should be. There might be many opponents with the same name. If there was no information recovered (no colors containing name and age), print only "No data recovered." The input comes from the console on a variable number of lines and ends when the keyword "END" is received. Print at the console the information for each player (sorted by color name) that holds the age, the name, a list with the opponents (in alphabetical order) and rank of the player.The rank of the players should be rounded to 2 digits after the decimal point. */
  6.  
  7. namespace Vladkos_Notebook
  8. {
  9.     using System;
  10.     using System.Collections.Generic;
  11.     using System.Linq;
  12.     using System.Text;
  13.  
  14.     class VladkosNotebook
  15.     {
  16.         private static SortedDictionary<string, Player> playersList;
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             playersList = new SortedDictionary<string, Player>();
  21.  
  22.             GetInformationFromInput();
  23.             var players = playersList
  24.                 .Where(entry => entry.Value.Age != 0 && !string.IsNullOrEmpty(entry.Value.Name))
  25.                 .OrderBy(entry => entry.Key).ToList();
  26.  
  27.             if (!players.Any())
  28.             {
  29.                 Console.WriteLine("No data recovered.");
  30.                 return;
  31.             }
  32.  
  33.             foreach (KeyValuePair<string, Player> entry in players)
  34.             {
  35.                 Console.WriteLine("Color: {0}", entry.Key);
  36.                 Console.WriteLine(entry.Value);
  37.             }
  38.         }
  39.  
  40.         private static void GetInformationFromInput()
  41.         {
  42.             string inputLine = Console.ReadLine();
  43.             while (inputLine != "END")
  44.             {
  45.                 string[] inputs = inputLine.Split('|');
  46.                 if (!playersList.ContainsKey(inputs[0]))
  47.                 {
  48.                     playersList[inputs[0]] = new Player();
  49.                 }
  50.  
  51.                 switch (inputs[1])
  52.                 {
  53.                     case "name":
  54.                         playersList[inputs[0]].Name = inputs[2];
  55.                         break;
  56.                     case "age":
  57.                         playersList[inputs[0]].Age = byte.Parse(inputs[2]);
  58.                         break;
  59.                     case "win":
  60.                         playersList[inputs[0]].Wins++;
  61.                         playersList[inputs[0]].Opponents.Add(inputs[2]);
  62.                         break;
  63.                     case "loss":
  64.                         playersList[inputs[0]].Losses++;
  65.                         playersList[inputs[0]].Opponents.Add(inputs[2]);
  66.                         break;
  67.                 }
  68.  
  69.                 inputLine = Console.ReadLine();
  70.             }
  71.         }
  72.     }
  73.  
  74.     class Player
  75.     {
  76.         public byte Age { get; set; }
  77.  
  78.         public string Name { get; set; }
  79.  
  80.         public List<string> Opponents { get; set; }
  81.  
  82.         public byte Wins { get; set; }
  83.  
  84.         public byte Losses { get; set; }
  85.  
  86.         public Player()
  87.         {
  88.             this.Opponents = new List<string>();
  89.         }
  90.  
  91.         double GetRank()
  92.         {
  93.             return ((this.Wins + 1) / (double)(this.Losses + 1));
  94.         }
  95.  
  96.         public override string ToString()
  97.         {
  98.             StringBuilder sb = new StringBuilder();
  99.             sb.AppendFormat("-age: {0}", this.Age).AppendLine();
  100.             sb.AppendFormat("-name: {0}", this.Name).AppendLine();
  101.  
  102.             string oppons = "(empty)";
  103.             if (this.Opponents.Count > 0)
  104.             {
  105.                 this.Opponents.Sort(StringComparer.Ordinal);
  106.                 oppons = string.Join(", ", this.Opponents);
  107.             }
  108.  
  109.             sb.AppendFormat("-opponents: {0}", oppons).AppendLine();
  110.             sb.AppendFormat("-rank: {0:F2}", this.GetRank());
  111.  
  112.             return sb.ToString();
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement