Reading a text file of deck of cards and replacing strings

540 views Asked by At

I am confused on how to begin this i have to find a way to read an input file and replace the string and write it to an output file (OUT). The input file (IN) is a deck of cards

  • IN: 2-H
  • OUT: Two of hearts (value = 2)
  • IN: 1-C
  • OUT: Invalid card rank in 1-C
  • IN: 7*C
  • OUT: Invalid delimiter in 7*C
  • IN: X*Y
  • OUT: Unrecognizable format in X*Y
  • IN: A-C
  • OUT: Ace of clubs (value=1 or value = 11)
  • IN: Q-H
  • OUT: Queen of hearts (value=10
1

There are 1 answers

3
weston On BEST ANSWER

I'd recommend test driving a function to do the mapping. The file reading/writing is trivial to look up.

Test driving leads to this:

Tests:

public class CardsTests {

    @Test
    public void TwoOfHearts() {
        Assert.assertEquals("Two of hearts (value = 2)", Card.fromString("2-H"));
    }

    @Test
    public void ThreeOfHearts() {
        Assert.assertEquals("Three of hearts (value = 3)", Card.fromString("3-H"));
    }

    @Test
    public void ThreeOfSpades() {
        Assert.assertEquals("Three of spades (value = 3)", Card.fromString("3-S"));
    }

}

Class:

public class Card {

    public static String fromString(String string) {
        char value = string.charAt(0);
        String textValue = valueToText(value);
        String suit = getSuit(string.charAt(2));
        return String.format("%s of %s (value = %c)", textValue, suit,
                value);
    }

    private static String getSuit(char value) {
        switch (value) {
        case 'H':
            return "hearts";
        case 'S':
            return "spades";
        default:
            return "";
        }
    }

    private static String valueToText(char value) {
        switch (value) {
        case '2':
            return "Two";
        case '3':
            return "Three";

        default:
            return "";
        }
    }
}

You need to just carry on adding tests to cover all the required functionality.