I am working on homework for school. I am making a simplified deck of cards, you can choose how many suits and how many cards per suit. (suit and rank). I have a Card class that creates a single card and a DeckOfCards class that creates a deck of Cards (a deck of Card Objects). I am attempting to put the cards into an ArrayList (inside the DeckOfCards constructor), but every time it just creates a reference to the most recent card created. I have spent a couple hours trying to figure it out and I cant find an answer in any search.
public class DeckOfCards
{
private int counter = 0;
private ArrayList<Card> cardList = new ArrayList<>();
public DeckOfCards(int rank, int suit)
{
for (int x = 0; x < suit; x++) // x is suit
{
for (int y = 0; y < rank; y++) // y is rank
{
cardList.add(counter, new Card(x, y));
counter++; // counter is position in ArrayList / deck
}
}
}
public String dealCard(int numOfCards)
{
// returns the card (numOfCards)
return cardList.get(numOfCards).toString();
}
}
/* Card Class and Constructor
public class Card
{
private static int SUIT;
private static int RANK;
public Card(int suit, int rank)
{
this.SUIT = suit;
this.RANK = rank;
}
public String toString()
{
return ("S"+ SUIT + "R" + RANK);
}
}
Depending on the rank and suit the output should be
S1R1
S1R2
S1R3
.
.
.
S4R1
S4R2
S4R3
But the out put is always the last card created
S4R3