76 lines
No EOL
2.5 KiB
C#
76 lines
No EOL
2.5 KiB
C#
namespace VocabularyTrainer;
|
|
|
|
/// <summary>
|
|
/// Represents one word in the vocabulary
|
|
/// </summary>
|
|
public sealed class VocabularyItem
|
|
{
|
|
private int _countCorrect;
|
|
private int _countAsked;
|
|
public readonly string NativeWord;
|
|
public readonly string Translation;
|
|
|
|
public VocabularyItem(string nativeWord, string translation)
|
|
{
|
|
NativeWord = nativeWord;
|
|
Translation = translation;
|
|
}
|
|
|
|
/// <summary>
|
|
/// A translation attempt is checked for correctness.
|
|
/// </summary>
|
|
/// <param name="translationAttempt">The user provided translation</param>
|
|
/// <returns>True if the translation was correct; false otherwise</returns>
|
|
public bool TestTranslation(string translationAttempt)
|
|
{
|
|
_countAsked++;
|
|
if (Translation == translationAttempt)
|
|
{
|
|
_countCorrect++;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The vocabulary item is compared to another. First the number of correct answers is compared.
|
|
/// If it is equal the native words are compared ordinal.
|
|
/// </summary>
|
|
/// <param name="other">The <see cref="VocabularyItem"/> to compare with</param>
|
|
/// <returns>0 if equal; less than 0 if this item is smaller; greater than 0 otherwise</returns>
|
|
public int CompareTo(VocabularyItem other)
|
|
{
|
|
if (_countCorrect == other._countCorrect)
|
|
{
|
|
return CompareStrings(NativeWord, other.NativeWord);
|
|
}
|
|
else if (_countCorrect < other._countCorrect)
|
|
{
|
|
return 1;
|
|
}
|
|
else
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Overrides the default string representation to display the word and translation statistics.
|
|
/// </summary>
|
|
/// <returns>A string containing the word, its translation and the training statistics</returns>
|
|
public override string ToString()
|
|
{
|
|
return $"{NativeWord,-10} {Translation,-10} {_countAsked,-5} {_countCorrect,-7}";;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Compares two strings by ordinal value, ignoring case.
|
|
/// </summary>
|
|
/// <param name="a">First string</param>
|
|
/// <param name="b">Second string</param>
|
|
/// <returns>Less than 0 if a precedes b in the sorting order; greater than 0 if b precedes a; 0 otherwise</returns>
|
|
|
|
private static int CompareStrings(string a, string b) => string.Compare(a, b, StringComparison.OrdinalIgnoreCase);
|
|
|
|
} |