0

Item

public enum Item
{
    SANDWICH("sandwich"), CRISPS("crisps"), DRINK("drink");

private String description;

Item(String description)
{
this.description = description;

}

public String toString()
{
    return description;
}

}

Character

  public enum Character
{
    LAURA("Laura",Item.SANDWICH),SALLY("Sally", Item.CRISPS),ANDY("Andy", Item.DRINK),ALEX("Alex", null);


private String charDescription;
private Item item;

private Character(String Chardescription, Item item) {
    this.charDescription = charDescription;
    this.item = item;
}


public String toString()
{
    return charDescription;
}

public boolean take(Item item)
{

}

I wrote these two enumeration classes, where Item contains items with their descriptions and Character has four different Character objects: LAURA, SALLY, ANDY and ALEX, each parameterized with a description and an item. The item may be null.

I need to write a method take(Item item), which takes the indicated item from the character if the character has that item, and returns true if it is successful in taking the item from the character, and false otherwise.

I don't know how to implement this by modifying the parameters of the enum class Character. Any help will be appreciated.

3 Answers 3

2

Enumeration values are designed as constant values.
Look at the Oracle examples : Day, Planet.

It is not explicitly said that the enum value states have to be immutable but I find quite counter intuitive to define constant things defined at compile time that have a mutable state.
Note also that the very most of time, enum values defined in the JDK (or third party libraries) are immutable.

Mutability seems really more natural and expected for classes.

So I advise to not change their state after their instantiation.
If you need to change their values, using a class makes probably more sense.

Note that defining reading methods in your enum class such as :

public boolean hasItem(Item item){
   return item == this.item;
}

is valid without any hesitation.

Sign up to request clarification or add additional context in comments.

13 Comments

There is nothing in the JLS (or any other literature I'm aware of) that suggests enums "are designed as constant values" (I assume you mean immutable when you say "constant").
@Bohemian "An enum type is a special data type that enables for a variable to be a set of predefined constants." (docs.oracle.com/javase/tutorial/java/javaOO/enum.html )
the references (to the enum instances) are constant, but the instances need not be immutable
@Bohemian A constant which the parts are mutable is not a constant any longer.
Every enum reference is constant and the number of references are predefined by the enum definition. Having mutable fields doesn't affect the usability of any enum
|
2

Contrary to what you might feel, enum instances are not immutable. Just modify the item field of the enum:

public boolean take(Item item) {
    if (item == this.item) {
        this.item = null;
        return true;
    }
    return false;
}

Comments

0

I think you want to create Characters as a Class, instead of an Enum.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.