mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-11 20:55:40 -08:00
Remove redundant Card record code
Java Records automatically acquire an implementation of equals and hashCode that accounts for their components. They also have read accessors for their components (card.suit() to get the suit).
This commit is contained in:
@@ -24,14 +24,6 @@ record Card(int value, Suit suit) {
|
||||
}
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public Suit getSuit() {
|
||||
return this.suit;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder result = new StringBuilder(2);
|
||||
if(value == 1) {
|
||||
@@ -64,28 +56,4 @@ record Card(int value, Suit suit) {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
// Overriding 'equals' and 'hashCode' (below) make your class work correctly
|
||||
// with all sorts of methods in the Java API that need to determine the uniqueness
|
||||
// of an instance (like a Set).
|
||||
if(obj.getClass() != Card.class) {
|
||||
return false;
|
||||
}
|
||||
Card other = (Card) obj;
|
||||
return this.getSuit() == other.getSuit() && this.getValue() == other.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
// This is a fairly standard hashCode implementation for a data object.
|
||||
// The details are beyond the scope of this comment, but most IDEs can generate
|
||||
// this for you.
|
||||
|
||||
// Note that it's a best practice to implement hashCode whenever you implement equals and vice versa.
|
||||
int hash = 7;
|
||||
hash = 31 * hash + (int) value;
|
||||
hash = 31 * hash + suit.hashCode();
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user