1

I'm pretty new to Java and following a course for it. So here goes my super stupid question probably. Suppose I have class with an enum like this:

public class Diamant implements Comparable<Diamant> {

    final private String stockNr;
    final private double karaat;
    final private String helderheid;
    final private char kleur;
    final private String snede;

    public Diamant(String stockNr, double karaat, String helderheid, char kleur, String cut) {
        this.stockNr = stockNr;
        this.karaat = karaat;
        this.helderheid = helderheid;
        this.kleur = kleur;
        this.snede = cut;
    }

    public String getStockNummer() {
        return stockNr;
    }

    public double getKaraat() {
        return karaat;
    }

    public String getHelderheid() {
        return helderheid;
    }

    public char getKleur() {
        return kleur;
    }

    public String getSnede() {
        return snede;
    }

    enum Color {
        D(0), E(1), F(2), G(3), H(4), I(5), J(6), K(7), L(8), M(9), N(10), O(11),
        P(12), Q(13), R(14), S(15), T(16), U(17), V(18), W(19), X(20), Y(21), Z(22);

        int quality;

        Color(int quality) {
            this.quality = quality;
        }

        public int getQuality() {
            return quality;
        }
    }

    enum Brightness {
        FL(0), IF(1), VVS1(2), VVS2(3), VS1(4), VS2(5), SI1(6), SI2(7), I1(8), I2(9), I3(10);

        int quality;

        Brightness(int quality) {
            this.quality = quality;
        }

        public int getQuality() {
            return quality;
        }
    }

    @Override
    public String toString() {
        return "StockNr='" + getStockNummer() + "\t" +
                " Karaat=" + getKaraat() + "\t" +
                " Helderheid='" + getHelderheid() + "\t" +
                " Kleur=" + getKleur() + "\t";
    }

    @Override
    public int compareTo(Diamant other) {
        //Sort according to Karaat (Max=5.0)
        if (this.getKaraat() < other.getKaraat()) {
            return 1;
        } else if (this.getKaraat() > other.getKaraat()) {
            return -1;
        } else {
            // Sort according to Kleur first then Helderheid
            Color.D.getQuality();

            (this.getKleur()).getQuality();

            if (this.getKleur() < other.getKleur()) {
                return 1;
            } else if (this.getKleur() > other.getKleur()) {
                return -1;
            } else {
                return 0;
            }
        }
    }

How is it possible to use the method getQuality without explicitly typing the enum value in the CompareTo. In other words, this works fine:

Color.D.getQuality();

However, this doesn't work (although the method getKleur returns D to):

Color.(this.getKleur()).getQuality();

How can I work with the enum method getQuality() without hard typing the letter?

6
  • 5
    this.getKleur().getQuality() (I'm assuming this.getKleur() returns a Color) Commented Apr 28, 2017 at 8:40
  • 1
    you say getKleur returns D, but is it a Color.D or a "D" String ? Commented Apr 28, 2017 at 8:40
  • why not store the color in the diamond instead of a character? Commented Apr 28, 2017 at 9:15
  • Thanks for the quick accept! Commented Apr 28, 2017 at 9:17
  • 1
    I suggest you should be consistent in your language. Your variable names are in Dutch, whereas your enum types are English. You should choose one of either, and use that. My personal preference is English, since it'll make your code more portable. Commented Apr 28, 2017 at 9:45

3 Answers 3

3

This should work fine:

class YourClass { 
  Color getColor() { return Color.D; }

...

YourClass thingy = new YourClass(...
System.out.println(thingy.getColor().getQuality());

Explanation: Color is a valid return type; and a method defined to return Color can return any instance of that Enum. Which can then be used ... well, as you would expect it to use.

Given your comment:

final private char kleur;

is obviously "wrong". The point of an enum is ... to be an enum. The fact that all enum constants are single characters doesn't magically make

char klear = D;

a valid assignment. Thus the obvious answer is to change that code to

final private Color kleur;

And please note: if you need that "stringified" representation, you can always do

Color whatever = Color.D;
System.out.println(whatever.name());

for example (the above would print "D").

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

3 Comments

Added the class to the OP
Awesome! Your answer is the most correct I believe. Change the return type of kleur to be Color will always make sure that the enum will function properly. I just didn't know that the name of the enum was immediately a type I could use. Thank you, still have a long way to go.
And please see my other comment on the other answer: I would go one step further and rename those enum constants. D, E, F are names that dont "mean" anything. To the contrary: it is just confusing beyond limits.
1

Try this:

Color.valueOf(String.valueOf(this.getKleur())).getQuality();

4 Comments

Thx, but it complains that getKleur returns a char which cannot be converted to String for the enum to be used.
Added the proper call to the OP
Yes, your updated answer works 100% fine. Thank you :)
But please note: the above "solution" is taking away the compile-time safety benefit of using an Enum. If you decide to use an Enum, then use that enum in your source code, not single chars! You know have to remember that these chars have those special meaning; and on the other hand, you have to remember that all your enum constants must stay single chars (what is a rather bad idea in the first place). What is a name like D or S or X telling you about a Color? Meaning: @DarkLite1 please consider fixing your broken design; instead of solving its "symptoms" with a solution like this!
0

this.getKleur() returns a character, not the instances of Color enum , that's why Color.(this.getKleur()).getQuality(); doesn't work. You need to take a roundabout approach here:

Color.valueOf(Character.toString(this.getKleur())).getQuality();

2 Comments

Thx, is your way of casting better than Jerry06's?
No, its just another way of converting character to string.

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.