Enumeration of colors with a static method that produces a random color:
public enum CatColor { BLACK, WHITE, GREY, RED, BLUE; private static Random random = new Random(); private static int colorsCount = values().length; public static CatColor getRandomColor() { return values()[random.nextInt(colorsCount)]; } }
Cat class:
public class Cat { private String name; private CatColor color; public Cat(String name, CatColor color) { this.name = name; this.color = color; } public String getName() { return name; } public CatColor getColor() { return color; } @Override public String toString() { return "Cat " + name + " has color " + color; } }
main method:
public static void main(String[] args) { List<Cat> cats = new ArrayList<>(); //создаём котов for (int i = 0; i < 20; i++) { Cat cat = new Cat("cat"+(i+1), CatColor.getRandomColor()); cats.add(cat); } //печатаем котов cats.forEach(System.out::println); }
Conclusion:
Cat cat1 has color BLUE Cat cat2 has color WHITE Cat cat3 has color BLACK Cat cat4 has color BLACK Cat cat5 has color RED Cat cat6 has color BLUE Cat cat7 has color WHITE Cat cat8 has color RED Cat cat9 has color WHITE Cat cat10 has color BLUE Cat cat11 has color RED Cat cat12 has color WHITE Cat cat13 has color GREY Cat cat14 has color WHITE Cat cat15 has color BLACK Cat cat16 has color GREY Cat cat17 has color GREY Cat cat18 has color BLACK Cat cat19 has color BLUE Cat cat20 has color BLACK