This commit is contained in:
Rens Pastoor
2025-06-12 11:20:08 +02:00
parent 37013ec1fc
commit 1086760c4a
21 changed files with 444 additions and 256 deletions

View File

@@ -18,101 +18,107 @@ using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
namespace PoloClubApp {
class Club {
private string name; // the name of the club
private List<Device> devices; // a list of devices
namespace PoloClubApp
{
/// <summary>
/// Represents a Polo Club that manages a collection of devices.
/// </summary>
class Club
{
private string name;
private List<Device> devices;
public Club(string name){
/// <summary>
/// Initializes a new instance of the Club class.
/// </summary>
/// <param name="name">The name of the club.</param>
public Club(string name)
{
this.name = name;
this.devices = new List<Device>();
}
public string Name { get { return this.name; } } // read only property for Name
/// <summary>
/// Gets the name of the club (read-only).
/// </summary>
public string Name => this.name;
// ----- Graded Methods -----
//-----Provide your answers here-----
public List<Device> GetAllWearables(){
List<Device> wearables = new List<Device>();
foreach (Device dev in this.devices){
if (dev is SmartWatch || dev is FitTracker){
wearables.Add(dev);
}
}
return wearables;
}
public void AssignDevice(int id, string playerName){
Device device = GetDeviceById(id);
if(device == null){
throw new Exception("Device not found");
}
if (!(device is IWearable)){
device.AssignDevice(playerName, null);
} else {
IWearable wearable = (IWearable)device;
device.AssignDevice(playerName, wearable.GetWaterResistanceMeters());
}
}
public bool ReturnDevice(int id){
Device device = GetDeviceById(id);
return device.ReturnDevice();
/// <summary>
/// Retrieves all wearable devices (implementing IWearable interface).
/// </summary>
/// <returns>List of wearable devices.</returns>
public List<Device> GetAllWearables()
{
return devices.Where(d => d is IWearable).ToList();
}
public List<Device> GetAllAssignedDevicesByPlayer(string playerName){
List<Device> assignedDevices = new List<Device>();
foreach (Device dev in this.devices){
if (dev.PlayerName == playerName){
assignedDevices.Add(dev);
}
}
return assignedDevices;
/// <summary>
/// Assigns a device to a player.
/// </summary>
/// <param name="id">Device ID.</param>
/// <param name="playerName">Player's name.</param>
/// <exception cref="Exception">Thrown if device is not found.</exception>
public void AssignDevice(int id, string playerName)
{
var device = GetDeviceById(id) ?? throw new Exception("Device not found");
var waterResistance = (device as IWearable)?.GetWaterResistanceMeters();
device.AssignDevice(playerName, waterResistance);
}
public List<String> GenerateReportPerPlayer(string playerName){
string returnString = "List of devices assigned to " + playerName;
List<String> lines = new List<String>();
List<Device> assignedDevices = GetAllAssignedDevicesByPlayer(playerName);
lines.Add(returnString);
string currentDeviceType = "SmartPhone";
lines.Add("Phones");
lines.Add("-");
foreach (Device dev in assignedDevices){
if (dev is SmartPhone){
lines.Add(dev.GetDetails());
}
}
lines.Add("Wearables");
lines.Add("-");
foreach (Device dev in assignedDevices){
if (!(dev is SmartPhone)){
lines.Add(dev.GetDetails());
}
}
int phoneCount = 0;
int wearableCount = 0;
foreach (Device dev in assignedDevices){
if (dev is SmartPhone){
phoneCount++;
}
if (dev is IWearable){
wearableCount++;
}
}
lines.Add("Total: " + assignedDevices.Count + " devices, " + phoneCount + " phones and " + wearableCount + " wearables");
return lines;
/// <summary>
/// Returns a device from a player.
/// </summary>
/// <param name="id">Device ID.</param>
/// <returns>True if successful, false if device not found.</returns>
public bool ReturnDevice(int id)
{
var device = GetDeviceById(id);
return device?.ReturnDevice() ?? false;
}
/// <summary>
/// Gets all devices assigned to a specific player.
/// </summary>
/// <param name="playerName">Player's name.</param>
/// <returns>List of assigned devices.</returns>
public List<Device> GetAllAssignedDevicesByPlayer(string playerName)
{
return devices.Where(d => d.PlayerName == playerName).ToList();
}
/// <summary>
/// Generates a formatted device report for a player.
/// </summary>
/// <param name="playerName">Player's name.</param>
/// <returns>Formatted report lines.</returns>
public List<string> GenerateReportPerPlayer(string playerName)
{
var assignedDevices = GetAllAssignedDevicesByPlayer(playerName);
var report = new List<string>
{
$"List of devices assigned to {playerName}",
"Phones",
"-"
};
report.AddRange(assignedDevices
.OfType<SmartPhone>()
.Select(d => d.GetDetails()));
report.AddRange(new[] { "Wearables", "-" });
report.AddRange(assignedDevices
.Where(d => d is IWearable)
.Select(d => d.GetDetails()));
report.Add($"Total: {assignedDevices.Count} devices, " +
$"{assignedDevices.Count(d => d is SmartPhone)} phones and " +
$"{assignedDevices.Count(d => d is IWearable)} wearables");
return report;
}
// -----The provided code below will not be graded, therefore should not be changed-----