This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Compare an Object to an Enum list

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the Comparable interface...

 
Declan Dee
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rogel.

Thank You

This should keep me busy for a while

Declan D
 
reply
    Bookmark Topic Watch Topic
  • New Topic