面向对象的第三次pta作业第四题:日期问题面向对象设计(聚合二)

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        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.showDate() + " 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.showDate() + " 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 {
    Year year;
    Month month;
    Day day;
    int mon_maxnum[] = {31,28,31,30,31,30,31,31,30,31,30,31};
    public DateUtil(){
        
    }
    public DateUtil(int y, int m, int d){
        this.day = new Day(d);
        this.month = new Month(m);
        this.year = new Year(y);
    }
    public Year getYear() {
        return year;
    }
    public void setYear(Year year) {
        this.year = year;
    }
    public Month getMonth() {
        return month;
    }
    public void setMonth(Month month) {
        this.month = month;
    }
    public Day getDay() {
        return day;
    }
    public void setDay(Day day) {
        this.day = day;
    }
    public void setDayMin() {
        this.day.value=1;
    }
    public void setDatMax() {
        if(this.year.isLeapYear())
            mon_maxnum[1]++;
        this.day.value=mon_maxnum[this.month.value-1];
    }
    
    public boolean checkInputValidity(){//检测输入的年、月、日是否合法
        if(this.year.isLeapYear())
            mon_maxnum[1]++;
        if(this.year.validate()&&this.month.validate()&&this.day.value>=1&&this.day.value<=mon_maxnum[this.month.value-1]) 
            return true;
        return false;
    }
    
    public boolean compareDates(DateUtil date){//比较当前日期与date的大小(先后)
        if (date.year.value<this.year.value)
            return false;
        
        else if (date.year.value==this.year.value
                &&date.month.value<this.month.value) 
            return false;
        
        if (date.year.value==this.year.value
                &&date.month.value==this.month.value
                &&date.day.value<this.day.value) 
            return false;
        
        return true;
    }
    
    public boolean equalTwoDates(DateUtil date){//判断两个日期是否相等
        if (this.getDay().getValue()==date.getDay().getValue() 
                && this.getYear().getValue()==date.getYear().getValue() 
                && this.getMonth().getValue()==date.getMonth().getValue()) 
            return true;
        
        return false;
    }
    
    public String showDate(){//以“year-month-day”格式返回日期值
        return this.getYear().getValue()+"-"+this.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.getYear().getValue();
            int mday = arr[this.getMonth().getValue()];
            if (this.getYear().isLeapYear()&&this.getMonth().getValue()==2) {
                mday++;
            }
            mday-=this.getDay().getValue();//本月剩余的日期
            if (mday>=n) {    //本月
                month = this.getMonth().getValue();
                day = n+this.getDay().getValue();
            }
            else{    //其他月
                n-=mday;
                month = this.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.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.getMonth().getValue()+1; i <=12; i++) {
            n+=arr[i];
        }
        n+=arr[d.getMonth().getValue()]-d.getDay().getValue();
        if(d.getYear().isLeapYear()&&d.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.getYear().isLeapYear()) {
            rest++;
        }
        if (rest>n) {//本年
            year=this.getYear().getValue();
            int mday=this.getDay().getValue();//本月剩余的日期
            if (mday>n) {    //本月
                month = this.getMonth().getValue();
                day = mday-n;
            }
            else{    //其他月
                n-=mday;
                month = this.getMonth().getValue()-1;
                if (month==0) {
                    month = 12;
                    year=this.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.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.getYear().getValue()+1;i<nextd.getYear().getValue();i++) {
            d=d+365;
            if(new Year(i).isLeapYear()) 
                d++;
        }
        if (pred.getYear().getValue()!=nextd.getYear().getValue()) {
            for(j=pred.getMonth().getValue()+1;j<=12;j++) 
                d=d+arr[j];
            d+=arr[pred.getMonth().getValue()]-pred.getDay().getValue();
            for(j=1;j<nextd.getMonth().getValue();j++)
                d+=arr[j];
            d+=nextd.getDay().getValue();
            if(pred.getYear().isLeapYear()&&pred.getMonth().getValue()<=2)
                d++;
            if (nextd.getYear().isLeapYear()&&nextd.getMonth().getValue()>2) {
                d++;
            }
        }
        else if(pred.getYear().getValue()==nextd.getYear().getValue()&&pred.getMonth().getValue()!=nextd.getMonth().getValue()){
            for(j=pred.getMonth().getValue()+1;j<=nextd.getMonth().getValue()-1;j++)
                d+=arr[j];
            d+=arr[pred.getMonth().getValue()]-pred.getDay().getValue();
            d+=nextd.getDay().getValue();
            if(pred.getYear().isLeapYear()&&pred.getMonth().getValue()<=2)
                d++;
        }
        else if(pred.getYear().getValue()==nextd.getYear().getValue()&&pred.getMonth().getValue()==nextd.getMonth().getValue()){
            d=nextd.getDay().getValue()-pred.getDay().getValue();
        }
        return d;
    }
    

}
class Day{
    int value;
    public Day() {
        
    }
    public Day(int value) {
        this.value = value;
    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public void dayIncrement() {
        value++;
    }
    public void dayReduction() {
        value--;
    }
}
class Month{
    int value;
    public Month() {
        
    }
    public Month(int value) {
        this.value = value;
    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public void resetMin() {
        value=1;
    }
    public void resetMax() {
        value=12;
    }
    public boolean validate() {
        if(1<=this.value&&12>=this.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(this.value<=2020&&this.value>=1820)
            return true;
        return false;
    }
    public void yearIncrement() {
        value++;
    }
    public void yearReduction() {
        value--;
    }
}

究极版本!!!!!!!!!!!!

但是重复的地方也太多了吧

下次老师好像要讲继承了

会不会方便一点噢

不过从一元二次方程和日期代码的n次改良中

我终于体会到了Java和c的不同了!!!!!!!!!!!!

感谢天感谢地

感谢老师感谢同学感谢自己

猜你喜欢

转载自www.cnblogs.com/axyx/p/12662841.html