0

I am new to programming and Java and am currently learning enum types.

I have created an enum type Card which stores the value of each card in a deck of cards, for example Two = 2, Three = 3 ... Ace = 11. Each picture card has the same value, 10.

I am trying to implement a method getPrevious(), that will return the previous enum value in the list. For example if the method is called on SIX, it will return FIVE, and when called on TWO will return ACE. However i am struggling to figure out a way to do so.

Below you can see the current code that i have written, any help or tips on how to implement the method getPrevious() would be incredibly helpful.

public class EnumPractice {

    public enum Card {
        TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), 
        NINE(9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11), ;

        private int value;
        private Card card;

        Card(int value){
        this.value = value;
        }

        public int getValue(){
            return value;
        }

        public String toString(){
            return "The value of this card is: " + value;
        }
    }

    public static void main(String[] args) {
        System.out.println(Card.ACE.getValue());
    }
}
Mateusz Korwel
  • 1,118
  • 1
  • 8
  • 14
Mickd94
  • 63
  • 8
  • 1
    Here is a post which should help you, based on what I think your root problem is: [How to get Enum Value from index in Java?](http://stackoverflow.com/questions/6692664/how-to-get-enum-value-from-index-in-java) – CubeJockey Dec 29 '15 at 19:50
  • Similar problem: http://stackoverflow.com/questions/18883646/java-enum-methods – Pshemo Dec 29 '15 at 20:07

3 Answers3

2

Your getPrevious() method can look like this

public enum Card {
    TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8),
    NINE(9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11);

    private int value;

    Card(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public Card getPrevious() {
        int previous = ordinal() - 1;
        if (previous < 0 ) { // if negative, then we have to go to the last element
            previous += values().length;
        }

        return Card.values()[previous];
    }
}
Mateusz Korwel
  • 1,118
  • 1
  • 8
  • 14
  • Thank you very much, this method was very helpful as it took into account the possibility of a negative index. However, how would i go about changing this method to output say, `KING` rather than `"10" ` or `ACE` rather than `11`? – Mickd94 Dec 29 '15 at 20:28
  • 1
    @Mickd94 `getPrevious` returns `KING` and `ACE` since it is declared to return `Card`. How you will use that values later is up to you. – Pshemo Dec 29 '15 at 20:29
  • If you want saw the value, you can invoke method `getValue()`. For example: `Card.TWO.getPrevious().getValue()` – Mateusz Korwel Dec 29 '15 at 20:31
  • @Pshemo Ah, i now see my toString method was outputting the int value rather than the actual enum type value, after commenting it out it is now outputting the correct values, thank you for pointing this out. – Mickd94 Dec 29 '15 at 20:34
1

here is your getPrevious() implemenation

import java.util.Arrays;

public class EnumPractice {

    public enum Card {

        TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), 
        NINE(9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11), ;

        private int value;
        private Card card;

        Card(int value){
        this.value = value;
        }

        public int getValue(){
            return value;
        }

        public String toString(){
            return "The value of this card is: " + value;
        }

        public Card getPrevious() {
            int pos = ordinal();
            if (pos == 0)
                return ACE;
            return values()[pos - 1];
        }
    }

    public static void main(String[] args) {

    System.out.println(Card.THREE.getPrevious());
    System.out.println(Card.TWO.getPrevious());

    }

}

the above program output is

The value of this card is: 2
The value of this card is: 11
Pshemo
  • 122,468
  • 25
  • 185
  • 269
QuakeCore
  • 1,886
  • 2
  • 15
  • 33
  • You probably wanted to use `ordinal()` not `getValue()`. Also what is the point of having `private Card card;` if you are not using it? – Pshemo Dec 29 '15 at 20:06
  • `Arrays.binarySearch(this.values(), this)` can be replaced with `ordinal()`. – Pshemo Dec 29 '15 at 20:16
  • Also `values()` is static method so try not to invoking it from instance. Same about enum values like ACE (we don't need that `this.` before it). – Pshemo Dec 29 '15 at 20:17
  • 1
    I updated your code a little to remove these problems. Hope you don't mind. – Pshemo Dec 29 '15 at 20:25
1

Other approach could be holding reference to previous card. You could set it after all cards are created in static block like:

public enum Card {
    TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), 
    NINE(9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11);

    private int value;
    private Card previous;

    static{
        //lets set previous element for cards
        //we will iterate starting from TWO so its previous should be:
        Card previous = ACE;//yes ACE
        for (Card card : values()){
            card.previous = previous;
            previous = card; 
        }
    }

    Card(int value){
        this.value = value;
    }

    public int getValue(){
        return value;
    }

    public Card getPrevious(){
        return previous; //<-- simple getter is enough since we took care 
                         //    of setting this value earlier in static 
                         //    initialization block
    }

    public String toString(){
        return "The value of this card is: " + value;
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269