Advertisement
jig487

calendar Printer

Feb 15th, 2024 (edited)
1,110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.62 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Formatter;
  3.  
  4. /**
  5.  * Description goes here.
  6.  *
  7.  * Last Modified: 2/14/23
  8.  * Author: Brock Hansen
  9.  */
  10.  
  11. public class DisplayMonth {
  12.    
  13.    private static int[] monthDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  14.    private static String[] monthNames = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
  15.    private static int weekStart;
  16.    private static int displayMonth;
  17.    private static int maxDays;
  18.    private static int leapYear = 2;
  19.    
  20.     public static void main(String[] args) {
  21.      
  22.       //Gets day the week starts on
  23.       //Asks for user input until receives an int between 1-7, inclusive
  24.       while( weekStart < 1 || weekStart > 7) {
  25.            System.out.println("What day does the month start on? Enter a number between 1-7 (1=Monday, 2=Tuesday, 3=Wednesday...)");
  26.          Scanner scan = new Scanner(System.in);
  27.          weekStart = scan.nextInt();
  28.          
  29.          if( weekStart < 1 || weekStart > 7 ) {
  30.             System.out.println("Not a valid option! Please pick a number between 1-7!");
  31.          }
  32.          scan.close();
  33.       }
  34.      
  35.       //Gets month to display, if february then asks if leap year
  36.       //Asks for user input until receives an int between 1-12, inclusive. If february (2), asks if leap year (int 0 or 1)
  37.       while( displayMonth < 1 || displayMonth > 12) {
  38.            System.out.println("What month would you like to display? Enter a number between 1-12 (1=January, 2=February, 3=March...)");
  39.          Scanner scan = new Scanner(System.in);
  40.          displayMonth = scan.nextInt();
  41.          
  42.          if( displayMonth < 1 || displayMonth > 12 ) {
  43.             System.out.println("Not a valid option! Please pick a number between 1-12!");
  44.            
  45.             //Is february?
  46.          } else if( displayMonth == 2 ) {
  47.            
  48.             //Asks if leap year or not
  49.             while( leapYear < 0 || leapYear > 1) {
  50.                  System.out.println("Is this a leap year? 0=no, 1=yes");
  51.                leapYear = scan.nextInt();
  52.          
  53.                if( leapYear < 0 || leapYear > 1 ) {
  54.                   System.out.println("Not a valid option! Please pick either 0 or 1!");
  55.                }
  56.             }
  57.          }
  58.          scan.close();
  59.       }
  60.      
  61.       //set max days to count to for calendar
  62.       //if february, check if leap year. else grab from list
  63.       maxDays = displayMonth==2 ? ( leapYear==0 ? 28 : 29 ) : monthDays[displayMonth-1];
  64.      
  65.       //Display calendar
  66.       System.out.println(monthNames[displayMonth-1]);
  67.       System.out.println("Su Mo Tu We Th Fr Sa");
  68.      
  69.       //Keeps track of how many days we've printed so far
  70.       int dayCount = 1;
  71.      
  72.       //Print number grid for calendar
  73.       for(int y = 0; y < 7; y++) {
  74.          for(int x = 0; x < 7; x++) {
  75.             //calculates the current position, assuming we are on a 2d grid w/ row lengths of 7
  76.             int gridNum = y*7+x+1;
  77.                  
  78.             //Print spaces until we get to the correct starting day, then print day numbers.
  79.             //Print spaces when we are past the max days for this month
  80.             if(gridNum >= weekStart && dayCount <= maxDays) {
  81.                System.out.print(String.format("%" + 2 + "s", dayCount) + " ");
  82.                dayCount++;
  83.             } else {
  84.                System.out.print("   ");
  85.             }
  86.          }
  87.          //If we have printed all the days, break out of the for() loop.
  88.          if(dayCount > maxDays) {
  89.             break;
  90.          }
  91.          System.out.println();
  92.       }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement