Reputation: 63
I cannot get the properties of a certain card in this code it always prints null
and I don't know why. I'm from Php-HTML world and I'm a newbie on JAVA.
This is the from Opportunity.java
package opportunity;
import java.io.*;
import java.util.Collections;
import java.util.List;
import static java.util.stream.Collectors.toList;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import opportunity.Card.CardType;
public class Opportunity {
/*Player 1 */
public int p1_money = 10000;
public int p1_card_d = 40;
public int p1_card_h = 0;
/*Player 2 */
public int p2_money = 10000;
public int p2_card_d = 40;
public int p2_card_h = 0;
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
System.out.print("***************Opportunity***************\n");
final Card card1 = new Card(Card.CardType.EVENT, "Get Tax Returns").
setProperty("cost", "0.00").
setProperty("Effect", "Effect: Earn money equal to the\n"
+ "maximum income each of your\n"
+ "properties can give you,\n"
+ "depending on their level.");
final Card card2 = new Card(Card.CardType.EVENT, "BIR Hunting Begins").
setProperty("cost", "0.00").
setProperty("Effect", "Effect: An opponent loses\n"
+ "money equal to 50% of the\n"
+ "maximum income each of\n"
+ "their properties can give him or her,\n"
+ "depending on the level of the\n"
+ "property.");
final Card card3 = new Card(Card.CardType.EVENT, "Restore Balance").
setProperty("cost", "10000.00").
setProperty("Effect", "Effect: The total income of all\n"
+ "the players becomes equal to\n"
+ "the income of the player\n"
+ "with the lowest income.");
final List<Card> deck = Stream.of(CardType.values()).
flatMap(type -> IntStream.rangeClosed(1, 4).mapToObj(num -> new Card(type, "CardName" + num))).
collect(toList());
Collections.shuffle(deck);
System.out.println(deck.get(0).getProperty("Effect"));
}
}
And This is the codes from Card.java
package opportunity;
import java.util.*;
public class Card {
public enum CardType {
EVENT,
PROPERTY,
ASSET;
}
private final CardType cardType;
private final String cardName;
private final Map<String, String> properties = new HashMap<>();
Card(final CardType cardType, final String cardName) {
this.cardType = cardType;
this.cardName = cardName;
}
public Card setProperty(final String name, final String value) {
properties.put(name, value);
return this;
}
public String getProperty(final String name) {
return properties.get(name);
}
}
It always returns this:
run:
***************Opportunity***************
null
BUILD SUCCESSFUL (total time: 0 seconds)
even though I put an index on get()
Upvotes: 1
Views: 374
Reputation: 13
The comment of the previous folk is totally right. You are not adding the created cards to the list.
Try replacing your code
final List<Card> deck = Stream.of(CardType.values()).
flatMap(type -> IntStream.rangeClosed(1, 4).mapToObj(num -> new Card(type, "CardName" + num))).
collect(toList());
for standard list creation and adding. For instance (and updated for 4 cards):
List<Card> deck = new ArrayList<Card>();
for (int i = 0; i < 4; i++){
deck.add(card1);
deck.add(card2);
deck.add(card3);
}
If you want to minimise the number of lines of code, there are ways to initialise a new list with content. Try this post Initialization of an ArrayList in one line.
Anyway, always try to code the simplest solution, even more when starting with a new language.
Upvotes: 0
Reputation: 1093
To build on @Luiggi Mendoza's answer, you are not adding your Card objects to the deck
list.
Replace
final List<Card> deck = Stream.of(CardType.values()).
flatMap(type -> IntStream.rangeClosed(1, 4).mapToObj(num -> new Card(type, "CardName" + num))).
collect(toList());
With
final List<Card> deck = new ArrayList<>();
deck.add(card1);
deck.add(card2);
deck.add(card3);
Instead of adding new Cards
to the list, add the objects you have already created - and have properties set.
Edit: to address your comment
final List<Card> deck = new ArrayList<>();
for (int i=0; i<4; i++) {
deck.add(card1);
deck.add(card2);
deck.add(card3);
}
This will add four copies of each of your three cards to the list.
Upvotes: 1
Reputation: 85779
Here:
final List<Card> deck = Stream.of(CardType.values()).
flatMap(type -> IntStream.rangeClosed(1, 4).mapToObj(num -> new Card(type, "CardName" + num))).
collect(toList());
Collections.shuffle(deck);
You fill List<Card> deck
with new Card
s, you don't add any of the other cardX
variables. Thus, when retrieving this "Effect"
property, you get null
because the Card
s in deck
don't have the "Effect"
property set before.
Upvotes: 0