View Single Post
Old 09-19-2005, 07:39 PM   #5 (permalink)
ZimbuTM
Registered User
 
Join Date: Sep 2005
Posts: 10
ZimbuTM is on a distinguished road
Hey. Thanks again for the help. Here's my finished program, please comment on it. It works perfectly, but I'm sure I didn't take the most efficient approach. For example, I know I didn't have to use a boolean variable, but I was experimenting and learning how to use them:

Code:
                                                                                                                                                                                                                                                               

//********************************************************************************
//AUTHOR: RM
//Purpose: 1.   Convert year/month/day into proper date (April 30, 2005)
//.        2.   Display the day number. (April 30th, 2005 = Day 120 in the year)
//         3.   Display the NEXT date of inputted date (May 1, 2005)
//Last modified: September 19th, 2005
//********************************************************************************

import java.util.*;

public class Date
{

   //Initate scanner
   static Scanner keyboard = new Scanner(System.in);

   public static void main(String[] args)
   {

      //Variable and constant declaration
      int year;
      int month;
      int dayOfMonth;
      int dayNumber;
      int nextDay = 0;
      int nextYear = 0;

      String monthName = "";
      String nextMonth = "";

      final int DAYS_IN_JANUARY     = 31;
      int       DAYS_IN_FEBRUARY    = 28;
      final int DAYS_IN_MARCH       = 31;
      final int DAYS_IN_APRIL       = 30;
      final int DAYS_IN_MAY         = 31;
      final int DAYS_IN_JUNE        = 30;
      final int DAYS_IN_JULY        = 31;
      final int DAYS_IN_AUGUST      = 31;
      final int DAYS_IN_SEPTEMBER   = 30;
      final int DAYS_IN_OCTOBER     = 31;
      final int DAYS_IN_NOVEMBER    = 30;
      final int DAYS_IN_DECEMBER    = 31;

      //Prompt user for input
      System.out.print("Please enter a date (year month day): ");
      year       = keyboard.nextInt();
      month      = keyboard.nextInt();
      dayOfMonth = keyboard.nextInt();

   //Boolean for leap year
   boolean isLeapYear = ( (year % 400 == 0) || ((year % 4 == 0) && !(year % 100 == 0)) );

   //Determine number of days in February
   if (isLeapYear)
      DAYS_IN_FEBRUARY = 29;
   else
      DAYS_IN_FEBRUARY = 28;


   //Exclude invalid entries (will give more information to user than just using Switch default cases)

   if (dayOfMonth > 31 || dayOfMonth < 1 || month > 12 || month < 1 || year < 1583)
   {

      System.out.println("\nPlease enter a valid date. Invalid date entries include:\n");
      System.out.println("1. Days larger than 31 OR Days lower than 1");
      System.out.println("2. Months larger than 12 OR Months lower than 1");
      System.out.println("3. Years below 1583\n");
      System.exit(0);
   }

   if (dayOfMonth > 30 && (month == 4 || month == 6 || month == 9 || month == 11))
   {
      System.out.println("\nThe months of April, June, September and November cannot have more than 30 days.");
      System.out.println("Please re-run the program\n");
      System.exit(0);
   }

   if (dayOfMonth > 29 && month == 2)
   {
      System.out.println("\nThe month of February cannot have more than 29 days.");
      System.out.println("Please re-run the program\n");
      System.exit(0);
   }


   if (!isLeapYear && (month == 2 && dayOfMonth == 29))
   {
      System.out.println("\nInputted date does not comply with Gregorian standards. \nFebruary can only have 29 days in a leap year.");
      System.out.println("Please re-run the program\n");
      System.exit(0);
   }




      //Store month name in variable

      switch (month)
      {
            case 1:  monthName = "January";   break;
            case 2:  monthName = "February";  break;
            case 3:  monthName = "March";     break;
            case 4:  monthName = "April";     break;
            case 5:  monthName = "May";       break;
            case 6:  monthName = "June";      break;
            case 7:  monthName = "July";      break;
            case 8:  monthName = "August";    break;
            case 9:  monthName = "September"; break;
            case 10: monthName = "October";   break;
            case 11: monthName = "November";  break;
            case 12: monthName = "December";  break;
            default: break;
      }

      //Store day number in variable

      switch (month)
      {
         case 1: dayNumber  =  dayOfMonth; break;
         case 2: dayNumber  = (dayOfMonth + DAYS_IN_JANUARY); break;
         case 3: dayNumber  = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY); break;
         case 4: dayNumber  = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH); break;
         case 5: dayNumber  = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL); break;
         case 6: dayNumber  = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY); break;
         case 7: dayNumber  = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY + DAYS_IN_JUNE); break;
         case 8: dayNumber  = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY + DAYS_IN_JUNE + DAYS_IN_JULY); break;
         case 9: dayNumber  = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY + DAYS_IN_JUNE + DAYS_IN_JULY + DAYS_IN_AUGUST); break;
         case 10: dayNumber = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY + DAYS_IN_JUNE + DAYS_IN_JULY + DAYS_IN_AUGUST + DAYS_IN_SEPTEMBER); break;
         case 11: dayNumber = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY + DAYS_IN_JUNE + DAYS_IN_JULY + DAYS_IN_AUGUST + DAYS_IN_SEPTEMBER + DAYS_IN_OCTOBER); break;
         case 12: dayNumber = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY + DAYS_IN_JUNE + DAYS_IN_JULY + DAYS_IN_AUGUST + DAYS_IN_SEPTEMBER + DAYS_IN_OCTOBER + DAYS_IN_NOVEMBER); break;
         default: dayNumber = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY + DAYS_IN_JUNE + DAYS_IN_JULY + DAYS_IN_AUGUST + DAYS_IN_SEPTEMBER + DAYS_IN_OCTOBER + DAYS_IN_NOVEMBER + DAYS_IN_DECEMBER); break;
      }


      //Store next date values in variables

      switch (month)
      {
         case 1: if  (dayOfMonth == DAYS_IN_JANUARY)
                  {
                    nextDay = 1;
                    nextMonth = "February";
                  }

         case 2: if (dayOfMonth == DAYS_IN_FEBRUARY)
                  {
                    nextDay = 1;
                    nextMonth = "March";
                  }

         case 3: if (dayOfMonth == DAYS_IN_MARCH)
                  {
                    nextDay = 1;
                    nextMonth = "April";
                  }

         case 4: if (dayOfMonth == DAYS_IN_APRIL)
                  {
                    nextDay = 1;
                    nextMonth = "May";
                  }

         case 5: if (dayOfMonth == DAYS_IN_MAY)
                  {
                     nextDay = 1;
                     nextMonth = "June";
                  }

         case 6: if (dayOfMonth == DAYS_IN_JUNE)
                  {
                     nextDay = 1;
                     nextMonth = "July";
                  }

         case 7: if (dayOfMonth == DAYS_IN_JULY)
                  {
                     nextDay = 1;
                     nextMonth = "August";
                  }

         case 8: if (dayOfMonth == DAYS_IN_AUGUST)
                  {
                     nextDay = 1;
                     nextMonth = "September";
                  }

         case 9: if (dayOfMonth == DAYS_IN_SEPTEMBER)
                  {
                     nextDay = 1;
                     nextMonth = "October";
                  }

         case 10: if (dayOfMonth == DAYS_IN_OCTOBER)
                  {
                     nextDay = 1;
                     nextMonth = "November";
                  }

         case 11: if (dayOfMonth == DAYS_IN_NOVEMBER)
                  {
                     nextDay = 1;
                     nextMonth = "December";
                  }

         case 12: if (dayOfMonth == DAYS_IN_DECEMBER);
                  {
                     nextDay = 1;
                     nextMonth = "January";
                  }

         default: {
			 		nextDay = (dayOfMonth + 1);
			 		nextMonth = monthName; break;
			      }
      }

      //Store next year (if any) value in variable

      if (monthName == "December" && dayOfMonth == 31)
      {
         nextYear = (year + 1);
      }
      else
      {
         nextYear = year;
      }



      System.out.println("\nThe date is: " + monthName + " " + dayOfMonth + ", " + year);
      System.out.println("The date's day number is: " + dayNumber);
      System.out.println("The next date is: " + nextMonth + " " + nextDay + ", " + nextYear + "\n");

















   }

}
ZimbuTM is offline   Reply With Quote