面向对象的第三次pta作业第三题:日期问题面向对象设计(聚合一)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int year = 0; int month = 0; int day = 0; int choice = input.nextInt(); if (choice == 1) { // test getNextNDays method int m = 0; year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); DateUtil date = new DateUtil(year, month, day); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } m = input.nextInt(); if (m < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print(date.getDay().getMonth().getYear().getValue() + "-" + date.getDay().getMonth().getValue() + "-" + date.getDay().getValue() + " next " + m + " days is:"); System.out.println(date.getNextNDays(m).showDate()); } else if (choice == 2) { // test getPreviousNDays method int n = 0; year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); DateUtil date = new DateUtil(year, month, day); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } n = input.nextInt(); if (n < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print(date.getDay().getMonth().getYear().getValue() + "-" + date.getDay().getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:"); System.out.println(date.getPreviousNDays(n).showDate()); } else if (choice == 3) { // test getDaysofDates method year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); int anotherYear = Integer.parseInt(input.next()); int anotherMonth = Integer.parseInt(input.next()); int anotherDay = Integer.parseInt(input.next()); DateUtil fromDate = new DateUtil(year, month, day); DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { System.out.println("The days between " + fromDate.showDate() + " and " + toDate.showDate() + " are:" + fromDate.getDaysofDates(toDate)); } else { System.out.println("Wrong Format"); System.exit(0); } } else { System.out.println("Wrong Format"); System.exit(0); } } } class DateUtil { Day day; public DateUtil(){ } public DateUtil(int d, int m, int y){ this.day = new Day(d,m,y); } public Day getDay() { return day; } public void setDay(Day d) { this.day = d; } public boolean checkInputValidity(){//检测输入的年、月、日是否合法 if(this.getDay().getMonth().getYear().validate()&&this.getDay().getMonth().validate()&&day.validate()) return true; return false; } public boolean compareDates(DateUtil date){//比较当前日期与date的大小(先后) if (date.getDay().getMonth().getYear().getValue()<this.getDay().getMonth().getYear().getValue()) return false; else if (date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue() &&date.getDay().getMonth().getValue()<this.getDay().getMonth().getValue()) return false; if (date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue() &&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue() &&date.getDay().getValue()<this.getDay().getValue()) return false; return true; } public boolean equalTwoDates(DateUtil date){//判断两个日期是否相等 if (this.getDay().getValue()==date.getDay().getValue() && this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue() && this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue()) return true; return false; } public String showDate(){//以“year-month-day”格式返回日期值 return this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue(); } public DateUtil getNextNDays(int n){//取得year-month-day的下n天日期 int arr[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int year=0, month=0, day=0; int rest = restday(this); if (rest>n) {//本年 year=this.getDay().getMonth().getYear().getValue(); int mday = arr[this.getDay().getMonth().getValue()]; if (this.getDay().getMonth().getYear().isLeapYear()&&this.getDay().getMonth().getValue()==2) { mday++; } mday-=this.getDay().getValue();//本月剩余的日期 if (mday>=n) { //本月 month = this.getDay().getMonth().getValue(); day = n+this.getDay().getValue(); } else{ //其他月 n-=mday; month = this.getDay().getMonth().getValue()+1; int k = month; while(n-arr[k]>0&&k<=12){ n -= arr[k]; month++; k++; } day = n; } } else { n-=rest; year = this.getDay().getMonth().getYear().getValue()+1; int y = 365; if (new Year(year).isLeapYear()) { y++; } while(n-y>0){ n-=y; year++; y=365; if (new Year(year).isLeapYear()) y++; } int k = 1; while(n-arr[k]>0&&k<=12){ n -= arr[k]; k++; } month = k; day = n; } // System.out.println(this.showDate()+" next "+n+" days is:"+year+"-"+month+"-"+day); return new DateUtil(year, month, day); } public int restday(DateUtil d) { int n = 0; int arr[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; for (int i = d.getDay().getMonth().getValue()+1; i <=12; i++) { n+=arr[i]; } n+=arr[d.getDay().getMonth().getValue()]-d.getDay().getValue(); if(d.getDay().getMonth().getYear().isLeapYear()&&d.getDay().getMonth().getValue()<=2) n++; return n; } public DateUtil getPreviousNDays(int n){//取得year-month-day的前n天日期 int arr[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int year=0, month=0, day=0; int rest = 365-restday(this); if (this.getDay().getMonth().getYear().isLeapYear()) { rest++; } if (rest>n) {//本年 year=this.getDay().getMonth().getYear().getValue(); int mday=this.getDay().getValue();//本月剩余的日期 if (mday>n) { //本月 month = this.getDay().getMonth().getValue(); day = mday-n; } else{ //其他月 n-=mday; month = this.getDay().getMonth().getValue()-1; if (month==0) { month = 12; year=this.getDay().getMonth().getYear().getValue()-1; } int k = month; while(n-arr[k]>0&&k>=0){ n -= arr[k]; month--; k--; } day = arr[k]-n; if (new Year(year).isLeapYear()&&month==2) { day++; } } } else { n-=rest; year = this.getDay().getMonth().getYear().getValue()-1; int y = 365; if (new Year(year).isLeapYear()) { y++; } while(n-y>0){ n-=y; year--; y=365; if (new Year(year).isLeapYear()) y++; } int k = 12; while(n-arr[k]>0&&k>=0){ n -= arr[k]; k--; } month = k; day = arr[k]-n; if (new Year(year).isLeapYear()&&month==2) { day++; } } // System.out.println(this.showDate()+" previous "+n+" days is:"+year+"-"+month+"-"+day); return new DateUtil(year, month, day); } public int getDaysofDates(DateUtil date){//求当前日期与date之间相差的天数 DateUtil pred = this; DateUtil nextd = date; if (this.equalTwoDates(date)) { return 0; } else if (!this.compareDates(date)) { pred = date; nextd = this; } int arr[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int i,j,d = 0; for(i=pred.getDay().getMonth().getYear().getValue()+1;i<nextd.getDay().getMonth().getYear().getValue();i++) { d=d+365; if(new Year(i).isLeapYear()) d++; } if (pred.getDay().getMonth().getYear().getValue()!=nextd.getDay().getMonth().getYear().getValue()) { for(j=pred.getDay().getMonth().getValue()+1;j<=12;j++) d=d+arr[j]; d+=arr[pred.getDay().getMonth().getValue()]-pred.getDay().getValue(); for(j=1;j<nextd.getDay().getMonth().getValue();j++) d+=arr[j]; d+=nextd.getDay().getValue(); if(pred.getDay().getMonth().getYear().isLeapYear()&&pred.getDay().getMonth().getValue()<=2) d++; if (nextd.getDay().getMonth().getYear().isLeapYear()&&nextd.getDay().getMonth().getValue()>2) { d++; } } else if(pred.getDay().getMonth().getYear().getValue()==nextd.getDay().getMonth().getYear().getValue()&&pred.getDay().getMonth().getValue()!=nextd.getDay().getMonth().getValue()){ for(j=pred.getDay().getMonth().getValue()+1;j<=nextd.getDay().getMonth().getValue()-1;j++) d+=arr[j]; d+=arr[pred.getDay().getMonth().getValue()]-pred.getDay().getValue(); d+=nextd.getDay().getValue(); if(pred.getDay().getMonth().getYear().isLeapYear()&&pred.getDay().getMonth().getValue()<=2) d++; } else if(pred.getDay().getMonth().getYear().getValue()==nextd.getDay().getMonth().getYear().getValue()&&pred.getDay().getMonth().getValue()==nextd.getDay().getMonth().getValue()){ d=nextd.getDay().getValue()-pred.getDay().getValue(); } return d; } } class Day{ int value; Month month; int mon_maxnum[]= {31,28,31,30,31,30,31,31,30,31,30,31}; public Day() { } public Day(int yearValue,int monthValue,int dayValue) { this.month = new Month(yearValue,monthValue); this.value = dayValue; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public Month getMonth() { return month; } public void setMonth(Month value) { this.month = value; } public void resetMin() { value=1; } public void resetMax() { value=mon_maxnum[month.getValue()-1]; } public boolean validate() { if(this.getMonth().getYear().isLeapYear()) mon_maxnum[1]++; if(1<=value&&mon_maxnum[month.getValue()-1]>=value) return true; return false; } public void dayIncrement() { value++; } public void dayReduction() { value--; } } class Month{ int value; Year year; public Month() { } public Month(int yearValue,int monthValue) { this.year = new Year(yearValue); this.value = monthValue; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public Year getYear() { return year; } public void setYear(Year year) { this.year = year; } public void resetMin() { value=1; } public void resetMax() { value=12; } public boolean validate() { if(1<=value&&12>=value) return true; return false; } public void monthIncrement() { value++; } public void monthReduction() { value--; } } class Year{ int value; public Year() { } public Year(int value) { this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public boolean isLeapYear(){//判断year是否为闰年 boolean y1 = value%4 == 0; boolean y2 = value%100 != 0; boolean y3 = value%400 == 0; if((y1&&y2)||y3) return true; else return false; } public boolean validate() { if(value<=2020&&value>=1820) return true; return false; } public void yearIncrement() { value++; } public void yearReduction() { value--; } }
我的天太复杂了。。。。
我好像有点明白创建类的意义了
但是对于这道题我想说——禁止套娃!