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);
        }
    }  


}

Recommended Answers

All 3 Replies

ehm... you add that where you want the user input to be treated/accepted.

Don't put it in your Date class - that's got a clearly defined and very sensible scope now, so don't confuse it.
You could call a simple JOptionPane from your main method then pass the input to the Date class. If you wanted to get more elegant you could create a GUI class (eg extend JPanel) than allows the user to input dates that are validated etc by your Date class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.