一前言 題目集四主要考察的是對LocalDate,ArrayList,HashSet等Java自帶類的使用 題目集五主要考察的是對正則表達式的使用,以及對其題目集三的時間題目的進行類結構的改變 題目集六隻有一道題,主要是對題目集四的第一題進行加大難度 總的來說這幾次的題目量比前面幾次都要少,但是題目 ...
一前言
題目集四主要考察的是對LocalDate,ArrayList,HashSet等Java自帶類的使用
題目集五主要考察的是對正則表達式的使用,以及對其題目集三的時間題目的進行類結構的改變
題目集六隻有一道題,主要是對題目集四的第一題進行加大難度
總的來說這幾次的題目量比前面幾次都要少,但是題目難度開始加深。
二設計與分析
由於部分題目過於簡單,所以這裡主要是對題目集四的7-1,題目集五的7-5,7-6,題目集六的7-1進行分析
題目集四7-1:當時見到這道題時認為難度較大且複雜,所以在這次題目集時放棄了寫這道題,但由於題目集六的7-1題目是在此基礎上的加深所以主要在後面分析
題目集五7-5:此題目主要是我們將前面題目集三的7-3的進行如下類圖的類結構更改
所以我們主要是對類圖進行分析,我們發現各個類之間是聚合的關係,每一個類與類之間聚合,所以我們按照此類圖設計類時應該由year到month到day到DateUtil之間一個個寫。最後的題目要求的主要功能則在DateUtil這裡實現,因為這裡是最後一個聚合。
代碼如下:
1 import java.util.Scanner; 2 public class Main { 3 public static void main(String[] args) { 4 Scanner input = new Scanner(System.in); 5 int year = 0; 6 int month = 0; 7 int day = 0; 8 9 int choice = input.nextInt(); 10 11 if (choice == 1) { // test getNextNDays method 12 int m = 0; 13 year = Integer.parseInt(input.next()); 14 month = Integer.parseInt(input.next()); 15 day = Integer.parseInt(input.next()); 16 17 DateUtil date = new DateUtil(year, month, day); 18 19 if (!date.checkInputValidity()) { 20 System.out.println("Wrong Format"); 21 System.exit(0); 22 } 23 24 m = input.nextInt(); 25 26 if (m < 0) { 27 System.out.println("Wrong Format"); 28 System.exit(0); 29 } 30 System.out.println(date.getNextNDays(m).showDate()); 31 } else if (choice == 2) { // test getPreviousNDays method 32 int n = 0; 33 year = Integer.parseInt(input.next()); 34 month = Integer.parseInt(input.next()); 35 day = Integer.parseInt(input.next()); 36 37 DateUtil date = new DateUtil(year, month, day); 38 39 if (!date.checkInputValidity()) { 40 System.out.println("Wrong Format"); 41 System.exit(0); 42 } 43 44 n = input.nextInt(); 45 46 if (n < 0) { 47 System.out.println("Wrong Format"); 48 System.exit(0); 49 } 50 System.out.println(date.getPreviousNDays(n).showDate()); 51 } else if (choice == 3) { //test getDaysofDates method 52 year = Integer.parseInt(input.next()); 53 month = Integer.parseInt(input.next()); 54 day = Integer.parseInt(input.next()); 55 56 int anotherYear = Integer.parseInt(input.next()); 57 int anotherMonth = Integer.parseInt(input.next()); 58 int anotherDay = Integer.parseInt(input.next()); 59 60 DateUtil fromDate = new DateUtil(year, month, day); 61 DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 62 63 if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { 64 System.out.println(fromDate.getDaysofDates(toDate)); 65 } else { 66 System.out.println("Wrong Format"); 67 System.exit(0); 68 } 69 } 70 else{ 71 System.out.println("Wrong Format"); 72 System.exit(0); 73 } 74 } 75 public static class Day { 76 int value; 77 int mon_maxnum[]= new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};//數組儲存各月份天數 78 Month month = new Month(); 79 Day(){ 80 81 } 82 Day(int yearValue,int monthValue,int dayValue){ 83 value = dayValue; 84 month = new Month( yearValue, monthValue); 85 } 86 87 public void setValue(int value) { 88 this.value = value; 89 } 90 91 public int getValue() { 92 return value; 93 } 94 95 public Month getMonth() { 96 return month; 97 } 98 99 public void setMonth(Month month) { 100 this.month = month; 101 } 102 public void resetMin(){ 103 value = 1; 104 } 105 public void resetMax(){ 106 if(month.year.isLeapYear()){ 107 mon_maxnum[2]=29; 108 }else{ 109 mon_maxnum[2]=28; 110 } 111 value = mon_maxnum[month.value]; 112 } 113 boolean validate(){ 114 if(value>mon_maxnum[month.value]||value<1){ 115 return false; 116 }else{ 117 return true; 118 } 119 } 120 public void dayIncrement(){ 121 value++; 122 } 123 public void dayReduction(){ 124 value--; 125 } 126 } 127 public static class Month { 128 int value; 129 Year year = new Year(); 130 Month(){ 131 132 } 133 Month(int yearValue,int monthValue){ 134 value = monthValue; 135 year.setValue(yearValue); 136 } 137 138 public void setValue(int value) { 139 this.value = value; 140 } 141 142 public int getValue() { 143 return value; 144 } 145 146 public Year getYear() { 147 return year; 148 } 149 150 public void setYear(Year year) { 151 this.year = year; 152 } 153 public void resetMin(){ 154 value = 1; 155 } 156 public void resetMax(){ 157 value = 12; 158 } 159 boolean validate(){ 160 if(value>12||value<1){ 161 return false; 162 }else{ 163 return true; 164 } 165 } 166 public void monthIncrement(){ 167 value++; 168 } 169 public void montReduction(){ 170 value--; 171 } 172 } 173 public static class Year { 174 int value; 175 Year(){ 176 177 } 178 Year(int value){ 179 this.value = value; 180 } 181 182 public int getValue() { 183 return value; 184 } 185 186 public void setValue(int value) { 187 this.value = value; 188 } 189 boolean validate(){ 190 if(value>2050||value<1900){ 191 return false; 192 }else{ 193 return true; 194 } 195 } 196 boolean isLeapYear(){ 197 if((value%4==0&&value%100!=0)||value%400==0){ 198 return true; 199 } 200 return false; 201 } 202 void yearIncrement(){ 203 value++; 204 } 205 void yearReduction(){ 206 value--; 207 } 208 } 209 public static class DateUtil { 210 Day day = new Day(); 211 DateUtil(){ 212 213 } 214 DateUtil(int y,int m,int d){ 215 day = new Day(y,m,d); 216 } 217 218 public Day getDay() { 219 return day; 220 } 221 222 public void setDay(Day day) { 223 this.day = day; 224 } 225 boolean checkInputValidity(){ 226 if(day.month.year.isLeapYear()){ 227 day.mon_maxnum[2]=29; 228 } 229 if(day.month.year.value<1900||day.month.year.value>2050||day.month.value<1||day.month.value>12||day.value<1||day.value>day.mon_maxnum[day.month.value]){ 230 return false; 231 } 232 else{ 233 return true; 234 } 235 } 236 boolean compareDates(DateUtil date){ 237 if(this.day.month.year.value>date.day.month.year.value){ 238 return true; 239 }else if(this.day.month.year.value==date.day.month.year.value){ 240 if(this.day.month.value>date.day.month.value){ 241 return true; 242 }else if(this.day.month.value==date.day.month.value){ 243 if(this.day.value>date.day.value){ 244 return true; 245 }else{ 246 return false; 247 } 248 } 249 }else if(this.day.month.year.value<date.day.month.year.value){ 250 return false; 251 } 252 return false; 253 } 254 boolean equalTwoDates(DateUtil date){ 255 if(this.day.month.year.value==date.day.month.year.value&&this.day.month.value==date.day.month.value&&this.day.value==date.day.value){ 256 return true; 257 }else{ 258 return false; 259 } 260 } 261 262 DateUtil getNextNDays(int n){ 263 int i; 264 for(i=0;i<n;i++) { 265 if (day.month.year.isLeapYear()) { 266 day.mon_maxnum[2] = 29; 267 } else { 268 day.mon_maxnum[2] = 28; 269 } 270 if (day.value != day.mon_maxnum[day.month.value]) { 271 day.dayIncrement(); 272 273 } else { 274 if (day.month.value == 12) { 275 day.month.resetMin(); 276 day.resetMin(); 277 day.month.year.yearIncrement(); 278 } else { 279 day.resetMin(); 280 day.month.monthIncrement(); 281 } 282 } 283 } 284 return this; 285 } 286 DateUtil getPreviousNDays(int n){ 287 int i; 288 for(i=0;i<n;i++){ 289 if(day.month.year.isLeapYear()){ 290 day.mon_maxnum[2]=29; 291 }else{ 292 day.mon_maxnum[2]=28; 293 } 294 if(day.value!=1){ 295 day.dayReduction();//判斷月份是否需要變化 296 297 }else{ 298 if(day.month.value==1){//判斷年份是否需要變化 299 day.month.value=12; 300 day.value=31; 301 day.month.year.yearReduction(); 302 }else{ 303 day.month.montReduction(); 304 day.value = day.mon_maxnum[day.month.value]; 305 } 306 } 307 } 308 return this; 309 } 310 int getDaysofDates(DateUtil date){ 311 int i,n=0; 312 int j=365,k=366; 313 if(this.compareDates(date)){//比較兩個日期判斷是否進行進行更換操作 314 if(this.day.month.year.value==date.day.month.year.value){ 315 if(day.month.year.isLeapYear()){ 316 day.mon_maxnum[2]=29; 317 }else{ 318 day.mon_maxnum[2]=28; 319 } 320 for(i=date.day.month.value+1;i<this.day.month.value;i++){ 321 n=n+day.mon_maxnum[i]; 322 } 323 n=n+day.mon_maxnum[date.day.month.value]-date.day.value+this.day.value; 324 }else{ 325 for(i=date.day.month.year.value+1;i<this.day.month.year.value;i++){ 326 Year flag = new Year(i); 327 if(flag.isLeapYear()){ 328 n=n+k; 329 }else{ 330 n=n+j; 331 } 332 } 333 for(i=date.day.month.value+1;i<=12;i++){ 334 if(day.month.year.isLeapYear()){ 335 day.mon_maxnum[2]=29; 336 }else{ 337 day.mon_maxnum[2]=28; 338 } 339 n=n+day.mon_maxnum[i]; 340 } 341 for(i=1;i<this.day.month.value;i++){ 342 if(day.month.year.isLeapYear()){ 343 day.mon_maxnum[2]=29; 344 }else{ 345 day.mon_maxnum[2]=28; 346 } 347 n=n+day.mon_maxnum[i]; 348 } 349 n=n+day.mon_maxnum[date.day.month.value]-date.day.value+this.day.value; 350 } 351 }else{ 352 int z,x,c;//中間變數更換日期 353 z=this.day.month.year.value; 354 x=this.day.month.value; 355 c=this.day.value; 356 this.day.month.year.value=date.day.month.year.value; 357 this.day.month.value=date.day.month.value; 358 this.day.value=date.day.value; 359 if(this.day.month.year.value==z){ 360 if(day.month.year.isLeapYear()){ 361 day.mon_maxnum[2]=29; 362 }else{ 363 day.mon_maxnum[2]=28; 364 } 365 if(this.day.value==c){//判斷是否日期相等 366 n=0; 367 }else { 368 for(i=x+1;i<this.day.month.value;i++){ 369 n=n+day.mon_maxnum[i]; 370 } 371 n=n+day.mon_maxnum[x]-c+this.day.value; 372 } 373 }else{ 374 for(i=z+1;i<this.day.month.year.value;i++){ 375 Year flag = new Year(i); 376 if(flag.isLeapYear()){ 377 n=n+k; 378 }else{ 379 n=n+j; 380 } 381 } 382 for(i=x+1;i<=12;i++){ 383 if(day.month.year.isLeapYear()){ 384 day.mon_maxnum[2]=29; 385 }else{ 386 day.mon_maxnum[2]=28; 387 } 388 n=n+day.mon_maxnum[i]; 389 } 390 for(i=1;i<this.day.month.value;i++){ 391 if(day.month.year.isLeapYear()){ 392 day.mon_maxnum[2]=29; 393 }else{ 394 day.mon_maxnum[2]=28; 395 } 396 n=n+day.mon_maxnum[i]; 397 } 398 n=n+day.mon_maxnum[x]-c+this.day.value; 399 }