dimanche 9 octobre 2016

I have to create a month class in Java

one of the requirements is a getMonthName method that returns the name f the month, i.e. january is 1. and a toString that returns the same thing, 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?

Aucun commentaire:

Enregistrer un commentaire