Reputation: 18572
I have this snippet of code and want to know if exist way to make it much more readable?
My code:
public String getMonthName()
{
if (1 == this.monthNumber) {
monthName = "January";
} else if (2 == this.monthNumber) {
monthName = "February";
} else if (3 == this.monthNumber) {
monthName = "March";
} else if (4 == this.monthNumber) {
monthName = "April";
} else if (5 == this.monthNumber) {
monthName = "May";
} else if (6 == this.monthNumber) {
monthName = "June";
} else if (7 == this.monthNumber) {
monthName = "July";
} else if (8 == this.monthNumber) {
monthName = "August";
} else if (9 == this.monthNumber) {
monthName = "September";
} else if (10 == this.monthNumber) {
monthName = "Oktober";
} else if (11 == this.monthNumber) {
monthName = "November";
} else if (12 == this.monthNumber) {
monthName = "December";
}
return monthName;
}
/**
* Gets the number of days in this month
* @return the number of days in this month in a non-leap year
*/
public int getNumberOfDays()
{
int numberOfDays;
if (monthNumber == 1 || monthNumber == 3 || monthNumber == 5 || monthNumber == 7 || monthNumber == 9 || monthNumber == 11) {
numberOfDays = 31;
} else if (monthNumber == 4 || monthNumber == 6 || monthNumber == 8 || monthNumber == 10 || monthNumber == 12) {
numberOfDays = 30;
} else {
numberOfDays = 28;
}
return numberOfDays;
}
}
How better way to refactoring code? Make it much more readable.
Upvotes: 0
Views: 104
Reputation: 1455
Use enum
s. Months and its orderung/number and days don't change, so months are quite a good example when and how to use them.
Let the enum have an int numberOfDays
and if you need a monthNumber
. Though you need to get into enum
s you will gain a lot as you can reuse your code and probably can omit your if/switch statement as well.
Upvotes: 0
Reputation: 40336
public String getMonthName(){
switch (monthNumber) {
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default: return "Invalid month";
}
}
Upvotes: 0
Reputation: 35547
Make two arrays
for month and number of date per month. both are size of 12.
in month array put name of the month in order from index 0 to 11 and put number of days in order to from index 0 to 11. now you can retrieve data by index.
Upvotes: 0
Reputation: 1143
Not sure if this is exactly what you were asking for, but it's a start! Comment if you would like it in some other way.
public string getMonthName(int nbr){
String[] months = {"January", "February", "Mars", "April", "May", "June", "July", "August", "September", "Oktober", "November", "December"}:
return months[nbr-1];
}
public int nbrOfDays(int nbr){
int[] nbrOfDays= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
return nbrOfDays[nbr-1];
}
EDIT: fixed the "index 0 issue".
Upvotes: 1
Reputation: 61515
You should use the Calendar
class, it was provided to save you from all this extra branching. You also won't have to determine if it is a Leap Year, the API will take of that for you.
final String[] months = new String[]{ "January", "February", ... };
public String getMonthName() {
return months[Calendar.getInstance().get(Calendar.MONTH)];
}
public int getNumberOfDays() {
return Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);
}
Upvotes: 1
Reputation: 45576
Use enum
public enum Month
{
JANUARY( "January", 31 ),
FEBRUARY( "February", 28 ),
...
DECEMBER( "December", 31 );
private String monthName;
private int numOfDays;
private Month ( String monthName, int numOfDays )
{
this.monthName = monthName;
this.numOfDays = numOfDays;
}
public String getDisplayName ( )
{
return monthName;
}
public int getNumOfDays ( )
{
return numOfDays;
}
}
Upvotes: 3
Reputation: 8724
public String getMonthName(){
switch (this.monthNumber) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
return monthString;
}
Upvotes: 1
Reputation: 9775
String[] months = String[] {"Jan", "Feb", ... , "Dec"};
String monthName = months[monthNumber - 1];
Upvotes: 0
Reputation: 4477
Make an array that stores all the names. If this.monthNumber is between 1 and 12, read the name out of the array.
Upvotes: 1