|
1 | 1 | package com.sbiswas001.twelveproject; |
2 | 2 |
|
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +/** |
| 6 | + * This class enters a number of days and a year(4 digit) |
| 7 | + * from user and prints the date. It also takes an |
| 8 | + * increment(1 to 100) value and shows the new date. |
| 9 | + * @author Sayan Biswas |
| 10 | + * @version 25.04.2022 |
| 11 | + */ |
3 | 12 | public class DateRepresentation { |
| 13 | + |
| 14 | + /** |
| 15 | + * Stores number of initial days entered by user |
| 16 | + */ |
| 17 | + private int days; |
| 18 | + |
| 19 | + /** |
| 20 | + * Stores year entered by user |
| 21 | + */ |
| 22 | + private int year; |
| 23 | + |
| 24 | + /** |
| 25 | + * Stores number of days to increment to old date |
| 26 | + */ |
| 27 | + private int increment; |
| 28 | + |
| 29 | + /** |
| 30 | + * Stores the month index |
| 31 | + */ |
| 32 | + private int monthIndex; |
| 33 | + |
| 34 | + /** |
| 35 | + * Stores the suffix of date |
| 36 | + */ |
| 37 | + private String suffix; |
| 38 | + |
| 39 | + /** |
| 40 | + * Stores the old date |
| 41 | + */ |
| 42 | + private int oldDate; |
| 43 | + |
| 44 | + /** |
| 45 | + * Stores new date |
| 46 | + */ |
| 47 | + private int daysAfter; |
| 48 | + |
| 49 | + /** |
| 50 | + * Stores names of months |
| 51 | + */ |
| 52 | + private final String[] monthNames; |
| 53 | + |
| 54 | + /** |
| 55 | + * Stores number of days of each month |
| 56 | + */ |
| 57 | + private final int[] monthDays; |
| 58 | + |
| 59 | + /** |
| 60 | + * Initialises instance variables |
| 61 | + */ |
| 62 | + private DateRepresentation() { |
| 63 | + days = 0; |
| 64 | + year = 0; |
| 65 | + increment = 0; |
| 66 | + monthIndex = 0; |
| 67 | + suffix = ""; |
| 68 | + oldDate = 0; |
| 69 | + daysAfter = 0; |
| 70 | + monthNames = new String[]{"JANUARY", "FEBRUARY", "MARCH", |
| 71 | + "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", |
| 72 | + "OCTOBER", "NOVEMBER", "DECEMBER"}; |
| 73 | + monthDays = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Inputs the days, year and increment value |
| 78 | + */ |
| 79 | + private void input() { |
| 80 | + Scanner sc = new Scanner(System.in); |
| 81 | + System.out.print("Enter number of days: "); |
| 82 | + days = Integer.parseInt(sc.next()); |
| 83 | + System.out.print("Enter year(4 digits): "); |
| 84 | + year = Integer.parseInt(sc.next()); |
| 85 | + if (year < 1000 || year > 9999) { |
| 86 | + System.out.println("Try again! Year must be of 4 digits."); |
| 87 | + input(); |
| 88 | + } |
| 89 | + System.out.print("Enter increment after old date: "); |
| 90 | + increment = Integer.parseInt(sc.next()); |
| 91 | + if (increment < 1 || increment > 100) { |
| 92 | + System.out.println("Try again! Range is 1 to 100."); |
| 93 | + input(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Calculates old date |
| 99 | + */ |
| 100 | + private void oldDateCalculator() { |
| 101 | + //February should have 29 days if it is leap year |
| 102 | + if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) |
| 103 | + monthDays[1] = 29; |
| 104 | + |
| 105 | + oldDate = days; |
| 106 | + int i = 0; |
| 107 | + while(oldDate > monthDays[i]) { |
| 108 | + oldDate -= monthDays[i]; |
| 109 | + monthIndex = i + 1; |
| 110 | + i++; |
| 111 | + if (i == 12) { |
| 112 | + i = 0; |
| 113 | + year++; |
| 114 | + if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) { |
| 115 | + monthDays[1] = 29; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + //Adding suffix as per day |
| 121 | + if (oldDate % 10 == 1 && oldDate / 10 != 1) { |
| 122 | + suffix = "ST"; |
| 123 | + } else if (oldDate % 10 == 2 && oldDate / 10 != 1) { |
| 124 | + suffix = "ND"; |
| 125 | + } else if (oldDate % 10 == 3 && oldDate / 10 != 1) { |
| 126 | + suffix = "RD"; |
| 127 | + } else { |
| 128 | + suffix = "TH"; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Calculates new date |
| 134 | + */ |
| 135 | + private void newDateCalculator() { |
| 136 | + //Calculating date after n days |
| 137 | + daysAfter = oldDate + increment; |
| 138 | + int i = 0; |
| 139 | + while(daysAfter > monthDays[i]) { |
| 140 | + daysAfter -= monthDays[i]; |
| 141 | + monthIndex = i + 1; |
| 142 | + i++; |
| 143 | + if (i == 12) { |
| 144 | + i = 0; |
| 145 | + year++; |
| 146 | + if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) { |
| 147 | + monthDays[1] = 29; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + //Adding suffix as per day |
| 152 | + if (daysAfter % 10 == 1 && daysAfter / 10 != 1) { |
| 153 | + suffix = "ST"; |
| 154 | + } else if (daysAfter % 10 == 2 && daysAfter / 10 != 1) { |
| 155 | + suffix = "ND"; |
| 156 | + } else if (daysAfter % 10 == 3 && daysAfter / 10 != 1) { |
| 157 | + suffix = "RD"; |
| 158 | + } else { |
| 159 | + suffix = "TH"; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Calls other methods |
| 165 | + * @param args Arguments passed to main method |
| 166 | + */ |
| 167 | + public static void main(String[] args) { |
| 168 | + DateRepresentation ob = new DateRepresentation(); |
| 169 | + ob.input(); |
| 170 | + ob.oldDateCalculator(); |
| 171 | + System.out.println("Old date: " + ob.oldDate + ob.suffix + " " + |
| 172 | + ob.monthNames[ob.monthIndex] + " " + ob.year); |
| 173 | + ob.newDateCalculator(); |
| 174 | + System.out.println("New date: " + ob.daysAfter + ob.suffix + " " + |
| 175 | + ob.monthNames[ob.monthIndex] + " " + ob.year); |
| 176 | + } |
4 | 177 | } |
0 commit comments