1

i'm having a bit of trouble on the last bit of my program. I'm not too sure how to calculate int's within my array. Here's my code so far:

class Tester
{
    static void Main(string[] args)
    {
        Card[] hand = {
                          new Card("Spade", 3),
                          new Card("Club", 10),
                          new Card("Diamond", 11),
                          new Card("Heart", 9),
                          new Card("Diamond", 13),
                      };
        ProcessHand(hand);
    }//end of static void main

    static void ProcessHand(Card[] cards)
    {
        cards[0].DisplayCard();
        cards[1].DisplayCard();
        cards[2].DisplayCard();
        cards[3].DisplayCard();
        cards[4].DisplayCard();
    }//end of static void processhand
}//end of class Tester


class Card
{
    public string suit { get; set; }
    public int facevalue { get; set; }
    public Card (string su, int fa) 
    {
        suit = su;
        facevalue = fa;
    }
    public int Sum(params int[] facevalue)// <- trying to calculate sum of facevalues of cards.
    {
        return facevalue.Sum();
    }
    public void DisplayCard()
    {
        Console.WriteLine("The card is  {0,-10} {1,-10}", suit, facevalue);

    }
}//end of class Card

I'm a bit confused on where to firstly put the code, as i'm trying to put it in the class Card. I've tried different variants of everything i know, within both classes but nothing seems to work. I'm simply wanting to sum all the facevalues of the cards shown at the top, and then use console.writeline to show the total value.

Any help would be appreciated! thanks :)

0

3 Answers 3

1

You can do this instead, remove the Sum function on your class Card, and put it under your static void Main as static function:

private static void Main(string[] args)
    {
        Card[] hand =
            {
                new Card("Spade", 3),
                new Card("Club", 10),
                new Card("Diamond", 11),
                new Card("Heart", 9),
                new Card("Diamond", 13),
            };
        ProcessHand(hand);
        Console.WriteLine(Sum(hand.Select(x=>x.facevalue).ToArray()));
    }

    static int Sum(params int[] facevalue)// <- trying to calculate sum of facevalues of cards.
    {
        return facevalue.Sum();
    }

I remove this Sum function outside your class since you are creating an instance of class which will not make sense if you are computing different instance of class. So rather put it as static function instead then you can compute from there.

Sign up to request clarification or add additional context in comments.

Comments

1

Made some changes to your code and I'll explain those.

Class Card

    class Card
    {
        public string Suit { get; }
        public int FaceValue { get; }
        public Card(string su, int fa)
        {
            Suit = su;
            FaceValue = fa;
        }

        public void DisplayCard()
        {
            Console.WriteLine("The card is  {0,-10} {1,-10}", Suit, FaceValue);
        }
    }

Note the changes:

  • Naming convention for the properties
  • Properties are readonly now
  • Sum() method doesn't belong to the Card class. The method has no direct relationship with a Card object. It has a relationship with the Card class. So, you can have a static method inside the class like:

Card class with Static method Sum

    class Card
    {
        public string Suit { get; }
        public int FaceValue { get; }
        public Card(string su, int fa)
        {
            Suit = su;
            FaceValue = fa;
        }

        public void DisplayCard()
        {
            Console.WriteLine("The card is  {0,-10} {1,-10}", Suit, FaceValue);
        }

        public static int Sum(Card[] cards)
        {
            return cards.Sum(c => c.FaceValue);
        }
    }

Changes to ProcessHand method

    static int ProcessHand(Card[] cards)
    {
        var sum = 0;
        foreach (var card in cards)
        {
            card.DisplayCard();
            sum += card.FaceValue;
        }

        return sum;
    }
  • Iterate over the list of cards and DisplayCard() and add to the sum
  • Not using Sum() because it iterates over the array again (after the foreach loop), which seems unnecessary.
  • The method returns the sum of the cards (my personal architectural decision)

Main

    public static void Main(string[] args)
    {
        Card[] hand = {
                      new Card("Spade", 3),
                      new Card("Club", 10),
                      new Card("Diamond", 11),
                      new Card("Heart", 9),
                      new Card("Diamond", 13),
                  };
        var sum = ProcessHand(hand);
        Console.WriteLine($"The sum is {sum}");
    }

Comments

1

I don't think Sum should be part of Card. That would imply that any single Card instance knows about your entire hand, but it doesn't. The concept of a "hand" object is missing from your current program, and I think introducing a new class would add some necessary separation of duties. This new class Hand should contain your array of Cards and your processing logic:

public class Hand
{
    private readonly Card[] _cards;

    public Hand(Card[] cards)
    {
        _cards = cards;
    }

    public void Process()
    {
        // You can use a loop and avoid hard-coding the indices
        foreach (var card in _cards)
        {
            card.DisplayCard();
        }
    }
}

To Hand you could add a method or read-only property called TotalFaceValue that calculates the total face value.

// Read-only property
public int TotalFaceValue
{
    get { return _cards.Sum(c => c.facevalue); }
}

// Method
public int GetTotalFaceValue()
{
    return _cards.Sum(c => c.facevalue);
}

I'm going to opt for the former. All together, your new code might look like:

class Tester
{
    static void Main(string[] args)
    {
        // Fill a new hand with your cards
        var hand = new Hand(new [] {
            new Card("Spade", 3),
            new Card("Club", 10),
            new Card("Diamond", 11),
            new Card("Heart", 9),
            new Card("Diamond", 13),
        });

        // "process" (display) your cards values
        hand.Process();

        // Shows the total hand value. This could also be moved into
        // the Process() function if you want
        Console.WriteLine("Total face value: {0}", hand.TotalFaceValue);

    } //end of static void main


}//end of class Tester

public class Hand
{
    private readonly Card[] _cards;

    public Hand(Card[] cards)
    {
        _cards = cards;
    }

    public void Process()
    {
        foreach (var card in _cards)
        {
            card.DisplayCard();
        }
    }

    public int TotalFaceValue
    {
        get { return _cards.Sum(c => c.facevalue); }
    }
}    

class Card
{
    public string suit { get; set; }
    public int facevalue { get; set; }
    public Card (string su, int fa) 
    {
        suit = su;
        facevalue = fa;
    }

    public void DisplayCard()
    {
        Console.WriteLine("The card is  {0,-10} {1,-10}", suit, facevalue);

    }
}//end of class Card

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.