1

If I have two classes (Character and Person) that are located in the same package and one class(Audit) that is located in a different package from Character and Person, how can I randomly list values in the enum in Character and Person? In the Character class,

public abstract class Character{

    private int age;
    private BodyType bodyType;

    public enum BodyType
    {
        AVERAGE,
        ATHELETIC,
        UNSPECIFIED;

    }
    public Character(int age, BodyType bodyType){
        this.age = age;
        this.bodyType = bodyType;
    }

    public int getAge(){
        return this.age;
    }
    public BodyType getBodyType(){
        return this.bodyType;
    }

    public void setAge(int age){
        this.age = age;
    }
    public void setBodyType(BodyType bodyType){
        this.bodyType = bodyType;
    }

}

In the Person class, which extends Character

public class Person extends Character{


    private AgeCategory ageCategory;

    public enum AgeCategory
    {
        CHILD,
        ADULT;
    }
    public Person(int age, BodyType bodyType){

        super(age, bodyType);
    }
    public AgeCategory getAgeCategory()
    {
        if (getAge() >=0 && getAge() <=16){
            return AgeCategory.CHILD;
        }

        if (getAge() >=17){
            return AgeCategory.ADULT;
        }
        return ageCategory;
    }

}

In the Audit class located in different package, I have to return strings. I’ve tried the following code, but this just results in the enumeration in order . What I wanted to do here is that I want to get all enum values listed but in random order.

public class Audit{

    public String toString() 
    {       
        String strings = “random enumeration\n”;

        for (BodyType bodyType : EnumSet.allOf(BodyType.class)) {
            strings += bodyType.name().toLowerCase() + ":";
            strings += "\n";
        }

        for (AgeCategory ageCategory : EnumSet.allOf(AgeCategory.class) ) {
            strings += ageCategory.name().toLowerCase() + ":";
            strings += "\n";
        }
        return strings;
    }
}
jean
  • 35
  • 1
  • 4

2 Answers2

1

That's the behaviour of EnumSet.

The iterator returned by the iterator method traverses the elements in their natural order (the order in which the enum constants are declared).

Refer this

Or

Try

List<BodyType> randomType = Arrays.asList(BodyType.values());
Collections.shuffle(randomType);
for (BodyType type : randomType) {
    System.out.println(type);
}

To achieve that[in your comment - mix of all enum types]:

Your ENUMs should share common type and use shuffle.

  1. Iterate ENUM of BodyType and store in list
  2. Iterate ENUM of AgeType and store in the same List
  3. Shuffle
  4. Print
Gibbs
  • 21,904
  • 13
  • 74
  • 138
0

it seems you are already very close to the desired solution, especially with the help of the above answer.

Below is a complete solution for what you seem to be after. Basically it iterates over both enums, stores the lowercase name of each value in a list, shuffles the list, then builds a string with its contents:

public class Audit {

    public String toString() 
    {   
        List<String> names = new ArrayList<>();
        for (BodyType bodyType : BodyType.values())
        {
            names.add(bodyType.name().toLowerCase());
        }

        for (AgeCategory ageCategory : AgeCategory.values())
        {
            names.add(ageCategory.name().toLowerCase());
        }

        Collections.shuffle(names);

        StringBuilder sb = new StringBuilder("random enumeration\n");
        for (String name : names) {
            sb.append(name).append(":\n");
        }

        return sb.toString();
    }

}

Please note that I've changed the way you generate the string, using StringBuilder is significantly more efficient.

Furthermore I really don't understand why you would need what you are asking for, but I'll assume you have your reasons.

radu
  • 114
  • 2
  • 7