tryingtoflow
tryingtoflow

Reputation: 61

How to change from Constructors to Enum in java?

I have these integers and this method which uses constructors but I am supposed to change this into an Enum since thise code is outdated. I do understand how Enums work in theory but no matter what I try to change I do it wrong.

public static final int CHILDRENS = 2;
public static final int REGULAR = 0;
public static final int NEW_RELEASE = 1;

private Price price;

public void setPriceCode(int priceCode) {
    
    switch (priceCode) {
    case REGULAR:
        price = new RegularPrice();
        break;
    case CHILDRENS:
        price = new ChildrensPrice();
        break;
    case NEW_RELEASE:
        price = new NewReleasePrice();
        break;
    default:
        throw new IllegalArgumentException("Incorrect Price Code");
    }
}

I thaught that to change to enums I should first delete the given integers at the top and then add this before the method:

enum cases {
    REGULAR, CHILDRENS, NEW_RELEASE;
}

And now I do not know how to change the method's code accordingly. How do I give the cases the integers, should I not delete these: ?

public static final int CHILDRENS = 2;

How should I change my switch case accordingly? Would really appreciate some help, I watched a bunch of videos on enums but the cases were very different.

Upvotes: 1

Views: 79

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79085

Do it like this:

enum PriceCode {
    REGULAR(0), // calls constructor with value 0
    NEW_RELEASE(1), // calls constructor with value 1
    CHILDREN(2) // calls constructor with value 2
    ;

    private final int priceCode;

    private PriceCode(int priceCode) {
        this.priceCode = priceCode;
    }

    int value() {
        return priceCode;
    }
}

public class Main {
    // Usage
    public static void setPriceCode(PriceCode priceCode) {
        System.out.println("The value of " + priceCode + " is " + priceCode.value());
        switch (priceCode) {
        case REGULAR:
            // Do something
            break;
        case CHILDREN:
            // Do something
            break;
        case NEW_RELEASE:
            // Do something
            break;
        default:
            throw new IllegalArgumentException("Incorrect Price Code");
        }
    }

    public static void main(String[] args) {
        // Set price code as CHILDREN
        setPriceCode(PriceCode.CHILDREN);

        // Another sample usage
        System.out.println("Price code of NEW_RELEASE is " + PriceCode.NEW_RELEASE.value());
    }
}

Output:

The value of CHILDREN is 2
Price code of NEW_RELEASE is 1

Learn more about enum and enum constructor at Java tutorial by Oracle.

Upvotes: 2

Abra
Abra

Reputation: 20914

Think of an enum as a class or interface, in other words it is a type. I recommend the following tutorial: Enum Types And according to java coding conventions, names of types should begin with a capital letter. Therefore, in the below code, I changed the name of the enum to Cases.

Hence the parameter to method setPriceCode should be Cases. The rest of the code remains the same.

enum Cases {
    REGULAR, CHILDRENS, NEW_RELEASE;
}

private Price price;

public void setPriceCode(Cases priceCode) {
    
    switch (priceCode) {
    case REGULAR:
        price = new RegularPrice();
        break;
    case CHILDRENS:
        price = new ChildrensPrice();
        break;
    case NEW_RELEASE:
        price = new NewReleasePrice();
        break;
    default:
        throw new IllegalArgumentException("Incorrect Price Code");
    }
}

Here is an example call to method setPriceCode()

setPriceCode(Cases.CHILDRENS);

Upvotes: 2

Related Questions