0

I would appreciate any help here.

executing the following code, Buyer2 ID always override buyers1 ID. Means I always get ID=2. I am not sure what is wrong with the following code. I think it is my enum and the get method which always retains the last value.

Buyer bOne = new Buyer("buyer1", 1); 
Buyer bTwo = new Buyer("buyer2", 2);

rest of the code:

public enum Fruits {
    Banana("banana", "B"),
    Apple("apple","A"),
    Orange("orange","O");

    private String type, ID;

    private Fruits(String type, String ID){
        this.type = type;
        this.ID = ID;
    }

    public String getType() {
        return type;
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID){
        this.ID = ID;
    }
}

public class Player {
    private String name;
    private Fruits banana, apple, orange;

    private int ID;

    public buyer(String name, int ID) {
        this.name = name;
        this.ID = ID;
        banana = Fruits.Orange;
        apple = Fruits.Apple;
        orange = Fruits.Orange;

        banana.setID("B"+ID);
        apple.setID("A"+ID);
        Orange.setID="O"+ID;
    }
}

thanks

2
  • 1
    You have a Player class. Did you mean Buyer? Commented Apr 26, 2015 at 10:46
  • 1
    enum doesn't work like that. With enum, there is only one Apple, only one Banana and only one Orange. Can you describe what you want your code to do? Commented Apr 26, 2015 at 10:48

4 Answers 4

1

Your Fruits enum is mutable, which is probably not what you want. The setID() method on Fruits is probably a design error. As the different fruits (Banana, Apple, ...) are a single instance, shared through your all program, they need to reflect some globabl state, not some state specific to the buyer.

Try to remove this setter and see if you can refactor your code ...

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

2 Comments

I guess I need to Look into enums and study them carefully. It sounds like the enums are mostly suitable for constants
There is no problem having mutable state in your enum, but there is only one instance of an enum, so its state is global.
0

Fruits.java

package test;

public enum Fruits {

    Banana("banana", "B"), Apple("apple", "A"), Orange("orange", "O");

    private String type, ID;

    private Fruits(String type, String ID) {
        this.type = type;
        this.ID = ID;

    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getID() {
        return ID;
    }

    public void setID(String iD) {
        ID = iD;
    }
}

Buyer.java

   package test;
    public class Buyer {
    private String name;
    private Fruits banana, apple, orange;

    private int ID;


    public Buyer(String name, int ID) {
        this.name = name;
        this.ID = ID;
        banana = Fruits.Orange;
        apple = Fruits.Apple;
        orange = Fruits.Orange;

        banana.setID("B"+ID);
        apple.setID("A"+ID);
        orange.setID("O"+ID);

    }
    public static void main (String  []args){

            Buyer bOne = new Buyer("buyer1", 1); 
    System.out.println("id = " + bOne.ID + " name =" + bOne.name);
    Buyer bTwo = new Buyer("buyer2", 2);
    System.out.println("id = " + bTwo.ID  + " name =" + bTwo.name);
    }
    }

Result on consol :

id = 1 name =buyer1

id = 2 name =buyer2

1 Comment

Orange.setID="O"+ID; to orange.setID("O"+ID), générate setter & getter and add a main class to test
0

Maybe check your last line:)

Orange.setID="O"+ID; -> orange.setID="O"+ID;

1 Comment

This is irrelevant; Orange and orange both point to the same (static final) enum constant.
0

each instance of the enumeration is guaranteed to be constructed exactly once and because of this the last value retains.

1 Comment

Thanks, I guess I need to Look into enums and study them carefully. It sounds like the enums are mostly suitable for constants

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.