简体   繁体   中英

How to implement certain method for enum type

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());
    }
}

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];
    }
}

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

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM