# Tage berechnen



## Irreversible (18. Dez 2014)

Guten Tag zusammen,

und zwar besteht meine Aufgabe darin, dass ich aus 3 Klassen und einer Testerklasse ein Datum erstellen soll. Dieses soll dann mittels Copy Constructor kopiert werden. Ich bin eigentlich zu drei vierteln fertig, allerdings verstehe ich nicht ganz, wie man mit dem Copy-Constructor eine Methode aus der gleichen Klasse benutzen kann. Um zu verdeutlichen was ich meine, lade ich mal alle Klassen hoch.

Testerklasse:

```
public class DaysBetweenTester {

    /**
     * Tests the classes.
     * 
     * @param args  not used
     */
    public static void main(String[] args) {
        testYear();
        testMonth();
        testDate();
        testDateDaysBetween();
    }

    
    /**
     * Tests the class Date.
     */
    private static void testDateDaysBetween() {
        System.out.println("--- Test: Date.daysBetween ---");

        Date date1 = new Date(9, 9, 1999);
        Date date2 = new Date(date1);
        testDaysBetween(date1, date2);

        date2 = new Date(10, 9, 1999);
        testDaysBetween(date1, date2);

        date2 = new Date(11, 9, 1999);
        testDaysBetween(date1, date2);

        date2 = new Date(10, 10, 1999);
        testDaysBetween(date1, date2);

        date2 = new Date(7, 10, 1999);
        testDaysBetween(date1, date2);

        date2 = new Date(1, 3, 2000);
        testDaysBetween(date1, date2);

        date2 = new Date(2, 2, 2004);
        testDaysBetween(date1, date2);

        date2 = new Date(7, 7, 1993);
        testDaysBetween(date1, date2);

        date2 = new Date(29, 2, 1993);
        testDaysBetween(date1, date2);

        date2 = new Date(29, 2, 2003);
        testDaysBetween(date1, date2);

        date2 = new Date(29, 2, 2000);
        testDaysBetween(date1, date2);

        testDaysBetween(new Date(12, 12, 1899), new Date(03, 03, 1900));
        testDaysBetween(new Date(12, 12, 1903), new Date(03, 03, 1904));
        testDaysBetween(new Date(12, 12, 1953), new Date(03, 03, 1954));
        testDaysBetween(new Date(12, 12, 1999), new Date(03, 03, 2000));
        testDaysBetween(new Date(12, 12, 2003), new Date(03, 03, 2004));
    }

    
    /**
     * Tests the method daysBetween.
     * 
     * @param date1
     *            one side of the date interval
     * @param date2
     *            the other side of the date interval
     */
    private static void testDaysBetween(Date date1, Date date2) {
        System.out.println(date1.toString() + " to " + date2.toString() + " = "
                + date1.daysBetween(date2) + " day(s)");
        System.out.println(date2.toString() + " to " + date1.toString() + " = "
                + date2.daysBetween(date1) + " day(s)");
    }

    
    /**
     * Tests the class Date.
     */
    private static void testDate() {
        System.out.println("--- Test: class Date ---");

        System.out.println((new Date(6, 6, 2004)).isValidDate());
        System.out.println((new Date(6, 13, 2004)).isValidDate());
        System.out.println((new Date(33, 6, 2004)).isValidDate());
        System.out.println((new Date(31, 4, 2004)).isValidDate());
        System.out.println((new Date(29, 2, 2003)).isValidDate());
        System.out.println((new Date(29, 2, 2004)).isValidDate());

        Date baseDate = new Date(6, 6, 2003);
        Date iterDate = new Date(baseDate);

        System.out.println(baseDate.compareTo(new Date(6, 6, 2003)) == 0);
        System.out.println(baseDate.compareTo(new Date(4, 6, 2003)) > 0);
        System.out.println(baseDate.compareTo(new Date(8, 6, 2003)) < 0);
        System.out.println(baseDate.compareTo(new Date(6, 6, 2002)) > 0);
        System.out.println(baseDate.compareTo(new Date(6, 6, 2004)) < 0);

        for (int year = 2003; year <= 2004; year++) {
            for (int month = 1; month <= 12; month++) {
                for (int day = 1; day <= 31; day++) {
                    iterDate = new Date(day, month, year);
                    if (iterDate.isValidDate()) {
                        System.out.println(iterDate.toString() + "  passed: "
                                + iterDate.passedDaysInYear() + ", remaining: "
                                + iterDate.remainingDaysInYear());
                    }
                }
            }
        }

        for (int year = 2003; year <= 2004; year++) {
            for (int month = 1; month <= 12; month++) {
                for (int day = 1; day <= 31; day++) {
                    iterDate = new Date(day, month, year);
                    if (iterDate.isValidDate()) {
                        if (baseDate.daysBetween(iterDate) < 10) {
                            System.out.println(baseDate.toString() + " to "
                                    + iterDate.toString() + " = "
                                    + baseDate.daysBetween(iterDate) + " day(s)");
                        }
                    }
                }
            }
        }
    }

    
    /**
     * Tests the class Month.
     */
    private static void testMonth() {

        System.out.println("--- Test: class Month ---");

        Month myMonth = new Month(1, new Year(2003));

        for (int year = 2003; year <= 2004; year++) {
            for (int month = 1; month <= 12; month++) {
                System.out.println("Month:" + myMonth.getNumber()
                        + ", Year:" + myMonth.getYear().getNumber()
                        + ", Days: " + myMonth.daysInMonth());
                myMonth.goNext();
            }
        }
    }

    
    /**
     * Tests the class Year.
     */
    private static void testYear() {

        System.out.println("--- Test: class Year ---");

        Year myYear = new Year(1999);

        for (int i = myYear.getNumber(); i < 2010; i++) {
            System.out.println("year " + myYear.getNumber()
                    + ", leap year: " + myYear.isLeapYear()
                    + ", days in year " + myYear.daysInYear());
            myYear.goNext();
        }
    }
}
```

Klasse Year:

```
public class Year {
 
    // Attributes
    
    /** The integer representing the year. */
    private int year;
    /** The boolean value representing the Leap Year. */
    private boolean leapYear;
    
    // Constructor
    
    /**
     * Basic constructor accepting the year as int.
     * 
     * @param year - the integer representing the year. 
     */
    public Year(int year) {
        this.year = year;
    }
    
    // Methods
    
    /**
     * Tests whether the year is a leap year.
     * 
     * @return - true if year is a Leap Year, false otherwise. 
     */
    public boolean isLeapYear() {
        leapYear = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
        return leapYear;
    }
    /**
     * Gets the current year as a number.
     * 
     * @return - the integer representing the year.
     */
    public int getNumber() {
        return this.year;
    }
    /**
     * Calculates the number of days in the year.
     * 
     * @return - the number of days in the year. 
     */
    public int daysInYear() {
        if (leapYear) {
            return 366;
        } else {
            return 365;
        }
    }
    /**
     * Advances to the next year.
     */
    public void goNext() {
        year++;
    }
}
```

Klasse Month:

```
public class Month {
    
    // Attributes
   
    /** the number representing the days in the month. */
    private int monthNum;
    /** the number of the month. */
    private int month;
    /** the internal year object. */
    private Year year;
    /** numerical representation of the month January. */
    static final int JANUARY = 1;
    /** numerical representation of the month December. */
    static final int DECEMBER = 12;
    
    // Constructor
    
    /**
     * Basic constructor accepting the month as int and year as object.
     * 
     * @param month - the number of the month, January=1, ..., December=12.
     * @param year - the object representing the year. 
     */
    public Month(int month, Year year) {
        this.month = month;
        this.year = year;
    }
    
    // Methods
    
    /**
     * Gets the internal object-representation of the year.
     * 
     * @return - the internal Year object. 
     */
    public Year getYear() {
        return year;
    }
    
    /**
     * Calculates the number of days in the month.
     * 
     * @return - the number of days in a month. 
     */
    public int daysInMonth() {
        if (month == 1 || month == 3 || month == 5 || month == 7 
                || month == 8 || month == 10 || month == 12) {
            monthNum = 31;
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            monthNum = 30;
        } else if (year.isLeapYear()) {
            monthNum = 29;
        } else {
            monthNum = 28;
        }
        return monthNum;
    }
    /**
     * Gets the number of the month.
     * 
     * @return - the number of the month, January=1, ..., December=12. 
     */
    public int getNumber() {
        return this.month;
    }
    /**
     * Advances to the next month. If necessary the year is advanced too.
     */
    public void goNext() {
        if (month < 12) {
            month++;
        } else {
            month = 1;
            year.goNext();
        }
    }
}
```

Klasse Date:

```
public class Date {
    
    // Attributes
    
    /** The Month of the date. */
    private Month dateMonth;
    /** The number of the day. */
    private int dayNum;
    /** the number of the day within the month. */
    private int day;
    /** the number of the month within the year. */
    private int month;
    /** the number of the year. */
    private int year;
    
    
    // Constructor.
    
    /**
     * Copy constructor accepting a Date.
     * 
     * @param date - the date to be used for initialization. 
     */
    public Date(Date date) {
        this (date.day, date.month, date.year);
    }
    /**
     * Basic constructor accepting the day, month and year as int.
     * 
     * @param day - the number of the day within the month.
     * @param month - the number of the month, January=1, ..., December=12.
     * @param year - the integer representing the year. 
     */
    public Date(int day, int month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
    }
    
    // Methods
    
    /**
     * Gets the number of the day.
     * 
     * @return - the number of the day within the month. 
     */
    public int getNumber() {
        return dayNum;
    }
    /**
     * Gets the internal object-representation of the month.
     * 
     * @return - the internal Month object.
     */
    public Month getMonth() {
        return dateMonth;
    }
    /** Tests whether current date is a valid date.
     * 
     * @return - true if date is valid, false otherwise.
     */
    public boolean isValidDate() {
        Year currentYear1 = new Year(year);
        int daysOfCurrentYear = currentYear1.daysInYear();
        Year leapYear = new Year(year);
        boolean isLeapYear = leapYear.isLeapYear();
        
        if (isLeapYear == true && daysOfCurrentYear == 365 && day == 29
                && month == 2 || month > 12 || month <= 0 || day > 31
                || day <= 0) {
            System.out.println("The Date: " + this.day + "." + this.month + "."
            + this.year + " is invalid!");
            return false;
        } else {
            System.out.println("The Date: " + this.day + "." + this.month + "."
            + this.year + " is valid!");
            return true;
        }
    }
    /**
     * Calculates the number of days passed in the year up to, but not 
     * including, the current date.
     * 
     * @return - the number of days in the year so far and 0 for invalid dates. 
     */
    public int passedDaysInYear() {
        Year currentYear = new Year(year);
        int passedDays = 0;
        int currentDay = day;
        int currentMonthNumber = month;
        int daysOfCurrentYear = currentYear.daysInYear();
        
        if (daysOfCurrentYear == 365) {
            if (currentMonthNumber == 1) {
                passedDays = currentDay;
            } else if (currentMonthNumber == 2) {
                passedDays = currentDay + 31;
            } else if (currentMonthNumber == 3) {
                passedDays = currentDay + 59;
            } else if (currentMonthNumber == 4) {
                passedDays = currentDay + 89;
            } else if (currentMonthNumber == 5) {
                passedDays = currentDay + 120;
            } else if (currentMonthNumber == 6) {
                passedDays = currentDay + 150;
            } else if (currentMonthNumber == 7) {
                passedDays = currentDay + 181;
            } else if (currentMonthNumber == 8) {
                passedDays = currentDay + 212;
            } else if (currentMonthNumber == 9) {
                passedDays = currentDay + 242;
            } else if (currentMonthNumber == 10) {
                passedDays = currentDay + 273;
            } else if (currentMonthNumber == 11) {
                passedDays = currentDay + 303;
            } else if (currentMonthNumber == 12) {
                passedDays = currentDay + 334;
            }
        } else if (daysOfCurrentYear == 366) {
            if (currentMonthNumber == 1) {
                passedDays = currentDay;
            } else if (currentMonthNumber == 2) {
                passedDays = currentDay + 31;
            } else if (currentMonthNumber == 3) {
                passedDays = currentDay + 60;
            } else if (currentMonthNumber == 4) {
                passedDays = currentDay + 90;
            } else if (currentMonthNumber == 5) {
                passedDays = currentDay + 121;
            } else if (currentMonthNumber == 6) {
                passedDays = currentDay + 151;
            } else if (currentMonthNumber == 7) {
                passedDays = currentDay + 182;
            } else if (currentMonthNumber == 8) {
                passedDays = currentDay + 213;
            } else if (currentMonthNumber == 9) {
                passedDays = currentDay + 243;
            } else if (currentMonthNumber == 10) {
                passedDays = currentDay + 274;
            } else if (currentMonthNumber == 11) {
                passedDays = currentDay + 304;
            } else if (currentMonthNumber == 12) {
                passedDays = currentDay + 335;
            }
        }
        return passedDays;
    }
    /**
     * Calculates the number of days remaining in the year not including the 
     * current date.
     * 
     * @return - the number of days in the year still to go and 0 for 
     * invalid dates.
     */
    public int remainingDaysInYear() {
        int remainingDays;
        int passedDays;
        passedDays = passedDaysInYear();
        Year currentYear = new Year(year);
        int daysOfCurrentYear = currentYear.daysInYear();
        remainingDays = daysOfCurrentYear - passedDays;
        return remainingDays;
    }
    /**
     * Compares the date with the argument to determine the order of the 
     * two dates.
     * 
     * @param date - the date to compare to.
     * @return - the value 0 if the argument is the same date; 
     * a value less than 0 if the argument is a later date; and a value 
     * greater than 0 if the argument is an earlier date.
     */
    public int compareTo(Date date) {
        if (date.day == day && date.month == month && date.year == year) {
            return 0;
        } else if (date.day > day && date.month >= month && date.year >= year
                || date.day >= day && date.month > month && date.year >= year
                || date.day >= day && date.month >= month && date.year > year) {
            return -1;
        } else {
            return 1;
        }
    }
    /**
     * Tests whether the dates are equal.
     * 
     * @param date - the date to compare to.
     * @return - true if the dates are equal, false otherwise.
     */
    public boolean isEqual(Date date) {
        return date.day == day && date.month == month && date.year == year;
    }
    /**
     * Calculates the number of days between the date and the argument date.
     * 
     * @param date - the date to compare to.
     * @return - the number of days between both dates or 0 if either one of 
     * the two dates is invalid.
     */
    public int daysBetween(Date date) {
    }
    /**
     * Advances to the next date. If necessary month & year are advanced too.
     */
    public void goNext() {
    }
    /**
     * Returns a String representation of the current date.
     * @return - a String representation of the date in the format: yyyy-mm-dd.
     */
    @Override
    public java.lang.String toString() {
        return "Date: " + this.year + "." + this.month + "." + this.day;
    }
}
```

Die Klasse Date ist dabei mein Problemkind. Gerade die Methode daysBetween() bringt mich zum verzweifeln. Ich würde halt gerne mit dem Copy-Constructor einmal die methoden passedDaysInYear() und remainingDaysInYear() ansprechen und die methode daysInYear() aus der Klasse Year. Aber ich hab absolut keine Idee wie das genau gehen soll. Hab mich schon tot gegoogelt^^ Achja und bitte die anderen Methoden in der Klasse Date nicht so genau betrachten^^ is nicht unbedingt die eleganteste Lösung, aber sowas wie die Calender funktion sollen wir halt auch nicht benutzen.

Mit freundlichen Grüßen


----------

