The assignment is to create my own Date class. Restriction is that we cannot use the library date class. Here is what I have..
public class Date {
/* Use array to convert integer month into
month string abbreviation */
public String convertMonth(int month) {
String[] m = new String[12];
m[0] = "JAN";
m[1] = "FEB";
m[2] = "MAR";
m[3] = "APR";
m[4] = "MAY";
m[5] = "JUN";
m[6] = "JUL";
m[7] = "AUG";
m[8] = "SEP";
m[9] = "OCT";
m[10] = "NOV";
m[11] = "DEC";
return m[month - 1];
}
private int month;
private int day;
private int year;
/* default constructor */
Date () {
month = 1;
day = 1;
year = 2000;
}
public Date (int month, int day, int year) {
//check if entry is valid
if (year < 0)
throw new IllegalArgumentException("Year must be positive >0");
if (month < 1 || month > 12)
throw new IllegalArgumentException("The month bust be between 1 and 12 inclusive");
if (day < 1 || day > 31)
throw new IllegalArgumentException("Day value " + day + " is invalid for month " + month);
this.month = month;
this.day = day;
this.year = year;
System.out.printf("%s\n", this);
}
// convert into MMM-DD-YYYY format
public String toString() {
return String.format(convertMonth(month) + "-%02d-%04d", day, year);
}
// return true if the receiving date and parameter date are equal
public boolean equals(Date other) {
Date Date = new Date();
if (Date.equals(other)) {
return true;
}
else return false;
}