How can I reduce the repetiveness in my code?

90 views Asked by At

Look at the following program:

public class HouseOfCards 
{
    public static void main(String[] args)
    {
        for (int cards = 1; cards <= 4; cards++)
        {
           if (cards == 1) 
           {
               System.out.println("Ace of Clubs");
                for (int singles = 2; singles <= 9; singles++)
                { 
                   System.out.println(singles + " of Clubs");
                }//end of for loop()
               System.out.println("Jack of Clubs");
               System.out.println("Queen of Clubs");
               System.out.println("King of Clubs");
               System.out.println("Ace of Clubs");
          }//end of if() 
                            ......
             //More else if() blocks for each suit
                            ......
        }//end of for loop()
     }//end of method main() 
   }//end of class HouseOfCards

In the above code, I want to print the first set of cards, that being clubs, then do the same for the rest of the suits in a "new deck order" format.

Clubs --> Spades --> Hearts --> Diamonds

I see that the first if() block, that being, (cards == 1), is a little repetitive. I don't want to do 4 if blocks to do the whole deck.

My questions to you are as follows, 1. how would I go about reducing the code in that way? 2. Is it possible? Or 3. is it just best to do 4 sets of if() blocks for each suit?

Thanks in advance for the help!

8

There are 8 answers

1
maskacovnik On BEST ANSWER

make array of it:
String[] arr = new String[]{"clubs","spades","hearts","diamonds"}
then use loop:
for(int i=0;i<arr.length;i++){} example:

public class HouseOfCards 
{
    private static final String[] arr = new String[]{"Clubs","Spades","Hearts","Diamonds"};
    public static void main(String[] args)
    {
            for(int i=0;i<arr.length;i++)
            {
            System.out.println("Ace of "+arr[i]);
                for (int singles = 2; singles <= 9; singles++)
                { 
                   System.out.println(singles + " of "+arr[i]);
                }//end of for loop()
               System.out.println("Jack of "+arr[i]);
               System.out.println("Queen of "+arr[i]);
               System.out.println("King of "+arr[i]);
               System.out.println("Ace of "+arr[i]);
           }//end of if()
     }//end of method main() 
   }//end of class HouseOfCards
0
BobTheBuilder On

Create a method printSuit(String suitName) and use it inside each if statement.

You can also create Enum of suits and iterate over its values.

1
Jazzwave06 On
private String[] cards = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
private String[] colors = {"Clubs", "Spades", "Hearts", "Diamonds"};

Then iterate through both array using for loops.

for (int iColor = 0; iColor < colors.length; iColor++) {
    for (int iCard = 0; iCard < cards.length; iCard++) {
        System.out.printf("%s of %s%n", cards[iCard], colors[iColor];
    }
}
0
syntagma On

If the only problem you have is printing the suit, then I would either create an array:

String[] suits = {"Clubs", "Spades", "Hearts", "Diamonds"};

or an ArrayList<String>():

ArrayList<String> suits = new ArrayList<String>();
suits.add("Clubs");
suits.add("Spades");
suits.add("Hearts");
suits.add("Diamonds");

You can simply iterate it like that:

for(String suit : suits)
0
Michael Laffargue On

For algorithm understanding purpose :

If you have a For with X values and inside a if for each values, than just remove the for and the if.

for (int cards = 1; cards <= 4; cards++){
       if (cards == 1) System.out.println("A");
       if (cards == 2) System.out.println("B");
       if (cards == 3) System.out.println("C");
       if (cards == 4) System.out.println("D");
}

is exactly the same as :

System.out.println("A");
System.out.println("B");
System.out.println("C");
System.out.println("D");
0
afzalex On

To use enum would be best in your case with extra function.
Here is my aproach to make your code simple.

public static void main(String...args){
    for(Card card : Card.values()){
        showCards(card);
    }
}
static void showCards(Card card){
    for(CardVal cv : CardVal.values()){
        System.out.println(cv + " of "+card);
    }
}
static enum Card {
    Club, 
    Spades,
    Hearts,
    Diamond
}
static enum CardVal {
    Ace,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
    Ten,
    Jack,
    Queen,
    King
}
0
DMunz On

A more object-oriented solution would be to create a Card class, and promote suit and value to enumerated types as such:

public class Card {

    public enum Suit {
        HEARTS,
        CLUBS,
        SPADES,
        DIAMONDS;
    }

    public enum FaceValue {
    ACE,
    KING,
    QUEEN,
    JACK,
    TEN,
    NINE,
    EIGHT,
    SEVEN,
    SIX,
    FIVE,
    FOUR,
    THREE,
    TWO,
    ONE
    }

    private Suit suit;
    private FaceValue value;

    public Card(Suit suit, FaceValue value) {
        this.suit = suit;
        this.value = value;
    }

    @Override
    public String toString() {
        return value.toString() + " of " + suit.toString();
    }

}

Then you can reduce your printing code to two nested loops:

public static void main(String[] args) {

    for(Suit s : Suit.values()) {
        for (FaceValue v : FaceValue.values()) {
            System.out.println(new Card(s,v));
        }
    }
}
0
user902383 On

As java is object oriented language, try to think objects

first create Enums with colours and ranks;

    enum Colour{
        Clubs,Diamonds,Hearts,Spades;
    }

    enum Rank{
         Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King,Ace
    }

Define your card

class Card{
    @Override
    public String toString() {
        return rank + " of "+colour;
    }
    public Card(Colour colour, Rank rank) {
        super();
        this.colour = colour;
        this.rank = rank;
    }
    private final Colour colour;
    private final Rank rank;

}

it will be good if your card will implements comparable interface, then if you try to create any card game it might be useful

and last what you need is deck

class Deck{
    List<Card> cards = new ArrayList<Card>();       
    public Deck(){
        for (Colour colour : Colour.values()){
            for (Rank rank : Rank.values()){
                cards.add(new Card(colour, rank));
            }   
        }
    }
}

deck when instantiated creates all cards, it can be usefull to manipulate cards