Advertisement
deleriumbg

Handball Statistics

May 25th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Handball_Statistics
  6. {
  7.     public class Team
  8.     {
  9.         public Team()
  10.         { }
  11.  
  12.         public Team(string name, List<string> opponents, int wins)
  13.         {
  14.             Name = name;
  15.             Opponents = opponents;
  16.             Wins = wins;
  17.         }
  18.  
  19.         public string Name { get; set; }
  20.         public List<string> Opponents { get; set; }
  21.         public int Wins { get; set; }
  22.     }
  23.  
  24.     class Program
  25.     {
  26.         static void Main(string[] args)
  27.         {
  28.             string input = Console.ReadLine();
  29.  
  30.             List<Team> teams = new List<Team>();
  31.  
  32.             while (input != "stop")
  33.             {
  34.                 string[] matchDetails = input.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  35.                 string team1Name = matchDetails[0].Trim();
  36.                 string team2Name = matchDetails[1].Trim();
  37.                 int[] result1 = matchDetails[2].Trim().Split(':').Select(int.Parse).ToArray();
  38.                 int[] result2 = matchDetails[3].Trim().Split(':').Select(int.Parse).ToArray();
  39.  
  40.                 if (!teams.Any(t => t.Name == team1Name))
  41.                 {
  42.                     Team team = new Team();
  43.                     team.Name = team1Name;
  44.                     team.Wins += (team1Wins(result1, result2) ? 1 : 0);
  45.                     team.Opponents = new List<string>();
  46.                     team.Opponents.Add(team2Name);
  47.                     teams.Add(team);
  48.                 }
  49.                 else
  50.                 {
  51.                     Team existingTeam = teams.Find(t => t.Name == team1Name);
  52.                     existingTeam.Wins += (team1Wins(result1, result2) ? 1 : 0);
  53.                     existingTeam.Opponents.Add(team2Name);
  54.                 }
  55.  
  56.                 if (!teams.Any(t => t.Name == team2Name))
  57.                 {
  58.                     Team team = new Team();
  59.                     team.Name = team2Name;
  60.                     team.Wins += (team1Wins(result1, result2) ? 0 : 1);
  61.                     team.Opponents = new List<string>();
  62.                     team.Opponents.Add(team1Name);
  63.                     teams.Add(team);
  64.                 }
  65.                 else
  66.                 {
  67.                     Team existingTeam = teams.Find(t => t.Name == team2Name);
  68.                     existingTeam.Wins += (team1Wins(result1, result2) ? 0 : 1);
  69.                     existingTeam.Opponents.Add(team1Name);
  70.                 }
  71.  
  72.                 input = Console.ReadLine();
  73.             }
  74.  
  75.             foreach (var team in teams.OrderByDescending(x => x.Wins).ThenBy(x => x.Name))
  76.             {
  77.                 Console.WriteLine(team.Name);
  78.                 Console.WriteLine($"- Wins: {team.Wins}");
  79.                 Console.WriteLine($"- Opponents: {string.Join(", ", team.Opponents.OrderBy(x => x))}");
  80.             }
  81.         }
  82.  
  83.         private static bool team1Wins(int[] result1, int[] result2)
  84.         {
  85.             int team1Points = result1[0] + result2[1];
  86.             int team2Points = result1[1] + result2[0];
  87.  
  88.             if (team1Points > team2Points)
  89.             {
  90.                 return true;
  91.             }
  92.             else if (team1Points == team2Points)
  93.             {
  94.                 if (result2[1] > result1[1])
  95.                 {
  96.                     return true;
  97.                 }
  98.                 else
  99.                 {
  100.                     return false;
  101.                 }
  102.             }
  103.             return false;
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement