Removed a bug in the AddParticipants method

And completed the GetResultList method
This commit is contained in:
MarcUs7i 2024-12-06 12:37:02 +01:00
parent 19eeb95db3
commit 90a33cdfde
2 changed files with 55 additions and 2 deletions

View file

@ -8,6 +8,11 @@ public sealed class Marathon
public readonly DateOnly Date; public readonly DateOnly Date;
public int ParticipantCount { get; private set; } public int ParticipantCount { get; private set; }
/// <summary>
/// The constructor for the Marathon class
/// </summary>
/// <param name="city">The city</param>
/// <param name="date">The Date of the Marathon</param>
public Marathon(string city, DateOnly date) public Marathon(string city, DateOnly date)
{ {
City = city; City = city;
@ -36,6 +41,8 @@ public sealed class Marathon
_tail = middleNode; _tail = middleNode;
return; return;
} }
// Checking all the possibilities
if(leftNode == null && rightNode != null) if(leftNode == null && rightNode != null)
{ {
_head = middleNode; _head = middleNode;
@ -53,9 +60,21 @@ public sealed class Marathon
{ {
return; return;
} }
if (insertIndex == 0)
{
MoveNode(middleNode, rightNode);
_head = middleNode;
return;
}
MoveNode(leftNode, rightNode, middleNode); MoveNode(leftNode, rightNode, middleNode);
} }
/// <summary>
/// Removes a participant from the list
/// </summary>
/// <param name="startNo">The start number of the participant to remove</param>
/// <returns>true: if the operation succeeded; false: if it failed</returns>
public bool RemoveParticipant(int startNo) public bool RemoveParticipant(int startNo)
{ {
Node? nodeToRemove = GetNodeByStartNo(startNo); Node? nodeToRemove = GetNodeByStartNo(startNo);
@ -86,11 +105,35 @@ public sealed class Marathon
return true; return true;
} }
/// <summary>
/// The list of participants in the marathon
/// </summary>
/// <returns>the array string of the list of participants</returns>
public string[] GetResultList() public string[] GetResultList()
{ {
return Array.Empty<string>(); if(ParticipantCount == 0)
{
return [];
}
string[] result = new string[ParticipantCount];
for (int i = 0; i < ParticipantCount; i++)
{
Node? current = GetNodeByIndex(i);
if (current == null)
{
return [];
}
result[i] = $"#{i+1:00} {current.Data}";
}
return result;
} }
/// <summary>
/// Returns the string representation of the Marathon
/// </summary>
/// <returns>the string representation</returns>
public override string ToString() public override string ToString()
{ {
return $"{City} marathon on {Date.ToString(Const.Culture)}"; return $"{City} marathon on {Date.ToString(Const.Culture)}";
@ -186,6 +229,11 @@ public sealed class Marathon
startingNode.Next = middleNode; startingNode.Next = middleNode;
} }
/// <summary>
/// Gets the node by the index
/// </summary>
/// <param name="index">The index to return</param>
/// <returns>the found node</returns>
Node? GetNodeByIndex(int index) Node? GetNodeByIndex(int index)
{ {
if(index <= 0) if(index <= 0)
@ -206,6 +254,11 @@ public sealed class Marathon
return current; return current;
} }
/// <summary>
/// Gets the node by the start number
/// </summary>
/// <param name="startNo">The start number</param>
/// <returns>the found node</returns>
Node? GetNodeByStartNo(int startNo) Node? GetNodeByStartNo(int startNo)
{ {
Node? current = _head; Node? current = _head;

View file

@ -24,7 +24,7 @@ public sealed class Participant
// Check if the completion time is the same // Check if the completion time is the same
if(CompletionTime == participant.CompletionTime) if(CompletionTime == participant.CompletionTime)
{ {
return StartNo > participant.StartNo ? -1 : 1; return StartNo < participant.StartNo ? -1 : 1;
} }
return CompletionTime < participant.CompletionTime ? -1 : 1; return CompletionTime < participant.CompletionTime ? -1 : 1;