Skip to content
Snippets Groups Projects
Commit be3ce108 authored by Ethan Robert's avatar Ethan Robert
Browse files

tpOO-03::exo-static

parent c31dbde0
No related branches found
No related tags found
No related merge requests found
package tp03;
public class Card { public class Card {
private Color color; private Color color;
...@@ -47,4 +49,32 @@ public class Card { ...@@ -47,4 +49,32 @@ public class Card {
return this.rank.toString() + " of " + this.color.toString(); return this.rank.toString() + " of " + this.color.toString();
} }
public int compareColor(Card other) {
int indexOfSelf = 0;
int indexOfOther = 0;
for (int i = 0; i < Color.values().length; i++) {
if (Color.values()[i].equals(this.color)) {
indexOfSelf = i;
}
if (Color.values()[i].equals(other.color)) {
indexOfOther = i;
}
}
return indexOfSelf - indexOfOther;
}
public int compareRank(Card other) {
int indexOfSelf = 0;
int indexOfOther = 0;
for (int i = 0; i < Rank.values().length; i++) {
if (Rank.values()[i].equals(this.rank)) {
indexOfSelf = i;
}
if (Rank.values()[i].equals(other.rank)) {
indexOfOther = i;
}
}
return indexOfSelf - indexOfOther;
}
} }
\ No newline at end of file
package tp03;
public enum Color { public enum Color {
CLUB, DIAMOND, HEART, SPADE; CLUB, DIAMOND, HEART, SPADE;
} }
\ No newline at end of file
package tp03;
public enum Rank { public enum Rank {
SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE; SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE;
} }
\ No newline at end of file
package tp03;
public class UseCard {
public static void main(String[] args) {
Card c1 = new Card(Color.HEART, Rank.TEN);
Card c2 = new Card(Color.HEART, Rank.JACK);
Card c3 = new Card(Color.DIAMOND, Rank.TEN);
Card c4 = new Card(Color.CLUB, Rank.SEVEN);
Card c5 = new Card(Color.SPADE, null);
Card c6 = new Card(null, Rank.JACK);
Card c7 = new Card(Color.HEART, Rank.TEN);
// equals scenario
if(!c1.equals(c1) || c1.equals(null) || c1.equals(c2) || c1.equals(c3) || c1.equals(c4) || c1.equals(c5) || c1.equals(c6) || !c1.equals(c7))
System.out.println("equals FAILED");
// compareColor scenario
else if(c1.compareColor(c1) != 0 || c1.compareColor(c2) != 0 || c1.compareColor(c3) <= 0 || c1.compareColor(c4) <= 0 || c1.compareColor(c5) >= 0 || c1.compareColor(c7) != 0)
System.out.println("compareColor FAILED");
// compareRank scenario
else if(c1.compareRank(c1) != 0 || c1.compareRank(c2) >= 0 || c1.compareRank(c3) != 0 || c1.compareRank(c4) <= 0 || c1.compareRank(c6) >= 0 || c1.compareRank(c7) != 0)
System.out.println("compareRank FAILED");
else System.out.println("Tests OK");
}
}
import java.time.LocalDate;
import java.time.chrono.ChronoLocalDate;
import java.util.Random;
import java.time.Period;
public class UseLocalDate {
public static boolean inArray(int[] array, int value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == value) {
return true;
}
}
return false;
}
public static void main(String[] args) {
LocalDate d1 = LocalDate.now();
LocalDate d2 = LocalDate.of(2006, 11, 12);
// Random number manipulation for d3 date
Random random = new Random();
LocalDate d3;
int year = LocalDate.now().getYear() - random.nextInt(30) - 1;
int month = random.nextInt(12) + 1;
int dayBound;
if (inArray(new int[]{1, 3, 5, 7, 8, 10, 12}, month)) {
dayBound = 31;
} else if (month == 2) {
if (year % 4 == 0 && year % 100 != 0) {
dayBound = 29;
} else {
dayBound = 28;
}
} else {
dayBound = 30;
}
d3 = LocalDate.of(year, month, random.nextInt(dayBound) + 1);
System.out.println("d3 is " + d3);
if (d2.compareTo(d3) < 0) {
System.out.println("Older date is " + d2);
} else {
System.out.println("Older date is " + d3);
}
// d4 is LocalDate 7 days after today's date
LocalDate d4 = d1.plusDays(7);
System.out.println("In 1 week we@'ll be on " + d4);
Period periode = d2.until(ChronoLocalDate.from(d1));
System.out.println(periode.getYears() + " ans, " + periode.getMonths() + " mois, et " + periode.getDays() + " jours entre " + d1 + " et " + d2);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment