ENUMs
Here is my Soldier class
package militaryStructure;
public class Soldier {
public enum Rank { PRIVATE, PFC, CORPORAL, SERGENT,
CAPTAIN, MAJOR, LT_COLONEL, COLONEL, GENERAL, ADMIRAL}
private String name;
private Rank rank;
private int serialNumber;
public Soldier(String name, Rank rank, int serialNumber) {
this.name = name;
this.rank = rank;
this.serialNumber = serialNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Rank getRank() {
return rank;
}
public void setRank(Rank rank) {
this.rank = rank;
}
public int getSerialNumber() {
return serialNumber;
}
public Rank promote(Rank rank) {
int x = rank.ordinal() + 1;
if ( x < 11 )
x++;
return Rank.ADMIRAL;
}
}
If I want to promote a soldier, I have a method called promote that
takes in a rank of the Enum Rank type. My thinking is that since each
"rank" has an ordinal, then I should be able to
a) determine ordinal of the current rank int x = rank.ordinal() + 1;
b) increment the ordinal x++;
c) return the Rank in the position of the incremented ordinal
But I can't find a method attached to Rank which will let me do this.
Is there a way to do this without having to change anything in my code
besides the promote method?
I've looked in the API and done a google search but either there isn't a
way to do what I want or I've missed something.... Note: This is just
an experiment..I am NOT taking this any further than trying to construct
a Soldier and promote him/her to the next rank. My ranks are not
correct, just remembering from M.A.S.H so don't flame me. All I want to
know is how to pass back the next up rank... I can't have all my
soldiers getting promoted to Admiral.