Reputation: 11
One of the requirements is a getMonthName
method that returns the name if the month, i.e. January is 1. And a toString
that returns the month name (String represesentation) , and I'm just worried that there's an easier way for what I'm doing than this:
import java.util.Scanner;
import java.io.*;
public class Month {
private int monthNum = 0;
private String monthName;
String monthOne = "JANUARY";
String monthTwo = "FEBRUARY";
String monthThree = "MARCH";
String monthFour = "APRIL";
String monthFive = "MAY";
String monthSix = "JUNE";
String monthSeven = "JULY";
String monthEight = "AUGUST";
String monthNine = "SEPTEMBER";
String monthTen = "OCTOBER";
String monthEleven = "NOVEMBER";
String monthTwelve = "DECEMBER";
//CONSTRUCTORS
public Month()
{
monthNum = 1;
}
public Month(int monthNum)
{
this.monthNum = monthNum;
if ((monthNum > 12) || (monthNum <1))
{
monthNum = 1;
}
}
public Month(String monthOne, String monthTwo, String monthThree, String monthFour, String monthFive, String monthSix, String monthSeven,
String monthEight, String monthNine, String monthTen, String monthEleven, String monthTwelve)
{
monthName.toUpperCase();
if (monthName.equals(monthOne))
{
monthNum = 1;
}
else if (monthName.equals(monthTwo))
{
monthNum = 2;
}
else if (monthName.equals(monthThree))
{
monthNum = 3;
}
else if (monthName.equals(monthFour))
{
monthNum = 4;
}
else if (monthName.equals(monthFive))
{
monthNum = 5;
}
else if (monthName.equals(monthSix))
{
monthNum = 6;
}
else if (monthName.equals(monthSeven))
{
monthNum = 7;
}
else if (monthName.equals(monthEight))
{
monthNum = 8;
}
else if (monthName.equals(monthNine))
{
monthNum = 9;
}
else if (monthName.equals(monthTen))
{
monthNum = 10;
}
else if (monthName.equals(monthEleven))
{
monthNum = 11;
}
else
{
monthNum = 12;
}
}
//METHODS
public void setMonthNum(int monthNum)
{
this.monthNum = monthNum;
if ((monthNum >12) || (monthNum<1))
{
monthNum = 1;
}
}
public int getMonthNumber()
{
return monthNum;
}
public String getMonthName()
{
if (monthNum == 1)
{
return "January";
}
else if (monthNum == 2)
{
return "February";
}
else if (monthNum == 3)
{
return "March";
}
else if (monthNum == 4)
{
return "April";
}
else if (monthNum == 5)
{
return "May";
}
else if (monthNum == 6)
{
return "June";
}
else if (monthNum == 7)
{
return "July";
}
else if (monthNum == 8)
{
return "August";
}
else if (monthNum == 9)
{
return "September";
}
else if (monthNum == 10)
{
return "October";
}
else if (monthNum == 11)
{
return "November";
}
else
{
return "December";
}
}
public String toString()
{
if (monthNum == 1)
{
return "January";
}
else if (monthNum == 2)
{
return "February";
}
else if (monthNum == 3)
{
return "March";
}
else if (monthNum == 4)
{
return "April";
}
else if (monthNum == 5)
{
return "May";
}
else if (monthNum == 6)
{
return "June";
}
else if (monthNum == 7)
{
return "July";
}
else if (monthNum == 8)
{
return "August";
}
else if (monthNum == 9)
{
return "September";
}
else if (monthNum == 10)
{
return "October";
}
else if (monthNum == 11)
{
return "November";
}
else
{
return "December";
}
}
}
Am I missing something or is this the best way at my level to do this?
Upvotes: 1
Views: 2579
Reputation: 738
It is simply
class Month {
private static String monthNames[] = {
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"};
private int monthNumber;
public Month(int n) {
if (n < 1 || n > 12) {
monthNumber = 1;
} else {
monthNumber = n;
}
}
public String getMonthName() {
return monthNumber[monthNumber - 1];
}
@Override
public String toString() {
return getMonthName();
}
}
Upvotes: 0
Reputation: 96
If you must implement a Month class yourself then look into enums, otherwise check out the docs for java.time.Month – Try something like this...
import java.time.Month;
public Month getMonthName( int monthNum )
{
return Month.of( monthNum );
}
Or simply...
System.out.println( Month.of( monthNumber ) );
Upvotes: 1
Reputation: 633
Another approach you could use is HashMap where key would be the month's number and value is its name. Using HashMap you can also get the key based on the value and vice-versa which seems you want to do. Also try avoiding too much else ifs. You can use switch instead.
Upvotes: 0
Reputation:
Here are some suggestions to improve your class:
use KeyValuePair(example, map) to store the month list, since, it would be easy to maintain and retrieve information.
If you use keyValuePair collection you can access the month using id directly rather than using nested if statements.(as you did in getMonthName() and toString() method)
Upvotes: 0