diff --git a/Lab5/Lab5.Sport/Basketball.cs b/Lab5/Lab5.Sport/Basketball.cs new file mode 100644 index 0000000..704a2d4 --- /dev/null +++ b/Lab5/Lab5.Sport/Basketball.cs @@ -0,0 +1,17 @@ + +using System; + +namespace Lab5.Sport +{ + class Basketball : Player + { + public uint GainedBallsCount { get; set; } + public uint GreatPassessCount { get; set; } + + public Basketball(string teamName, string name, string surname, DateTime birthDate, uint gamesPlayed, uint pointsScored, uint gainedBallsCount, uint greatPassessCount) : base(teamName, name, surname, birthDate, gamesPlayed, pointsScored) + { + GainedBallsCount = gainedBallsCount; + GreatPassessCount = greatPassessCount; + } + } +} diff --git a/Lab5/Lab5.Sport/Football.cs b/Lab5/Lab5.Sport/Football.cs new file mode 100644 index 0000000..e67f194 --- /dev/null +++ b/Lab5/Lab5.Sport/Football.cs @@ -0,0 +1,15 @@ + +using System; + +namespace Lab5.Sport +{ + class Football : Player + { + public uint GottenYellowCardCount { get; set; } + + public Football(string teamName, string name, string surname, DateTime birthDate, uint gamesPlayed, uint pointsScored, uint gottenYellowCardCount) : base(teamName, name, surname, birthDate, gamesPlayed, pointsScored) + { + GottenYellowCardCount = gottenYellowCardCount; + } + } +} diff --git a/Lab5/Lab5.Sport/InOut.cs b/Lab5/Lab5.Sport/InOut.cs new file mode 100644 index 0000000..1506556 --- /dev/null +++ b/Lab5/Lab5.Sport/InOut.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Lab5.Sport +{ + static class InOut + { + + public static IEnumerable ReadByLines(string filename) + { + using (StreamReader reader = new StreamReader(filename, Encoding.UTF8)) + { + string line; + while ((line = reader.ReadLine()) != null) + { + yield return line; + } + } + } + + private static List FindPlayersByTeam(List players, string team) + { + List filtered = new List(); + foreach (Player player in players) + { + if (player.TeamName == team) + { + filtered.Add(player); + } + } + return filtered; + } + + public static List ReadTeams(string teamsFilename, string playersFilename) + { + List allPlayers = ReadPlayers(playersFilename); + List teams = new List(); + foreach (string line in ReadByLines(teamsFilename)) + { + string[] parts = line.Split(";"); + string name = parts[0]; + string city = parts[1]; + string coach = parts[2]; + uint gamesPlayed = uint.Parse(parts[3]); + List players = FindPlayersByTeam(allPlayers, name); + Team team = new Team(name, city, coach, gamesPlayed, players); + teams.Add(team); + } + return teams; + } + + public static List ReadPlayers(string filename) + { + List players = new List(); + foreach (string line in ReadByLines(filename)) + { + string[] parts = line.Split(";"); + string teamName = parts[0]; + string name = parts[1]; + string surname = parts[2]; + DateTime birthDate = DateTime.Parse(parts[3]); + uint gamesPlayed = uint.Parse(parts[4]); + uint pointsScored = uint.Parse(parts[5]); + Player player; + if (parts.Length == 8) // Basketball + { + uint gainedBallsCount = uint.Parse(parts[6]); + uint greatPassessCount = uint.Parse(parts[7]); + player = new Basketball(teamName, name, surname, birthDate, gamesPlayed, pointsScored, gainedBallsCount, greatPassessCount); + } + else if (parts.Length == 7) // Football + { + uint gottenYellowCardCount = uint.Parse(parts[6]); + player = new Football(teamName, name, surname, birthDate, gamesPlayed, pointsScored, gottenYellowCardCount); + } + else // Who knows what this is + { + throw new Exception($"Expected basketball or football player: {line}"); + } + + players.Add(player); + } + return players; + } + + public static void PrintPlayers(List players) + { + Console.WriteLine(new string('-', 93)); + Console.WriteLine("| {0,-15} | {1,-12} | {2,-12} | {3} | {4} | {5} |", "Team name", "Name", "Surname", "Birth date", "Games played", "Points scored"); + Console.WriteLine(new string('-', 93)); + foreach (Player p in players) + { + Console.WriteLine("| {0,-15} | {1,-12} | {2,-12} | {3:yyyy-MM-dd} | {4,12} | {5,13} |", p.TeamName, p.Name, p.Surname, p.BirthDate, p.GamesPlayed, p.PointsScored); + } + Console.WriteLine(new string('-', 93)); + } + } +} diff --git a/Lab5/Lab5.Sport/Lab5.Sport.csproj b/Lab5/Lab5.Sport/Lab5.Sport.csproj new file mode 100644 index 0000000..958d2f1 --- /dev/null +++ b/Lab5/Lab5.Sport/Lab5.Sport.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.0 + + + diff --git a/Lab5/Lab5.Sport/Player.cs b/Lab5/Lab5.Sport/Player.cs new file mode 100644 index 0000000..cda8470 --- /dev/null +++ b/Lab5/Lab5.Sport/Player.cs @@ -0,0 +1,24 @@ +using System; + +namespace Lab5.Sport +{ + abstract class Player + { + public string TeamName { get; set; } + public string Name { get; set; } + public string Surname { get; set; } + public DateTime BirthDate { get; set; } + public uint GamesPlayed { get; set; } + public uint PointsScored { get; set; } + + public Player(string teamName, string name, string surname, DateTime birthDate, uint gamesPlayed, uint pointsScored) + { + TeamName = teamName; + Name = name; + Surname = surname; + BirthDate = birthDate; + GamesPlayed = gamesPlayed; + PointsScored = pointsScored; + } + } +} diff --git a/Lab5/Lab5.Sport/Players.csv b/Lab5/Lab5.Sport/Players.csv new file mode 100644 index 0000000..c7bf185 --- /dev/null +++ b/Lab5/Lab5.Sport/Players.csv @@ -0,0 +1,8 @@ +ZALGIRIS;Antanas;Kavaliauskas;1999-10-12;4;40;8;15 +ZALGIRIS;Laurynas;Birutis;1989-05-15;6;20;30;10 +ZALGIRIS;Sarunas;Jasikevicius;1997-08-01;6;35;35;10 +ZALGIRIS;Brandon;Davies;1998-08-10;5;20;5;10 +PienoZvaigzdes;Jonas;Jonaitis;2008-09-20;5;5;0 +PienoZvaigzdes;Petras;Petraitis;2007-03-15;4;10;1 +PienoZvaigzdes;Ona;Onaite;2008-05-10;5;12;2 +PienoZvaigzdes;Sima;Simaite;2008-04-05;2;7;0 diff --git a/Lab5/Lab5.Sport/Program.cs b/Lab5/Lab5.Sport/Program.cs new file mode 100644 index 0000000..6c56df6 --- /dev/null +++ b/Lab5/Lab5.Sport/Program.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; + +namespace Lab5.Sport +{ + class Program + { + static void Main(string[] args) + { + List teams = InOut.ReadTeams("Teams.csv", "Players.csv"); + TeamsRegister register = new TeamsRegister(teams); + + /* Console.WriteLine("Number of teams: {0}\n", register.Count()); */ + + string targetCity = "Kaunas"; + List foundPlayers = register + .FilterByCity(targetCity) + .FindPlayerActiveAndWithHighPoints(); + + InOut.PrintPlayers(foundPlayers); + } + } +} diff --git a/Lab5/Lab5.Sport/Team.cs b/Lab5/Lab5.Sport/Team.cs new file mode 100644 index 0000000..15d0b08 --- /dev/null +++ b/Lab5/Lab5.Sport/Team.cs @@ -0,0 +1,37 @@ +using System.Collections.Generic; + +namespace Lab5.Sport +{ + class Team + { + public string Name { get; set; } + public string City { get; set; } + public string Coach { get; set; } + public uint GamesPlayed { get; set; } + public List Players { get; set; } + + public Team(string name, string city, string coach, uint gamesPlayed, List players) { + Name = name; + City = city; + Coach = coach; + GamesPlayed = gamesPlayed; + Players = players; + } + + public uint GetTotalPointsScored() + { + uint total = 0; + foreach (Player player in Players) + { + total += player.PointsScored; + } + return total; + } + + public float GetAveragePointsScored() + { + return (float)GetTotalPointsScored() / Players.Count; + } + } +} + diff --git a/Lab5/Lab5.Sport/Teams.csv b/Lab5/Lab5.Sport/Teams.csv new file mode 100644 index 0000000..4489617 --- /dev/null +++ b/Lab5/Lab5.Sport/Teams.csv @@ -0,0 +1,2 @@ +ZALGIRIS;Kaunas;Petriausias petraitis;6 +PienoZvaigzdes;Kazkur;Joniausias Jonaitis;5 diff --git a/Lab5/Lab5.Sport/TeamsRegister.cs b/Lab5/Lab5.Sport/TeamsRegister.cs new file mode 100644 index 0000000..8f83d3c --- /dev/null +++ b/Lab5/Lab5.Sport/TeamsRegister.cs @@ -0,0 +1,69 @@ + +using System.Collections.Generic; + +namespace Lab5.Sport +{ + class TeamsRegister + { + private List allTeams; + + public TeamsRegister() + { + allTeams = new List(); + } + + public TeamsRegister(List initialTeams) + { + allTeams = new List(); + foreach (Team team in initialTeams) + { + Add(team); + } + } + + public void Add(Team team) + { + allTeams.Add(team); + } + + public Team Get(int index) + { + return allTeams[index]; + } + + public int Count() + { + return allTeams.Count; + } + + public TeamsRegister FilterByCity(string city) + { + List filtered = new List(); + foreach (Team team in allTeams) + { + if (team.City == city) + { + filtered.Add(team); + } + } + return new TeamsRegister(filtered); + } + + public List FindPlayerActiveAndWithHighPoints() + { + List players = new List(); + foreach (Team team in allTeams) + { + float averagePoints = team.GetAveragePointsScored(); + foreach (Player player in team.Players) + { + if (team.GamesPlayed <= player.GamesPlayed && player.PointsScored > averagePoints) + { + players.Add(player); + } + } + } + return players; + } + } +}