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-----

View File

@@ -17,40 +17,81 @@ using System.Text;
using System.Threading.Tasks;
namespace PoloClubApp {
internal abstract class Device {
/// <summary>
/// Abstract base class representing a device in the Polo Club system.
/// </summary>
public abstract class Device {
/// <summary>
/// Gets the unique identifier of the device.
/// </summary>
public int Id { get; }
/// <summary>
/// Gets the name of the device.
/// </summary>
public string Name { get; }
/// <summary>
/// Gets or sets the name of the player the device is assigned to.
/// </summary>
public string PlayerName { get; set; }
public Device(int id, string name, string playerName = null){
/// <summary>
/// Initializes a new instance of the Device class.
/// </summary>
/// <param name="id">The unique identifier for the device.</param>
/// <param name="name">The name of the device.</param>
/// <param name="playerName">The name of the player the device is assigned to (optional).</param>
protected Device(int id, string name, string playerName = null) {
Id = id;
Name = name;
PlayerName = playerName;
}
/// <summary>
/// Gets detailed information about the device.
/// </summary>
/// <returns>Formatted string with device details.</returns>
public abstract string GetDetails();
/// <summary>
/// Checks if the device is currently assigned to a player.
/// </summary>
/// <returns>True if assigned, false otherwise.</returns>
public bool IsAssigned(){
return PlayerName != null;
}
public Exception AssignDevice(string playerName, int? waterResistanceMeters){
if (IsAssigned()){
throw new Exception("Device is already assigned");
} else if (this is IWearable && waterResistanceMeters < 3){
throw new Exception("Water resistance meters should be 3 or more");
} else if (this.Id == null){
throw new Exception("Device Id is null");
} else {
PlayerName = playerName;
return null;
}
return !string.IsNullOrEmpty(PlayerName);
}
/// <summary>
/// Assigns the device to a player.
/// </summary>
/// <param name="playerName">Name of the player to assign to.</param>
/// <param name="waterResistanceMeters">Water resistance for wearables (nullable).</param>
/// <exception cref="Exception">Throws when assignment fails.</exception>
public void AssignDevice(string playerName, int? waterResistanceMeters)
{
if (IsAssigned())
throw new Exception("Device is already assigned");
if (this is IWearable && waterResistanceMeters < 3)
throw new Exception("Water resistance must be 3 meters or more for wearables");
if (Id == 0)
throw new Exception("Invalid device ID");
PlayerName = playerName;
}
/// <summary>
/// Returns the device from a player.
/// </summary>
/// <returns>True if successful, false if device wasn't assigned.</returns>
public bool ReturnDevice(){
if (!IsAssigned()){
return false;
}
PlayerName = null;
return true;
if (!IsAssigned())
return false;
PlayerName = null;
return true;
}
}
}

View File

@@ -17,27 +17,57 @@ using System.Text;
using System.Threading.Tasks;
namespace PoloClubApp {
internal class FitTracker : Device, IWearable {
public string DeviceType = "FitTracker";
public int Id { get; }
public string Name { get; }
public string PlayerName { get; set; }
/// <summary>
/// Represents a fitness tracker device that implements IWearable interface.
/// </summary>
public class FitTracker : Device, IWearable {
/// <summary>
/// Gets the type of the device (always "FitTracker").
/// </summary>
public string DeviceType { get; } = "FitTracker";
/// <summary>
/// Gets the water resistance rating in meters.
/// </summary>
public int WaterResistanceMeters { get; }
/// <summary>
/// Gets the color of the fitness tracker.
/// </summary>
public string Color { get; }
public FitTracker(int id, string name, int waterResistanceMeters, string color) : base(id, name, null){
Id = id;
Name = name;
/// <summary>
/// Initializes a new instance of the FitTracker class.
/// </summary>
/// <param name="id">The unique device identifier.</param>
/// <param name="name">The name of the fitness tracker.</param>
/// <param name="waterResistanceMeters">Water resistance rating in meters.</param>
/// <param name="color">The color of the device.</param>
public FitTracker(int id, string name, int waterResistanceMeters, string color)
: base(id, name)
{
WaterResistanceMeters = waterResistanceMeters;
Color = color;
}
public override string GetDetails(){
return DeviceType + "Id: " + Id + ", Name: " + Name + ", Type: " + ", WaterResistanceMeters: " + WaterResistanceMeters + ", Color: " + Color;
/// <summary>
/// Gets detailed information about the fitness tracker.
/// </summary>
/// <returns>Formatted string with device details.</returns>
public override string GetDetails()
{
return $"{DeviceType} - Id: {Id}, Name: {Name}, " +
$"Water Resistance: {WaterResistanceMeters}m, " +
$"Color: {Color}";
}
public int GetWaterResistanceMeters(){
/// <summary>
/// Gets the water resistance rating.
/// </summary>
/// <returns>Water resistance in meters.</returns>
public int GetWaterResistanceMeters()
{
return WaterResistanceMeters;
}
}
}
}

View File

@@ -18,8 +18,14 @@ using System.Text;
using System.Threading.Tasks;
namespace PoloClubApp {
interface IWearable {
///Returns the water resistance level of the wearable device in meters.
/// <summary>
/// Interface for wearable devices that have water resistance capability.
/// </summary>
public interface IWearable{
/// <summary>
/// Gets the water resistance rating of the wearable device.
/// </summary>
/// <returns>Water resistance in meters.</returns>
int GetWaterResistanceMeters();
}
}
}

View File

@@ -17,18 +17,30 @@ using System.Text;
using System.Threading.Tasks;
namespace PoloClubApp {
internal class SmartPhone : Device {
public string DeviceType = "SmartPhone";
public int Id { get; }
public string Name { get; }
public string PlayerName { get; set; }
public SmartPhone(int id, string name) : base(id, name, null){
Id = id;
Name = name;
}
public override string GetDetails(){
return DeviceType + "Id: " + Id + ", Name: " + Name;
/// <summary>
/// Represents a smartphone device in the Polo Club system.
/// </summary>
public class SmartPhone : Device
{
/// <summary>
/// Gets the type of the device (always "SmartPhone").
/// </summary>
public string DeviceType { get; } = "SmartPhone";
/// <summary>
/// Initializes a new instance of the SmartPhone class.
/// </summary>
/// <param name="id">The unique identifier for the smartphone.</param>
/// <param name="name">The name of the smartphone.</param>
public SmartPhone(int id, string name) : base(id, name) { }
/// <summary>
/// Gets a formatted string with details about the smartphone.
/// </summary>
/// <returns>A string containing device details.</returns>
public override string GetDetails()
{
return $"{DeviceType} - Id: {Id}, Name: {Name}";
}
}
}

View File

@@ -18,25 +18,57 @@ using System.Threading.Tasks;
using System.Xml.Linq;
namespace PoloClubApp {
internal class SmartWatch : Device, IWearable {
public string DeviceType = "Watch";
/// <summary>
/// Represents a smartwatch device that implements IWearable interface.
/// </summary>
public class SmartWatch : Device, IWearable {
/// <summary>
/// Gets the type of the device (always "SmartWatch").
/// </summary>
public string DeviceType { get; } = "SmartWatch";
/// <summary>
/// Gets the water resistance rating in meters.
/// </summary>
public int WaterResistanceMeters { get; }
/// <summary>
/// Gets the screen size in millimeters.
/// </summary>
public int ScreenSize { get; }
/// Initializes a new SmartWatch with specified ID, name, player name, water resistance, and screen size.
public SmartWatch(int id, string name, int waterResistanceMeters, int screenSize) : base(id, name, null){
WaterResistanceMeters = waterResistanceMeters;
ScreenSize = screenSize;
/// <summary>
/// Initializes a new instance of the SmartWatch class.
/// </summary>
/// <param name="id">The unique device identifier.</param>
/// <param name="name">The name of the smartwatch.</param>
/// <param name="waterResistanceMeters">Water resistance rating in meters.</param>
/// <param name="screenSize">Screen size in millimeters.</param>
public SmartWatch(int id, string name, int waterResistanceMeters, int screenSize)
: base(id, name)
{
WaterResistanceMeters = waterResistanceMeters;
ScreenSize = screenSize;
}
/// Returns a string representation of the SmartWatch details.
public override string GetDetails(){
return DeviceType + "Id: " + Id + ", Name: " + Name + ", WaterResistanceMeters: " + WaterResistanceMeters + ", ScreenSize: " + ScreenSize;
/// <summary>
/// Gets detailed information about the smartwatch.
/// </summary>
/// <returns>Formatted string with device details.</returns>
public override string GetDetails()
{
return $"{DeviceType} - Id: {Id}, Name: {Name}, " +
$"Water Resistance: {WaterResistanceMeters}m, " +
$"Screen Size: {ScreenSize}mm";
}
/// Returns the water resistance of the SmartWatch in meters.
public int GetWaterResistanceMeters(){
/// <summary>
/// Gets the water resistance rating.
/// </summary>
/// <returns>Water resistance in meters.</returns>
public int GetWaterResistanceMeters()
{
return WaterResistanceMeters;
}
}
}
}