Java
This commit is contained in:
BIN
APL2.1/TP05/Periode/MyDate.class
Normal file
BIN
APL2.1/TP05/Periode/MyDate.class
Normal file
Binary file not shown.
37
APL2.1/TP05/Periode/MyDate.java
Normal file
37
APL2.1/TP05/Periode/MyDate.java
Normal file
@@ -0,0 +1,37 @@
|
||||
public class MyDate {
|
||||
private int year;
|
||||
private int month;
|
||||
private int day;
|
||||
|
||||
private int[] monthDurations = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
|
||||
public String toString() {
|
||||
return String.format("%04d-%02d-%02d", year, month, day);
|
||||
}
|
||||
|
||||
public MyDate tomorrow() {
|
||||
MyDate newDate = new MyDate(this.year, this.month, this.day+1);
|
||||
|
||||
if (newDate.day > monthDurations[newDate.month-1]) {
|
||||
newDate.day = 1;
|
||||
newDate.month += 1;
|
||||
}
|
||||
|
||||
if (newDate.month > 12) {
|
||||
newDate.month = 1;
|
||||
newDate.year += 1;
|
||||
}
|
||||
|
||||
return newDate;
|
||||
}
|
||||
|
||||
public boolean isEqual(MyDate date) {
|
||||
return (this.year == date.year && this.month == date.month && this.day == date.day);
|
||||
}
|
||||
|
||||
public MyDate(int y, int m, int d) {
|
||||
this.year = y;
|
||||
this.month = m;
|
||||
this.day = d;
|
||||
}
|
||||
}
|
||||
BIN
APL2.1/TP05/Periode/MyPeriod.class
Normal file
BIN
APL2.1/TP05/Periode/MyPeriod.class
Normal file
Binary file not shown.
31
APL2.1/TP05/Periode/MyPeriod.java
Normal file
31
APL2.1/TP05/Periode/MyPeriod.java
Normal file
@@ -0,0 +1,31 @@
|
||||
public class MyPeriod {
|
||||
|
||||
private int length;
|
||||
|
||||
public String toString() {
|
||||
return "Periode : " + length + " jour" + (length > 1 ? "s" : "");
|
||||
}
|
||||
|
||||
public void addDay() {
|
||||
this.length += 1;
|
||||
}
|
||||
|
||||
public MyPeriod(MyDate date1, MyDate date2) {
|
||||
if (date1.isEqual(date2)) {
|
||||
this.length = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
//Très dangereux et inefficace, ne faites pas ca svp.
|
||||
while (!date1.isEqual(date2)) {
|
||||
date1 = date1.tomorrow();
|
||||
length++;
|
||||
}
|
||||
}
|
||||
|
||||
public MyPeriod(int length) {
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
BIN
APL2.1/TP05/Periode/Periode.class
Normal file
BIN
APL2.1/TP05/Periode/Periode.class
Normal file
Binary file not shown.
9
APL2.1/TP05/Periode/Periode.java
Normal file
9
APL2.1/TP05/Periode/Periode.java
Normal file
@@ -0,0 +1,9 @@
|
||||
public class Periode {
|
||||
public static void main(String[] args) {
|
||||
MyDate date1 = new MyDate(2002, 05, 29);
|
||||
MyDate date2 = new MyDate(2022, 05, 29);
|
||||
|
||||
MyPeriod p = new MyPeriod(date1, date2);
|
||||
System.out.println(p.toString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user