I'm in CS2 for my college program and I've been tasked with creating code for a PokerCard
class which extends off of a Card
class that carries a bunch of methods which it overrides. In this particular instance, I am overriding the toString
method for testing and am in the middle of writing a bunch of if
statements. Is it possible to create a for
loop that can loop the creation of my code? I tried inserting i
's in place of ordinals for enums but am I stuck with this ugly code here? Thanks in advance!
/**
* An child of the Card class intended to Override the toString of the card
* class. The toString will output integer form followed by a suit symbol.
*
* @author Steven Lee
*/
// Implement PokerCard as an interface with Comparable
public class PokerCard extends Card {
/**
* Child constructor that copies the constructor of the parent Card. Create
* constructor.
*
* @param suit
* @param rank
*/
public PokerCard(Suit suit, Rank rank) {
super(suit, rank);
}
/**
* TODO Create compareTo Override here. comapareTo should be checking for card
* rank differences and sorting them from lowest rank to highest (2 to Ace).
*/
/**
* TODO Override toString from parent Card. This should print in the following
* format: "[Initial][Suit]. Initial is either an int or capitalized first
* letter of the rank. Suit will be output with unicode.
*/
@Override
public String toString() {
// Check for rank value. If J-A.
String letter = null;
String symbol = null;
if (PokerCard.this.getRank().equals(Rank.ACE)) {
letter = "A";
}
if (PokerCard.this.getRank().equals(Rank.KING)) {
letter = "K";
}
if (PokerCard.this.getRank().equals(Rank.QUEEN)) {
letter = "Q";
}
if (PokerCard.this.getRank().equals(Rank.JACK)) {
letter = "J";
}
if (PokerCard.this.getRank().equals(Rank.JACK)) {
letter = "J";
}
if (PokerCard.this.getRank().equals(Rank.TEN)) {
letter = "10";
}
if (PokerCard.this.getRank().equals(Rank.NINE)) {
letter = "9";
}
if (PokerCard.this.getRank().equals(Rank.EIGHT)) {
letter = "8";
}
if (PokerCard.this.getRank().equals(Rank.SEVEN)) {
letter = "7";
}
if (PokerCard.this.getRank().equals(Rank.SIX)) {
letter = "6";
}
if (PokerCard.this.getRank().equals(Rank.FIVE)) {
letter = "5";
}
if (PokerCard.this.getRank().equals(Rank.FOUR)) {
letter = "4";
}
if (PokerCard.this.getRank().equals(Rank.THREE)) {
letter = "3";
}
if (PokerCard.this.getRank().equals(Rank.TWO)) {
letter = "2";
}
if (PokerCard.this.getSuit().equals(Suit.SPADES)) {
symbol = "\u2660";
}
if (PokerCard.this.getSuit().equals(Suit.HEARTS)) {
symbol = "\u2665";
}
if (PokerCard.this.getSuit().equals(Suit.DIAMONDS)) {
symbol = "\u2666";
}
if (PokerCard.this.getSuit().equals(Suit.CLUBS)) {
symbol = "\u2663";
}
return letter + symbol;
}
}
I'm hoping to streamline the if
statement creation process. Or if there is a better way to arrange this toString
, I'd greatly appreciate that as well. thank you in advance. (Note that as far as the exercise goes, there is no need to add the red colored suit variants)