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;
}
}