forked from prmr/DesignBook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedHand.java
More file actions
132 lines (120 loc) · 3.09 KB
/
Copy pathSortedHand.java
File metadata and controls
132 lines (120 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*******************************************************************************
* Companion code for the book "Introduction to Software Design with Java"
* by Martin P. Robillard.
*
* Copyright (C) 2019 by Martin P. Robillard
*
* This code is licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* See http://creativecommons.org/licenses/by-nc-nd/4.0/
*******************************************************************************/
package chapter3;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
/**
* A collection of cards in a player's hand. Basic version for Exercise 1.
*/
public class SortedHand implements Iterable<Card>, Comparable<SortedHand>
{
private final List<Card> aCards = new ArrayList<>();
private Comparator<Card> aComparator = Card.createByRankComparator();
private final int aMaxCards;
/**
* Creates a new, empty hand, which can hold
* a maximum of pMaxCards.
*
* @param pMaxCards The maximum number of cards allowed in this hand.
* @pre pMaxCards > 0;
*/
public SortedHand(int pMaxCards)
{
assert pMaxCards > 0;
aMaxCards = pMaxCards;
}
/**
* Creates a new, empty hand, which can hold
* a maximum of pMaxCards. Allows specifying a sorting order
* other than the default (by rank) order.
*
* @param pMaxCards The maximum number of cards allowed in this hand.
* @pre pMaxCards > 0;
* @pre pOrder!=null
*/
public SortedHand(int pMaxCards, Comparator<Card> pOrder)
{
assert pMaxCards > 0 && pOrder != null;
aMaxCards = pMaxCards;
aComparator = pOrder;
}
/**
* Add pCards to the hand.
* @param pCard The card to add.
* @pre !isFull()
* @pre pCard != null;
*/
public void add(Card pCard)
{
assert pCard != null;
assert !isFull();
aCards.add(pCard);
aCards.sort(aComparator);
}
/**
* @return True if the number of cards in the hand
* is the maximum number of cards allowable, as specified
* in the constructor.
*/
public boolean isFull()
{
return aCards.size() == aMaxCards;
}
/**
* @return True if there are no cards in this hand.
*/
public boolean isEmpty()
{
return aCards.size() == 0;
}
/**
* Removes pCards if it is in the hand. If it is not in the
* hand, does nothing.
*
* @param pCard The card to remove.
* @pre pCards != null;
*/
public void remove(Card pCard)
{
assert pCard != null;
aCards.remove(pCard);
}
/**
* @param pCard A card to check for containment.
* @return True if pCard is a card in this hand.
* @pre pCard != null
*/
public boolean contains(Card pCard)
{
assert pCard != null;
return aCards.contains(pCard);
}
@Override
public Iterator<Card> iterator()
{
return aCards.iterator();
}
@Override
public int compareTo(SortedHand pHand)
{
return aCards.size() - pHand.aCards.size();
}
/**
* @return The number of cards currently in the hand.
*/
public int size()
{
return aCards.size();
}
}