Hi I have this code that performs error checking on initial values. Everythinbg works good but how would I go about allowing for user input? I am thinkging JOptionPane but where would I add that?
public class Date
{
private final int month;
private final int day;
private final int year;
public Date(int m, int d, int y)
{
if (!increment(m, d, y))
throw new RuntimeException("Invalid");
month = m;
day = d;
year = y;
}
private static boolean increment(int m, int d, int y)
{
int[] DAYS = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (m < 1 || m > 12)
return false;
if (d < 1 || d > DAYS[m])
return false;
if (m == 2 && d == 29 && !isLeapYear(y))
return false;
return true;
}
private static boolean isLeapYear(int y)
{
if (y % 400 == 0)
return true;
if (y % 100 == 0)
return false;
return (y % 4 == 0);
}
public Date nextDay()
{
if (increment(month, day + 1, year))
return new Date(month, day + 1, year);
else if (increment(month + 1, 1, year))
return new Date(month + 1, 1, year);
else
return new Date(1, 1, year + 1);
}
public String toString()
{
return day + "-" + month + "-" + year;
}
}
public class DateTest
{
public static void main(String[] args)
{
Date today = new Date(9, 30, 2010);
System.out.println(today);
for (int i = 0; i < 35; i++)
{
today = today.nextDay();
System.out.println(today);
}
}
}