• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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
 
His brain is the size of a cherry pit! About the size of this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic