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.