0

This program is supposed to use ArrayList to create a deck of cards. The user enters how many cards to draw and those cards are printed, then the rest of the cards that are remaining in the deck are printed. I got the user's cards to print but I cant figure out how to get the remaining cards in the deck to print. Any help would be appreciated.

public class Card 
{
    private int type, value;
    private String[] cardType = {"Clubs", "Spades", "Diamonds", "Hearts"};
    private String[] cardValue = {"Ace", "King", "Queen", "Jack", "10",
                                   "9", "8", "7", "6", "5", "4", "3", "2"};

    public Card(int types, int values)
    {
        type = types; 
        value = values;
    }

    public String toString()
    {
        String finalCard = cardValue[value] + " of " + cardType[type];

        return finalCard;
    }

}

import java.util.Random;
import java.util.ArrayList;

public class Deck 
{
    private ArrayList<Card> cards;

    public Deck()
    {
        cards = new ArrayList<Card>();

        for(int a =0; a<=3; a++)
        {
            for(int b =0; b<=12;b++)
            {
                cards.add(new Card(a,b));
            }
        }  
    }

    public Card drawRandomCard()
    {
        Random generator = new Random();
        int index = generator.nextInt(cards.size());
        return cards.remove(index);
    }

    public String toString()
    {
        String result = "Cards remaining in deck: " + cards;

        return result;

    }    
}


import java.util.Scanner;

public class CardProgram 
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        Card C;
        Deck deck = new Deck();

        System.out.println("Enter number of cards to be dealt: ");
        int numberCards = scan.nextInt();

        System.out.println("Cards drawn: ");
        for(int i=0; i<numberCards; i++)
        {
            C = deck.drawRandomCard();
            System.out.println(C.toString());
        }

        //C = deck.toString();
        //System.out.println(cards.toString());
       // System.out.println(C.toString());


    }

}
user3071909
  • 15
  • 3
  • 3
  • 9

2 Answers2

2

I think this should do your work

System.out.println(deck.toString());
//System.out.println(deck); // Note : this will also work

Though I think creating a new method remainingCard and returning the cards ArrayList makes sense rather than Overriding toString with remaining cards.

Anirban Nag 'tintinmj'
  • 5,572
  • 6
  • 39
  • 59
0
  for(int i=0; i<deck.cards.size(); i++)
    {
        System.out.println(deck.cards.get(i).toString());
    }

Try this for loop right after you have a for loop printing out the cards you have drawn

You have a deck in the main method...a deck has cards...you draw some cards out of your deck....then you say deck.cards which gives you the decks array list of cards...then you say .get(i) to get each card in that deck...and finally .toString() to print out the content of the card

Note: you would have to make the private array list of cards in your deck class public or add a public getter method in the deck class to get the arraylist of cards...

Josh Engelsma
  • 2,636
  • 14
  • 17