-1

I want to initialize a deck of individual cards in Java. This is my approach so far:

public ArrayList<Card> initDeck() {
    ArrayList<Card> cardDeck = new ArrayList<Card>(24);

    cardDeck.add(new Card("Emperor Augustus", 20.1, 40, 4.1, 300000, POWER_AND_INFLUENCE.VERY_HIGH));
    cardDeck.add(new Card("Jeff Bezos", 96, 22, 59.7, 268000, POWER_AND_INFLUENCE.HIGH));
    cardDeck.add(new Card("Bill Gates", 83.7, 41, 73, 112388, POWER_AND_INFLUENCE.MEDIUM));

    return cardDeck;
}

This is a lot of repetition IMO. Also I want to separate the data from function.

Is it possible to export the elements I want to add into cardDeck, i.e. new Card(...) into a separate file?

In JavaScript you would make it for example like this:

JSON-File:

{
    data: [{
        name: "asdf",
        economy: 123,
        yearsInPower: 4
    }, {
        name: "bsdf",
        economy: 3,
        yearsInPower: 10
    }, {
        name: "csdf",
        economy: 43,
        yearsInPower: 5
    }]
}

JS-File:

const cards = require("./cards.json").data;

function initDeck(cards) {
    const cardDeck = [];
    for (let i = 0; i < cards.length; i++) {
        cardDeck.push(cards[i]);
    }
    return cardDeck;
}

let cardDeck = initDeck(cards);

I've looked into other implementation of the initialization of cards in Java. But all the examples assumes a logical order of the cards, e.g. 2,3,4...Jack, Queen, King, Ace. But in my example the cards don't follow any logical order.

Cœur
  • 37,241
  • 25
  • 195
  • 267
thadeuszlay
  • 2,787
  • 7
  • 32
  • 67
  • 2
    There are packages available for Java to read name/value data in various formats: JSON, YAML, etc. – Pointy Mar 26 '17 at 14:21

1 Answers1

1

There are several ways that you can separate data from code (like loading from files, database, etc..).

If you wanted to load data from JSON file, then you can use Jackson library to read the data from JSON and convert to Java objects (deserialization) as shown below:

public List<Card> initDeck() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    Card[] staff = mapper.readValue(new File("C:\\cards.json"), Card[].class);
    List<Card> cardDeck = Arrays.asList(staff);
    return cardDeck;
}

Also, as a side note, remember that it is always a best practice to code for interfaces List<Card> as return types (shown above) rather than concrete classes like `ArrayList, you can look here on the same subject.

Community
  • 1
  • 1
Vasu
  • 21,832
  • 11
  • 51
  • 67
  • Is `throws Exception` the same as a `try-catch`-block in the function body? – thadeuszlay Mar 26 '17 at 16:00
  • Either you need to `catch` the Exception inside the function or `throw` from the function so that the caller needs to handle it – Vasu Mar 26 '17 at 16:18
  • Does it make sense to add a `throw` to every method you write? – thadeuszlay Mar 26 '17 at 16:47
  • You don't write throw for each method, only when there are checked exceptions, you need to specify in your method signature, look here: http://stackoverflow.com/questions/27578/when-to-choose-checked-and-unchecked-exceptions?rq=1 – Vasu Mar 26 '17 at 17:58