0% found this document useful (0 votes)
4 views

c# Assisgment

The document is a C# program that demonstrates multithreading, object-oriented programming, and data structures related to a sports club management system. It includes classes for players, coaches, teams, and clubs, along with methods for sorting and searching players by ratings and IDs. The program also showcases the use of a custom list implementation and enumerates player positions.

Uploaded by

earenyeager2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

c# Assisgment

The document is a C# program that demonstrates multithreading, object-oriented programming, and data structures related to a sports club management system. It includes classes for players, coaches, teams, and clubs, along with methods for sorting and searching players by ratings and IDs. The program also showcases the use of a custom list implementation and enumerates player positions.

Uploaded by

earenyeager2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

using System;

using System.Threading;
namespace program
{
class Program
{
static void Work(string name, int delay)
{
Console.WriteLine($"[Thread] {name} (ID:
{Thread.CurrentThread.ManagedThreadId}) starts.");
Thread.Sleep(delay);
Console.WriteLine($"[Thread] {name} (ID:
{Thread.CurrentThread.ManagedThreadId}) finishes.");
}
static void Main()
{
Console.WriteLine("[Thread] Main: " +
Thread.CurrentThread.ManagedThreadId);

Thread thread1 = new Thread(() => Work("Thread 1", 1000));


Thread thread2 = new Thread(() => Work("Thread 2", 1500));

thread1.Start();
thread2.Start();

thread1.Join();
thread2.Join();

Console.WriteLine("[Thread] Done.");

Player p1 = new Player(1, "Ali", 22, Position.Striker, 90);


Player p2 = new Player(2, "Omar", 21, Position.Midfielder, 80);
Player p3 = new Player(3, "Ziad", 20, Position.Defender, 85);
Player p4 = new Player(4, "Hassan", 23, Position.Goalkeeper, 75);

Coach c1 = new Coach(1, "Coach A", 40, "Offensive", 70);


Coach c2 = new Coach(2, "Coach B", 50, "Defensive", 90);

Team t1 = new Team("Tigers", c1);


Team t2 = new Team("Lions", c2);

t1.Players.Add(p1);
t1.Players.Add(p2);
t2.Players.Add(p3);
t2.Players.Add(p4);

Club club = new Club("Champions Club");


club.AddTeam(t1);
club.AddTeam(t2);

club.DisplayClubInfo();

Console.WriteLine("\n--- Sorted Players in Team 1 by Rating ---");


Sorter.SortPlayersByRating(t1.Players);
t1.Players.PrintAll();

Console.WriteLine("\n--- Player with ID 2 ---");


var found = Searcher.BinarySearchPlayerById(t1.Players, 2);
Console.WriteLine(found != null ? found.ToString() : "Not found");
Console.WriteLine("\n--- Players above rating 80 ---");
var above80 = club.GetPlayersAboveRating(80);
above80.PrintAll();

Console.WriteLine("\n--- Most Experienced Coach ---");


Console.WriteLine(club.GetMostExperiencedCoach());
}
}

public class MyList<T>


{
private T[] items;
private int count;
int capacity;

public MyList(int Capacity)


{
capacity = Capacity;
items = new T[capacity];
count = 0;
}
public bool Contains(T item)
{
for (int i = 0; i < count - 1; i++)
{
if(items[i].Equals(item))
{
return true;
}
}
return false;
}
public void Add(T item)
{
if (Contains(item))
{
throw new Exception("item is already in array");
}
if (count == items.Length)
Expand();
items[count++] = item;
}

public void InsertAt(int index, T item)


{
if (index < 0 || index > count)
throw new Exception("Invalid index");
if (count == items.Length)
Expand();
for (int i = count; i > index; i--)
items[i] = items[i - 1];
items[index] = item;
count++;
}

public void RemoveAt(int index)


{
if (index < 0 || index >= count)
throw new Exception("Invalid index");
for (int i = index; i < count - 1; i++)
items[i] = items[i + 1];
count--;
}
public void Remove(T item)
{
for (int i = 0; i < count; i++)
{
if (items[i].Equals(item))
{
RemoveAt(i);
break;
}
}
}
public T Get(int index)
{
if (index < 0 || index >= count)
throw new Exception("Invalid index");
return items[index];
}

public void PrintAll()


{
for (int i = 0; i < count; i++)
{
Console.WriteLine(items[i]);
}
}

private void Expand()


{
T[] newItems = new T[items.Length * 2];
for (int i = 0; i < count; i++)
newItems[i] = items[i];
items = newItems;
}

public int Count()


{
return count;

public T this[int index]


{
get
{
return Get(index);
}
set
{
if (index < 0 || index >= count)
throw new Exception("Invalid index");
items[index] = value;
}
}
}
public abstract class Person
{
public string Name;
public int Age;
public Person(string name, int age)
{
Name = name;
Age = age;
}
public abstract string GetRole();
public override string ToString()
{
return $"Name: {Name}, Age: {Age}";
}
}
public interface IRateable
{
int GetRating();
}
public class Player : Person, IRateable
{
public int PlayerId;
public Position Position;
private int rating;

public Player(int playerId, string name, int age, Position position, int
rating) : base(name, age)
{
PlayerId = playerId;
Position = position;
Rating = rating;
}

public int Rating


{
get { return rating; }
set
{
if (value < 0 || value > 100)
throw new Exception("Rating must be between 0 and 100.");
rating = value;
}
}

public override string GetRole()


{
return "Player";
}

public override string ToString()


{
return $"[Player] ID: {PlayerId}, Name: {Name}, Position: {Position},
Rating: {Rating}";
}

public int GetRating()


{
return Rating;
}
}
public class Coach : Person, IRateable
{
public int CoachId;
public string Specialty;
public int ExperienceScore;

public Coach(int coachId, string name, int age, string specialty, int
experienceScore) : base(name, age)
{
CoachId = coachId;
Specialty = specialty;
ExperienceScore = experienceScore;
}

public override string GetRole()


{
return "Coach";
}

public override string ToString()


{
return $"[Coach] ID: {CoachId}, Name: {Name}, Specialty: {Specialty},
Experience: {ExperienceScore}";
}

public int GetRating()


{
return ExperienceScore;
}
}
public class Team
{
public string TeamName;
public Coach TeamCoach;
public MyList<Player> Players = new MyList<Player>();

public Team(string name, Coach coach)


{
TeamName = name;
TeamCoach = coach;
}

public override string ToString()


{
return $"[Team] {TeamName}, Coach: {TeamCoach.Name}, Players:
{Players.Count}";
}
public int Sercher(MyList<Player> m, int target)
{
int left = 0;
int right = m.Count() - 1;
while (left <= right)
{
if (m[mid].PlayerId == target)
{
return mid;
}
else if(m[mid].PlayerId < target)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return -1;
}
public void Sorter(MyList<Player> m)
{
for(int i = 1; i < m.Count() - 1; i++)
{
Player key = m[i];
int j = i - 1;
while(j >= 0 && m[j].Rating < key.Rating)
{
m[j + 1] = m[j];
j--;
}
arr[j + 1] = key;
}
}
public class Club
{
public string ClubName;
MyList<Team> Teams = new MyList<Team>(2);
public Club(string name)
{
ClubName = name;
}
public void AddTeam(Team team)
{
if (team == null)
{
throw new Exception("Cannot add null team.");
}
Teams.Add(team);
}
public void RemoveTeam(string teamName)
{
for (int i = 0; i < Teams.Count(); i++)
{
if (Teams[i].TeamName == teamName)
{
Teams.RemoveAt(i);
}
}
throw new Exception("Team not found.");
}
public void DisplayClubInfo()
{
Console.WriteLine("ClubName : " + ClubName);
for(int i = 0; i < Teams.Count() - 1; i++)
{
System.Console.WriteLine(Teams[i]);
Teams[i].Players.PrintAll();
}
}
public override string ToString()
{
return $"ClubName : {ClubName}, with {Teams.Count()}";
}
public MyList<Player> GetPlayersAboveRating(int threshold)
{
MyList<Player> result = new MyList<Player>();
for (int i = 0; i < Teams.Count(); i++)
{
for (int j = 0; j < Teams[i].Players.Count(); j++)
{
if (Teams[i].Players[j].Rating > threshold)
result.Add(Teams[i].Players[j]);
}
}
return result;
}
public Coach GetMostExperiencedCoach()
{
Coach best = null;
for (int i = 0; i < Teams.Count(); i++)
{
Team team = Teams[i];
if (best == null || team.TeamCoach.ExperienceScore >
best.ExperienceScore)
best = team.TeamCoach;
}
return best;
}
}
}
}
public enum Position
{
Goalkeeper,
Defender,
Midfielder,
Striker,
Winger,
Fullback,
Sweeper
}

You might also like