public void setValue(int value) { if (value < 1 || value > 13) { throw new IllegalArgumentException("Value " + value + " out of valid 1 - 13 range."); } this.value = value; } public void setSuit(Suit suit) { this.suit = suit; }
Set<PlayingCard> set = new HashSet<PlayingCard>();
PlayingCard card = new PlayingCard(2, PlayingCard.Suit.C);
for (int i = 0; i < 5; i++) {
//add 5 cards to set
set.add(card);
card.setValue(card.getValue() + 1);
}
System.out.println(set); // [7C, 7C, 7C, 7C, 7C] (even w/ correct hashCode & equals)
// as a Set Set<PlayingCard> set = new HashSet<PlayingCard>(); PlayingCard card = new PlayingCard(2, PlayingCard.Suit.S); set.add(card); PlayingCard fourC = new PlayingCard(4, PlayingCard.Suit.C); set.add(fourC); set.add(new PlayingCard(11, PlayingCard.Suit.H)); System.out.println(set); // [JH, 2S, 4C] card.setValue(4); System.out.println(set); // [JH, 4S, 4C] System.out.println(card); // 4S System.out.println(set.contains(card)); // false
/** * Stores mutable PlayingCards where they cannot be affected by others! * Once you put a card into a CardProtector, you can't change its value. * Protect all of your cards today! */ public class CardProtector { private PlayingCard storage; //encapsulated! public CardProtector(PlayingCard card) { this.storage = card; } public PlayingCard getCard() { return this.storage; } @Override public String toString() { return "{" + this.storage + "}"; } }
PlayingCard card = new PlayingCard(2, PlayingCard.Suit.S); CardProtector cp = new CardProtector(card); System.out.println(cp); // {2S} card.setValue(11); System.out.println(cp); // {JS} !!
/**
* Stores mutable PlayingCards where they cannot be affected by others!
* Once you put a card into a CardProtector, you can't change its value.
* Protect all of your cards today!
*/
public class CardProtector {
private PlayingCard storage; //encapsulated!
public CardProtector(PlayingCard card) {
this.storage = new PlayingCard(card.getValue(), card.getSuit()); //make my own copy
}
public PlayingCard getCard() {
return this.storage;
}
@Override public String toString() {
return "{" + this.storage + "}";
}
}
PlayingCard card = new PlayingCard(2, PlayingCard.Suit.S);
CardProtector cp = new CardProtector(card);
System.out.println(cp); // {2S}
card.setValue(11);
System.out.println(cp); // {2S} (ha! foiled)
PlayingCard letMeSee = cp.getCard();
letMeSee.setValue(11);
System.out.println(cp); // {JS} (doh!)
/**
* Stores mutable PlayingCards where they cannot be affected by others!
* Once you put a card into a CardProtector, you can't change its value.
* Protect all of your cards today!
*/
public class CardProtector {
private PlayingCard storage; //encapsulated!
public CardProtector(PlayingCard card) {
this.storage = new PlayingCard(card.getValue(), card.getSuit()); //make my own copy
}
public PlayingCard getCard() {
return new PlayingCard(this.storage.getValue(), this.storage.getSuit());
}
@Override public String toString() {
return "{" + this.storage + "}";
}
}
PlayingCard card = new PlayingCard(2, PlayingCard.Suit.S);
CardProtector cp = new CardProtector(card);
System.out.println(cp); // {2S}
card.setValue(11);
System.out.println(cp); // {2S} (ha! foiled)
PlayingCard letMeSee = cp.getCard();
letMeSee.setValue(11);
System.out.println(cp); // {2S} (ha ha!)