Skip to content

Commit

Permalink
Edited GetValidCopies function to not check for 0 inputs.
Browse files Browse the repository at this point in the history
Edited ToString to show foil copies and added this to the constructor and CardAdd function.
Card edit function has been fixed to edit inputs to the correct data types.
Can search for last added document.
  • Loading branch information
TcPirate1 committed Dec 19, 2023
1 parent 3b5d6d8 commit 5857dfb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
62 changes: 44 additions & 18 deletions Card.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ internal class Card : CardFunctions

[BsonElement("Foil?")]
public bool IsFoil { get; private set;}
[BsonElement("Foil_copies")]
public int FoilCopies { get; private set;}

public Card(string name, string image, string type, int cost, IEnumerable<string> specialIcons, IEnumerable<string> elements, string code, int copies, bool isFoil)
public Card(string name, string image, string type, int cost, IEnumerable<string> specialIcons, IEnumerable<string> elements, string code, int copies, bool isFoil, int foilCopies)
{
Name = FirstCharUpper(name);
Image = image.Trim();
Expand All @@ -48,6 +50,7 @@ public Card(string name, string image, string type, int cost, IEnumerable<string
Code = ValidateAndFormatCardCode(code);
Copies = copies;
IsFoil = isFoil;
FoilCopies = foilCopies;
}

internal static bool CardAdd(IMongoCollection<BsonDocument> cardCollection)
Expand All @@ -68,7 +71,6 @@ internal static bool CardAdd(IMongoCollection<BsonDocument> cardCollection)

Console.WriteLine("\nImage location: ");
string image = Console.ReadLine()!.Trim();
CheckEmptyString(image);

Console.WriteLine("\nWhat is the card's type?");
string type = Console.ReadLine()!.Trim();
Expand All @@ -91,7 +93,18 @@ internal static bool CardAdd(IMongoCollection<BsonDocument> cardCollection)
Console.WriteLine("\nIs this card a foil?\nEnter 'y' for yes and 'n' for no.");
bool isFoil = GetIsFoil();

var newCard = new Card(cardName, image, type, cost, iconsArray, elementsArray, code, copies, isFoil);
int foilCopies;
if (isFoil)
{
Console.WriteLine("\nHow many foil copies are there?");
foilCopies = GetValidCopies();
}
else
{
foilCopies = 0;
}

var newCard = new Card(cardName, image, type, cost, iconsArray, elementsArray, code, copies, isFoil, foilCopies);

var newDocument = new BsonDocument
{
Expand All @@ -103,7 +116,8 @@ internal static bool CardAdd(IMongoCollection<BsonDocument> cardCollection)
{ "Elements", new BsonArray(newCard.Elements)},
{ "Card_code", newCard.Code},
{ "Copies", newCard.Copies},
{ "Foil?", newCard.IsFoil}
{ "Foil?", newCard.IsFoil},
{ "Foil_copies", newCard.FoilCopies}
};
cardCollection.InsertOne(newDocument);

Expand All @@ -112,23 +126,29 @@ internal static bool CardAdd(IMongoCollection<BsonDocument> cardCollection)

internal static void CardFind(IMongoCollection<Card> cardCollection)
{
Console.WriteLine("\nWould you like to find by code or name? c = code, n = name");
Console.WriteLine("What would you like to search for?\nl = find last card added, c = find card by code, n = find card by name");
char input = Console.ReadLine()!.Trim().ToLower()[0];
while (input != 'c' && input != 'n')
while (input != 'c' && input != 'n' && input != 'l')
{
Console.WriteLine("Please enter 'c' or 'n'");
Console.WriteLine("Please enter 'c' or 'n' or 'l'");
input = Console.ReadLine()!.Trim().ToLower()[0];
}

if (input == 'c')
if (input == 'l')
{
var sort = Builders<Card>.Sort.Descending("_id");
var searchResult = cardCollection.Find(new BsonDocument()).Sort(sort).Limit(1).ToList();
DisplaySearchResults(searchResult);
}
else if (input == 'c')
{
Console.WriteLine("Enter code of the card\n");
string code = GetValidCardCode();
var filter = Builders<Card>.Filter.Eq(card => card.Code, code);
var searchResult = cardCollection.Find(filter).ToList();
DisplaySearchResults(searchResult);
}
else
else if (input == 'n')
{
Console.WriteLine("Enter the name of the card\n");
string name = GetValidName();
Expand Down Expand Up @@ -222,11 +242,12 @@ private static void UpdateCard(IMongoCollection<Card> cardCollection, FilterDefi
Console.WriteLine("7. Code");
Console.WriteLine("8. Copies");
Console.WriteLine("9. Foil status");
Console.WriteLine("10. Foil copies");

if (int.TryParse(Console.ReadLine(), out int choice))
{
string fieldToUpdate;
string newValue;
object newValue;

switch (choice)
{
Expand All @@ -248,32 +269,37 @@ private static void UpdateCard(IMongoCollection<Card> cardCollection, FilterDefi
case 4:
fieldToUpdate = "Cost";
Console.WriteLine("Enter the new cost for the card:");
newValue = Console.ReadLine()!.Trim();
newValue = GetValidCost();
break;
case 5:
fieldToUpdate = "Special_icons";
Console.WriteLine("Enter the new Special Icons for the card:");
newValue = Console.ReadLine()!.Trim();
newValue = ParseAndFormatInputArray(Console.ReadLine()!.Trim());
break;
case 6:
fieldToUpdate = "Elements";
Console.WriteLine("Enter the new Elements for the card:");
newValue = Console.ReadLine()!.Trim();
newValue = ParseAndFormatInputArray(Console.ReadLine()!.Trim());
break;
case 7:
fieldToUpdate = "Card_code";
Console.WriteLine("Enter the new Code for the card:");
newValue = Console.ReadLine()!.Trim();
newValue = GetValidCardCode();
break;
case 8:
fieldToUpdate = "Copies";
Console.WriteLine("Enter the new value for number of Copies of this card:");
newValue = Console.ReadLine()!.Trim();
newValue = GetValidCopies();
break;
case 9:
fieldToUpdate = "Foil?";
Console.WriteLine("Enter the new Foil Status of this card:");
newValue = Console.ReadLine()!.Trim();
newValue = GetIsFoil();
break;
case 10:
fieldToUpdate = "Foil_copies";
Console.WriteLine("Enter the new value for number of Foil Copies of this card:");
newValue = GetValidCopies();
break;
default:
Console.WriteLine("Invalid choice. No field updated.");
Expand All @@ -288,7 +314,7 @@ private static void UpdateCard(IMongoCollection<Card> cardCollection, FilterDefi

Console.WriteLine(updateResult.ModifiedCount <= 0
? "No documents were updated."
: $"Updated the '{fieldToUpdate}' field for {updateResult.ModifiedCount} cards with code {code}.");
: $"Updated the '{fieldToUpdate}' field for {updateResult.ModifiedCount} card(s) with code {code}.");
}
else
{
Expand All @@ -298,7 +324,7 @@ private static void UpdateCard(IMongoCollection<Card> cardCollection, FilterDefi
public override string ToString()
{
// Return result as a string
string foilStatus = IsFoil ? "they're foil." : "they are not foil.";
string foilStatus = IsFoil ? $"There are {FoilCopies} foil." : "they are not foil.";
return $"There are {Copies} copies of {Name}({Code}), {foilStatus}";
}
}
Expand Down
4 changes: 2 additions & 2 deletions CardFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ internal static int GetValidCopies()
{
int copies;
string input = Console.ReadLine()!.Trim();
while (!int.TryParse(input, out copies) || copies == 0)
while (!int.TryParse(input, out copies))
{
Console.WriteLine("\nInvalid number or 0 was inputted.\nThere shouldn't be 0 copies of a card in the collection.\nPlease enter a valid number.");
Console.WriteLine("\nInvalid number.\nPlease enter a valid number.");
input = Console.ReadLine()!.Trim();
}
return copies;
Expand Down

0 comments on commit 5857dfb

Please sign in to comment.