Wrote the functions of VocabularyItem.cs

This commit is contained in:
MarcUs7i 2024-10-28 10:39:15 +01:00
parent 26c8e67c33
commit bed0769995
2 changed files with 41 additions and 9 deletions

View file

@ -88,7 +88,30 @@ public sealed class Trainer
/// <returns>A <see cref="VocabularyItem"/> for each (valid) word</returns>
private static VocabularyItem[] CreateVocabularyItems(string[][] wordsAndTranslations)
{
// TODO
return [];
VocabularyItem[] vocabularyItemsTemp = new VocabularyItem[wordsAndTranslations.Length];
bool[] definedItems = new bool[wordsAndTranslations.Length];
int usedItems = 0;
for (var i = 0; i < wordsAndTranslations.Length; i++)
{
if (wordsAndTranslations[i].Length == 2)
{
usedItems++;
definedItems[i] = true;
vocabularyItemsTemp[i] = new VocabularyItem(wordsAndTranslations[i][WordIdx], wordsAndTranslations[i][TranslationIdx]);
}
}
VocabularyItem[] vocabularyItems = new VocabularyItem[usedItems];
int j = 0;
for (int i = 0; i < wordsAndTranslations.Length; i++)
{
if (definedItems[i])
{
vocabularyItems[j] = vocabularyItemsTemp[i];
j++;
}
}
return vocabularyItems;
}
}

View file

@ -10,7 +10,7 @@ public sealed class VocabularyItem
public readonly string NativeWord;
public readonly string Translation;
VocabularyItem(string nativeWord, string translation)
public VocabularyItem(string nativeWord, string translation)
{
NativeWord = nativeWord;
Translation = translation;
@ -41,7 +41,18 @@ public sealed class VocabularyItem
/// <returns>0 if equal; less than 0 if this item is smaller; greater than 0 otherwise</returns>
public int CompareTo(VocabularyItem other)
{
return -1;
if (_countCorrect == other._countCorrect)
{
return CompareStrings(NativeWord, other.NativeWord);
}
else if (_countCorrect < other._countCorrect)
{
return 1;
}
else
{
return -1;
}
}
/// <summary>
@ -50,7 +61,7 @@ public sealed class VocabularyItem
/// <returns>A string containing the word, its translation and the training statistics</returns>
public override string ToString()
{
return string.Empty;
return $"{NativeWord,-10} {Translation,-10} {_countAsked,-5} {_countCorrect,-7}";;
}
/// <summary>
@ -60,8 +71,6 @@ public sealed class VocabularyItem
/// <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)
{
return -1;
}
private static int CompareStrings(string a, string b) => string.Compare(a, b, StringComparison.OrdinalIgnoreCase);
}