Why I have these error in this factory class that use random values of an enum to build my instance?

104 Views Asked by At

I am finding some difficulties trying to implement a factory class in Java that build a RoomTipology object selecting a random value from an enum. So I have the following situation:

public class RoomTipologyMockFactory {

    private RoomTipology roomTipology;

    private static enum RoomTipologyEnum {
        MATRIMONIALE,
        MATRIMONILAE_SUPERIOR,
        QUADRUPLA,
        TRIPLA,
        SINGOLA;

        private static final List<RoomTipologyEnum> VALUES = Collections.unmodifiableList(Arrays.asList(values()));
        private static final int SIZE = VALUES.size();
        private static final Random RANDOM = new Random();

        public static RoomTipologyEnum getRandomRoomTipologyValue()  {
            return VALUES.get(RANDOM.nextInt(SIZE));
        }
    }

    public static RoomTipology getRandomRoomTipology() {

        List<Object> value = RoomTipologyEnum.getRandomRoomTipology();

        RoomTipology result = new RoomTipology();

        switch (roomTipology) {

            case MATRIMONIALE:
                result.setName("Matrimoniale");
                result.setDescription("Camera matrimoniale con bagno interno, Lorem ipsum dolor sit amet, consectetuer " +
                        "adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus " +
                        "et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, " +
                        "pellentesque eu, pretium quis, sem.");
                result.setMaxPeople(2);
                result.setTimeStamp(new Date());

                break;

            case MATRIMONILAE_SUPERIOR:
                result.setName("Matrimoniale Superior");
                result.setDescription("Camera con bagno dotata di piccolo angolo soggiorno completo di divano e " +
                        "scrivania, molto luminosa, pavimenti in parquet, riscaldamento e aria condizionata, " +
                        "armadio/guardaroba, TV a schermo piatto dotata di canali satellitari, connessione Wi-Fi " +
                        "gratuita, bollitore e selezione di tè e tisane.");
                result.setMaxPeople(2);
                result.setTimeStamp(new Date());

                break;

            case QUADRUPLA:
                result.setName("Camera Quadruola");
                result.setDescription("Camera per quattro persone con bagno interno, Lorem ipsum dolor sit amet, consectetuer " +
                        "adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus " +
                        "et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, " +
                        "pellentesque eu, pretium quis, sem.");
                result.setMaxPeople(4);
                result.setTimeStamp(new Date());

                break;

            case TRIPLA:
                result.setName("Camera Tripla");
                result.setDescription("Camera per tre persone con bagno interno, Lorem ipsum dolor sit amet, consectetuer " +
                        "adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus " +
                        "et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, " +
                        "pellentesque eu, pretium quis, sem.");
                result.setMaxPeople(3);
                result.setTimeStamp(new Date());

                break;

            case SINGOLA:
                result.setName("Camera Singola");
                result.setDescription("Camera singola persona con bagno interno, Lorem ipsum dolor sit amet, consectetuer " +
                        "adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus " +
                        "et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, " +
                        "pellentesque eu, pretium quis, sem.");
                result.setMaxPeople(1);
                result.setTimeStamp(new Date());

                break;
        }

    return result;

    }

}

As you can see in the previous code I have an external RoomTipologyMockFactory that is used to create mocks instances of the RoomTipology class.

Every time tht the getRandomRoomTipology() method is called have to be selected a random possible value of the RoomTipologyEnum enum and so it use it to build a specific mock of the RoomTipology class.

So, inside the enum definition, I have created the getRandomRoomTipologyValue() method that returns a random value form the enum

public static RoomTipologyEnum getRandomRoomTipologyValue() { return VALUES.get(RANDOM.nextInt(SIZE)); }

I have to use this value into the getRandomRoomTipology to return a specific RoomTipology mock instance.

I have 2 problems:

1) I have declared this enum as private static (so it is a class level and, in theory I have not to build it with new() because I take it from the current RoomTipologyMockFactory instance. But I can't do:

List<Object> value = RoomTipologyEnum.getRandomRoomTipology();

it seems that can't resolve to the getRandomRoomTipology() method.

2) I have no more to switch on the enum but I think on the returned value.

What is wrong? What am I missing? How can I fix this issue?

2

There are 2 best solutions below

1
On BEST ANSWER
public class RoomTipologyMockFactory {

    private enum RoomTipologyEnum {
        MATRIMONIALE,
        MATRIMONILAE_SUPERIOR,
        QUADRUPLA,
        TRIPLA,
        SINGOLA;

        private static final List<RoomTipologyEnum> VALUES = Collections.unmodifiableList(Arrays.asList(values()));
        private static final int SIZE = VALUES.size();
        private static final Random RANDOM = new Random();
        private static List<Object> randomRoomTipology;

        public static RoomTipologyEnum getRandomRoomTipologyValue()  {
            return VALUES.get(RANDOM.nextInt(SIZE));
        }

    }

    public static RoomTipology getRandomRoomTipology() {
        RoomTipologyEnum roomTipologyEnum = RoomTipologyEnum.getRandomRoomTipologyValue();
        RoomTipology result = new RoomTipology();
        switch (roomTipologyEnum) {
            case MATRIMONIALE:
                break;
            case MATRIMONILAE_SUPERIOR:
                break;
            case QUADRUPLA:
                break;
            case TRIPLA:
                break;
            case SINGOLA:
                break;
        }
        return result;
    }

}
0
On

Your funtion in the enum is, as you wrote in the question getRandomRoomTipologyValue(), what Olayinka stated first.

I've addressed also the switch Problem by giving the Enum some properties. I named the first property label to avoid confusion with the name-Method of Enum

public class RoomTipologyMockFactory {

    private RoomTipology roomTipology;

    private static enum RoomTipologyEnum {
        MATRIMONIALE("Matrimoniale","Camera matrimoniale con bagno interno, Lorem ipsum dolor sit amet, consectetuer " +
                "adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus " +
                "et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, " +
                "pellentesque eu, pretium quis, sem.",2),

        MATRIMONILAE_SUPERIOR(...),
        QUADRUPLA(...),
        TRIPLA(...),
        SINGOLA(...);

        private String label;
        private String description;
        private int maxPeople;

        private static final List<RoomTipologyEnum> VALUES = Collections.unmodifiableList(Arrays.asList(values()));
        private static final int SIZE = VALUES.size();
        private static final Random RANDOM = new Random();

        public static RoomTipologyEnum getRandomRoomTipologyValue()  {
            return VALUES.get(RANDOM.nextInt(SIZE));
        }

        private RoomTipologyEnum(String label, String description, int maxPeople) {
            this.label = label;
            this.description = description;
            this.maxPeople = maxPeople;
        }

        public String getLabel() {
            return label;
        }

        public String getDescription() {
            return description;
        }

        public int getMaxPeople() {
            return maxPeople;
        }

    }

    public static RoomTipology getRandomRoomTipology() {

        RoomTipologyEnum value = RoomTipologyEnum.getRandomRoomTipologyValue();

        RoomTipology result = new RoomTipology();

        result.setName(value.getLabel());
        result.setDescription(value.getDescription());
        result.setMaxPeople(value.getMaxPeople());
        result.setTimeStamp(new Date());

    return result;

    }

}