namespace VocabularyTrainer;
///
/// Represents one word in the vocabulary
///
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;
}
///
/// A translation attempt is checked for correctness.
///
/// The user provided translation
/// True if the translation was correct; false otherwise
public bool TestTranslation(string translationAttempt)
{
_countAsked++;
if (string.Equals(Translation, translationAttempt, StringComparison.CurrentCultureIgnoreCase))
{
_countCorrect++;
return true;
}
return false;
}
///
/// 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.
///
/// The to compare with
/// 0 if equal; less than 0 if this item is smaller; greater than 0 otherwise
public int CompareTo(VocabularyItem other)
{
if (_countCorrect == other._countCorrect)
{
return CompareStrings(NativeWord, other.NativeWord);
}
else if (_countCorrect < other._countCorrect)
{
return 1;
}
else
{
return -1;
}
}
///
/// Overrides the default string representation to display the word and translation statistics.
///
/// A string containing the word, its translation and the training statistics
public override string ToString()
{
return $"{NativeWord,-10} {Translation,-10} {_countAsked,-5} {_countCorrect,-7}";;
}
///
/// Compares two strings by ordinal value, ignoring case.
///
/// First string
/// Second string
/// Less than 0 if a precedes b in the sorting order; greater than 0 if b precedes a; 0 otherwise
private static int CompareStrings(string a, string b) => string.Compare(a, b, StringComparison.OrdinalIgnoreCase);
}