File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compare an Object to an Enum list

 
Declan Dee
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings All,

My name is Declan and I am new to both Java programming, programming in general and of course this site.

I hope to get to know you all in the coming years (!)

My first question is this. I have the following two Enums:

public enum CardSuit { SPADES, CLUBS, DIAMONDS, HEARTS } and

public enum CardRank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }

I create an object (card1) with the following constructor:

public card (String suit, String rank) {
this.name = name;
this.rank = rank;
}

My question is, how do I compare the card1 object to these Enums?

For example if my object is Card1 (Spades, Six) and I create a second object (Hearts, Two), I want to have it that because SPADES is first (higher) in the Enum list that object1 (Card1) is automatically higher that object2 (Card2).

I have to write a program that compares the values of cards.

I have been working with CompareTo() with little success

Any help is greatly appreciated

Thanks in advance

Declan


 
rogel garcia
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do Something like that


But I think it is better to change the attributes of the class card to match the enums, ex:
 
rogel garcia
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using the Comparable interface...

 
Declan Dee
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rogel.

Thank You

This should keep me busy for a while

Declan D
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic