Hello I have an enum
of months.
public enum Months{
JANUARY, FEBUARY, MARCH,
APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
}
In the service I have a method that should only work if the month is greater than MARCH
i.e. APRIL, MAY, ....
. How can I write the following line more eloquently:
public void monthCheck(Object object){
if (object.month!= Months.JANUARY || object.month!= Months.FEBUARY || object.month != Months.MARCH){
//do something here
}
}
CodePudding user response:
Your enum should have get/set methods like
public enum Months {
JANUARY("january"), and so on;
String months;
Months(String months) {
this.months = months;
}
public String getMonths() {
return toggleStatus;
}
}
Now when you do Months.JANUARY.getMonths() anywhere in your project this will return "january" value. If you write
JANUARY("1"), FEBRUARY("2")
Then it will return 1,2 and so on
CodePudding user response:
You can use the ordinal()
on the enum to achieve this.
public void monthCheck(Object object) {
if(!(object instanceof Months)){
return;
}
Months input = (Months) object;
if( input.ordinal() > 2 ) {
/* your logic here */
System.out.println(input.ordinal());
}
}
CodePudding user response:
Try This Code Here:
public enum Months{
JANUARY, FEBUARY, MARCH,
APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
public void monthCheck(String str){
if (!str.equals(Months.JANUARY) ||
!str.equals(Months.FEBUARY) || !str.equals(Months.MARCH)){
//do somwthing here
}
}
}
Pass A Parameter When Needed In A Another Class.