Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • main
1 result

Target

Select target project
  • ethan.robert.etu/dev-oo
1 result
Select Git revision
  • main
1 result
Show changes
Commits on Source (3)
File moved
File moved
File moved
File moved
File moved
File moved
File moved
package tp03;
public class Card {
private Color color;
private Rank rank;
public Card (Color color, Rank rank) {
this.color = color;
this.rank = rank;
}
public Card (String color, String rank) {
for (int i = 0; i < Color.values().length; i++) {
if (Color.values()[i].equals(color)) {
this.color = Color.valueOf(color);
}
}
for (int i = 0; i < Rank.values().length; i++) {
if (Rank.values()[i].equals(color)) {
this.rank = Rank.valueOf(rank);
}
}
}
public Color getColor() {
return this.color;
}
public Rank getRank() {
return this.rank;
}
public boolean equals(Card other) {
if (this == other) {
return true;
}
if (other == null) {
return false;
}
if (this.color.equals(other.color) && this.rank.equals(other.rank)) {
return true;
}
return false;
}
public String 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 {
CLUB, DIAMOND, HEART, SPADE;
}
\ No newline at end of file
package tp03;
public enum Rank {
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