1
0

feat: complete independent assigment for lab 5

This commit is contained in:
Rokas Puzonas 2021-12-09 00:29:51 +02:00
parent b18dea67de
commit ccd5e570e4
10 changed files with 303 additions and 0 deletions

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}

100
Lab5/Lab5.Sport/InOut.cs Normal file
View File

@ -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<string> ReadByLines(string filename)
{
using (StreamReader reader = new StreamReader(filename, Encoding.UTF8))
{
string line;
while ((line = reader.ReadLine()) != null)
{
yield return line;
}
}
}
private static List<Player> FindPlayersByTeam(List<Player> players, string team)
{
List<Player> filtered = new List<Player>();
foreach (Player player in players)
{
if (player.TeamName == team)
{
filtered.Add(player);
}
}
return filtered;
}
public static List<Team> ReadTeams(string teamsFilename, string playersFilename)
{
List<Player> allPlayers = ReadPlayers(playersFilename);
List<Team> teams = new List<Team>();
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<Player> players = FindPlayersByTeam(allPlayers, name);
Team team = new Team(name, city, coach, gamesPlayed, players);
teams.Add(team);
}
return teams;
}
public static List<Player> ReadPlayers(string filename)
{
List<Player> players = new List<Player>();
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<Player> 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));
}
}
}

View File

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
</Project>

24
Lab5/Lab5.Sport/Player.cs Normal file
View File

@ -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;
}
}
}

View File

@ -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
1 ZALGIRIS;Antanas;Kavaliauskas;1999-10-12;4;40;8;15
2 ZALGIRIS;Laurynas;Birutis;1989-05-15;6;20;30;10
3 ZALGIRIS;Sarunas;Jasikevicius;1997-08-01;6;35;35;10
4 ZALGIRIS;Brandon;Davies;1998-08-10;5;20;5;10
5 PienoZvaigzdes;Jonas;Jonaitis;2008-09-20;5;5;0
6 PienoZvaigzdes;Petras;Petraitis;2007-03-15;4;10;1
7 PienoZvaigzdes;Ona;Onaite;2008-05-10;5;12;2
8 PienoZvaigzdes;Sima;Simaite;2008-04-05;2;7;0

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
namespace Lab5.Sport
{
class Program
{
static void Main(string[] args)
{
List<Team> 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<Player> foundPlayers = register
.FilterByCity(targetCity)
.FindPlayerActiveAndWithHighPoints();
InOut.PrintPlayers(foundPlayers);
}
}
}

37
Lab5/Lab5.Sport/Team.cs Normal file
View File

@ -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<Player> Players { get; set; }
public Team(string name, string city, string coach, uint gamesPlayed, List<Player> 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;
}
}
}

View File

@ -0,0 +1,2 @@
ZALGIRIS;Kaunas;Petriausias petraitis;6
PienoZvaigzdes;Kazkur;Joniausias Jonaitis;5
1 ZALGIRIS Kaunas Petriausias petraitis 6
2 PienoZvaigzdes Kazkur Joniausias Jonaitis 5

View File

@ -0,0 +1,69 @@
using System.Collections.Generic;
namespace Lab5.Sport
{
class TeamsRegister
{
private List<Team> allTeams;
public TeamsRegister()
{
allTeams = new List<Team>();
}
public TeamsRegister(List<Team> initialTeams)
{
allTeams = new List<Team>();
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<Team> filtered = new List<Team>();
foreach (Team team in allTeams)
{
if (team.City == city)
{
filtered.Add(team);
}
}
return new TeamsRegister(filtered);
}
public List<Player> FindPlayerActiveAndWithHighPoints()
{
List<Player> players = new List<Player>();
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;
}
}
}