0

I have to prepare some work until tomorrow, but I'm stuck. (Also all the names in the code are German, don't be confused :) )

This is my constructor class for an object called "Karte" (card)

private Farbe f;
private Wert w;

public enum Farbe {
    PIK, KREUZ, KARO, HERZ
}

public enum Wert {
    SIEBEN, ACHT, NEUN, ZEHN, BUBE, DAME, KOENIG, ASS
}

Karte(Farbe f, Wert w) {
    this.f = f;
    this.w = w;
}

public Farbe getFarbe() {
    return f;
}

public Wert getWert() {
    return w;
}

public String toString() {
    return f + " " + w;
}

And now there's another class that should generate an object, made out of multiple cards. The normal syntax would be

Karte name = new Karte(); // (random stuff here);

But how can I get the class to generate multiple objects with random names? If I tried to just use this kind of code in a for each loop it would just overwrite the previous one.

How do I give them random names or even chosen from a String[]?

Zini
  • 909
  • 7
  • 15
Wischm0pp
  • 66
  • 1
  • 5
  • possible duplicate of http://stackoverflow.com/questions/1972392/java-pick-a-random-value-from-an-enum – tom Apr 02 '14 at 11:39
  • *... to generate multiple objects with random names* What do you mean random names? Do you mean names of variables? Some example of expected result would be helpful. You can also describe why you need it because it looks like [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Pshemo Apr 02 '14 at 11:40

4 Answers4

2

You can convert an enum to an array containing all the values. Using that you could create something like follows with the random class:

public static void main(String[] args)
{
    Random r = new Random();
    for (int i = 0; i < 10; i++)
    {
        Karte k = new Karte(Farbe.values()[r.nextInt(Farbe.values().length)], Wert.values()[r.nextInt(Wert.values().length)]);
        System.out.println(k);
    }

}
tom
  • 2,735
  • 21
  • 35
2

You can try to create a simple ordered ArrayList<Karts> and after just shuffle its order getting a random list of not duplicated Karts.

public ArrayList<Karte> getMultipleInstances() {
  ArrayList<Karte> instances = new ArrayList<Karte>();
  for(Farbe f : Farbe.values()) {
    for(Wert w : Wert.values()) {
      instances.add(new Karte(f,w));
    }
  }
  Collections.shuffle(instances);
  return instances();
}
Zini
  • 909
  • 7
  • 15
1

You could create a factory method that would use java.util.Random in order to choose properties.

or you could create empty constructor within Karte that would randomly assign values:

Karte() {
    Random rd = new Random();
    Farbe[] farbes = Farbe.values();
    f = farbes[rd.nextInt(farbes.length)];
    Wert[] werts = Wert.values();
    w = werts[rd.nextInt(werts.length)];
}

If you are looking how to store the cards depending on names you could map them to a String like

//Create collection for storing karts
Map<String, Karte> karts = new HashMap<String, Karte>();
for(int i=0; i<10; i++) {
    Karte k = new Karte()
    karts.put((k.getWert().name()+" "+k.getFarbe.name()), k);
}
Taks
  • 2,033
  • 4
  • 18
  • 23
  • 1
    This is what I thought, but I don't think that this is what he's asking. I think he's talking about random variable names. – Tim Kuipers Apr 02 '14 at 11:35
  • I added simple generation/storing mechanism.. Its true that the question is a bit ambiguous. – Taks Apr 02 '14 at 11:41
  • 1
    Your answer now seems quite complete. Also the answer from @developer3466402 seems good, in case it is necessary to have no duplicate cards. – Tim Kuipers Apr 02 '14 at 12:43
1

If you want multiple cards, you should put them in a set or on a stack or something of the like.

Just use a LinkedList<Karte> and add each new Karte on it.

You shouldn't think you can give each card a separate variable name when you don't know the number of cards beforehand. If you do know the number of cards and the names they should have, just do

Karte eins = new Karte(...)
Karte zwei = new Karte(...)
Karte drei = new Karte(...)
...
Karte homo = new Karte(KREUZ, ASS)
Karte schauen = new Karte(PIK, ZEHN)
Tim Kuipers
  • 1,705
  • 2
  • 16
  • 26