feat: complete independent assigment for lab 5
This commit is contained in:
parent
b18dea67de
commit
ccd5e570e4
17
Lab5/Lab5.Sport/Basketball.cs
Normal file
17
Lab5/Lab5.Sport/Basketball.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
15
Lab5/Lab5.Sport/Football.cs
Normal file
15
Lab5/Lab5.Sport/Football.cs
Normal 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
100
Lab5/Lab5.Sport/InOut.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
8
Lab5/Lab5.Sport/Lab5.Sport.csproj
Normal file
8
Lab5/Lab5.Sport/Lab5.Sport.csproj
Normal 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
24
Lab5/Lab5.Sport/Player.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
8
Lab5/Lab5.Sport/Players.csv
Normal file
8
Lab5/Lab5.Sport/Players.csv
Normal 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
|
|
23
Lab5/Lab5.Sport/Program.cs
Normal file
23
Lab5/Lab5.Sport/Program.cs
Normal 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
37
Lab5/Lab5.Sport/Team.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
2
Lab5/Lab5.Sport/Teams.csv
Normal file
2
Lab5/Lab5.Sport/Teams.csv
Normal file
@ -0,0 +1,2 @@
|
||||
ZALGIRIS;Kaunas;Petriausias petraitis;6
|
||||
PienoZvaigzdes;Kazkur;Joniausias Jonaitis;5
|
|
69
Lab5/Lab5.Sport/TeamsRegister.cs
Normal file
69
Lab5/Lab5.Sport/TeamsRegister.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user