Im writing a program where I have a Deck of cards (MyDeck Object) which contains a list (List of type Card) within which are my cards (Card Objects).
There is also a Magazine (Magazine Object) with a list of cards (List of type string) for sale. I want to add cards that are in the magazine to my deck
only if I dont have the card yet. The difficult part is that my cards are objects while the ones in the magazine are strings, so I have to call mycard.getCardName()
to get the name of my card and with the cards in the magazine I can simply run down the list because they are simply a list of strings.
Here is my code for that and the problem is that only the first 2 cards in the magazine are added to my deck.
Perhaps Im approaching the problem wrong.
public static class MyDeck
{
public static List<Card> Cards = new List<Card>();
//Other Members
}
public class Card
{
//associated memebers
}
public class Magazine
{
public List<string> MagCards = new List<string>();
//Other Members
}
private void AddCardsToMyDeck(List<string> MagCards)
{
//If I have no cards in my deck then add the first one on the magazine to my deck
if(MyDeck.MyCards.Count == 0)
{
Card c = new Card(MagCards[0]);
MyDeck.MyCards.Add(c);
}
//for each name of each card in the magazine
foreach(string MagCard in MagCards)
{
//for each card in my deck
foreach (Card crd in MyDeck.MyCards)
{
//if the my card is in the magazine then don't add it, else add it
if (MagCards.Contains(crd.getCardName()))
{
break;
}
else
{
Card cd = new Card(MagCard);
MyDeck.MyCards.Add(cd);
}
}
}
}
// NOTE: MyDeck.Cards is empty
Magazine.MagCards.Add("B. Ruth");
Magazine.MagCards.Add("M. Piazza");
Magazine.MagCards.Add("J. Robinson");
AddCardsToMyDeck(Magazine.MagCards);
//Only Ruth and Piazza are added