using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace WPFDataBinding
{
public partial class Window1
{
public Window1()
{
InitializeComponent();
WithMembersOfClubs membersOfClubs = new WithMembersOfClubs();
/* Create your first club */
Club club = membersOfClubs.InitializeNewClub("Bondi Club", "33 Bondibeach Park", "0123456789");
/* Create your first player */
TennisPlayer tennisPlayer1 = membersOfClubs.InitializeNewPlayer("Sheepings", "Sheep ville", "0234567890");
/* If member does not exist, write that he joined the club */
if (membersOfClubs.AddTennisPlayerAsMember(tennisPlayer1, club))
Debug.WriteLine(string.Join(" ", tennisPlayer1.PlayerName, "joined", club.ClubName));
/* Create your second player */
TennisPlayer tennisPlayer2 = membersOfClubs.InitializeNewPlayer("Bob", "Bob ville", "3214567890");
/* If member does not exist, write that he joined the club */
if (membersOfClubs.AddTennisPlayerAsMember(tennisPlayer2, club))
Debug.WriteLine(string.Join(" ", tennisPlayer2.PlayerName, "joined", club.ClubName));
/* Lets display the number of members in a given club */
Debug.WriteLine(string.Join(" ", club.ClubName, "has", membersOfClubs.NumberOfMembers_In(club.ClubName, 0), "Members"));
/* Print the names of the players of a given club from the returned list */
membersOfClubs.ListOfPlayerNames_By("Bondi Club").ForEach((string name) => Debug.WriteLine($"Member : {name}"));
/* Remove the member from the given club if they exist */
if (membersOfClubs.RemoveTennisPlayerAsMember(tennisPlayer1))
Debug.WriteLine(string.Join(" ", tennisPlayer1.PlayerName, "left", club.ClubName));
/* How many members remain after removing one? */
Debug.WriteLine(string.Join(" ", club.ClubName, "has", membersOfClubs.NumberOfMembers_In(club.ClubName, 0), "Members"));
}
}
public class Club
{
public string ClubName { get; set; }
public string ClubAddress { get; set; }
public string ClubPhoneNumber { get; set; }
}
public class TennisPlayer
{
public string PlayerName { get; set; }
public string PlayerAddress { get; set; }
public string PlayerPhoneNumber { get; set; }
}
/// <summary>
/// Here you can use an additional class to work with the objects of your other classes; such as Club, and TenniPlayer.
/// This class can add your tennis players to a ConcurrentDictionary<TennisPlayer, Club>. When this class is first instantiated,
/// keep a copy of the instantiated object to pass around your application. New instances of this class will create a new instance of _PlayerClubPair.
/// </summary>
public class WithMembersOfClubs
{
/// <summary>
/// This Concurrent Dictionary keeps track of your tenis players and the clubs they are in.
/// </summary>
private readonly ConcurrentDictionary<TennisPlayer, Club> _PlayerClubPair = new ConcurrentDictionary<TennisPlayer, Club>();
/// <summary>
/// The TennisPlayer is initialised with a new instance of a TennisPlayer object and its details are provided upon instantiation.
/// </summary>
/// <param name="playerName">The name of your tennis player</param>
/// <param name="playerAddress">The address of your tennis player</param>
/// <param name="playerPhoneNum">The phone number of your tennis player</param>
/// <returns>Returns the TennisPlayer you created</returns>
public TennisPlayer InitializeNewPlayer(string playerName, string playerAddress, string playerPhoneNum) => new TennisPlayer
{
PlayerName = playerName,
PlayerAddress = playerAddress,
PlayerPhoneNumber = playerPhoneNum
};
/// <summary>
/// The Club class is initialised with a new instance of a club object and its details are provided upon instantiation.
/// </summary>
/// <param name="clubName">The name of the club is provided here</param>
/// <param name="clubAddress">The club address is provided here</param>
/// <param name="clubPhoneNum">The clubs phone number is provided here</param>
/// <returns>Returns the Club you created</returns>
public Club InitializeNewClub(string clubName, string clubAddress, string clubPhoneNum) => new Club
{
ClubName = clubName,
ClubAddress = clubAddress,
ClubPhoneNumber = clubPhoneNum
};
/// <summary>
/// This method will add players to your _PlayerClubPair collection and store the values of your tennis player and club in a key value pair <TennisPlayer, Club>.
/// </summary>
/// <param name="tennisPlayer">This is where you pass in the tennis player object</param>
/// <param name="clubName">This is where you pass in the club object</param>
public bool AddTennisPlayerAsMember(TennisPlayer tennisPlayer, Club clubName) => _PlayerClubPair.TryAdd(tennisPlayer, clubName);
/// <summary>
/// This method will remove all references of a TennisPlayer object passed into it.
/// </summary>
/// <param name="tennisPlayer">This is where you pass in the tennis player you wish to remove</param>
public bool RemoveTennisPlayerAsMember(TennisPlayer tennisPlayer) => _PlayerClubPair.TryRemove(tennisPlayer, out _);
public List<string> ListOfPlayerNames_By(string clubName) => (_PlayerClubPair.Where(pair => pair.Value.ClubName == clubName).Select(pair => pair.Key.PlayerName)).ToList();
/// <summary>
/// This method allows you to check the number of members in a given club object.
/// </summary>
/// <param name="clubName">The name of the club you want to check the number of players in</param>
/// <param name="number">The number variable will be used to count how many members are in a given club</param>
/// <returns></returns>
public int NumberOfMembers_In(string clubName, int number)
{
number += _PlayerClubPair.Values.Count(club => club.ClubName == clubName);
return number;
}
}
}