forked from prmr/DesignBook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard2.java
More file actions
144 lines (136 loc) · 2.81 KB
/
Card2.java
File metadata and controls
144 lines (136 loc) · 2.81 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
133
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************************************
* 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 chapter2;
/**
* Implementation of a playing card. This class yields immutable objects.
* This version is a facetious implementation with 6 Boolean values.
*/
public class Card2
{
private boolean aIsRed = false;
private boolean aIsHighSuit = false;
private boolean aRank1 = false;
private boolean aRank2 = false;
private boolean aRank3 = false;
private boolean aRank4 = false;
/**
* How to properly write automated tests such as this one
* is the topic of Chapter 5.
*/
public static void main(String[] args)
{
for( Rank rank : Rank.values() )
{
for( Suit suit : Suit.values() )
{
Card2 card = new Card2(rank, suit);
assert card.getRank() == rank;
assert card.getSuit() == suit;
System.out.println(card);
}
}
}
/**
* Creates a new card object.
*
* @param pRank The rank of the card.
* @param pSuit The suit of the card.
* @pre pRank != null
* @pre pSuit != null
*/
public Card2(Rank pRank, Suit pSuit)
{
assert pRank != null && pSuit != null;
fromSuit(pSuit);
fromRank(pRank);
}
private void fromSuit(Suit pSuit)
{
if( pSuit == Suit.HEARTS || pSuit == Suit.DIAMONDS )
{
aIsRed = true;
}
if( pSuit == Suit.HEARTS || pSuit == Suit.SPADES )
{
aIsHighSuit = true;
}
}
private void fromRank(Rank pRank)
{
int value = pRank.ordinal();
aRank1 = value % 2 == 1;
value /= 2;
aRank2 = value % 2 == 1;
value /= 2;
aRank3 = value % 2 == 1;
value /= 2;
aRank4 = value % 2 == 1;
value /= 2;
}
/**
* @return The rank of the card.
*/
public Rank getRank()
{
int value = 0;
if( aRank4 == true )
{
value += 8;
}
if( aRank3 == true )
{
value += 4;
}
if( aRank2 == true )
{
value += 2;
}
if( aRank1 == true )
{
value += 1;
}
return Rank.values()[value];
}
/**
* @return The suit of the card.
*/
public Suit getSuit()
{
if( aIsRed )
{
if( aIsHighSuit )
{
return Suit.HEARTS;
}
else
{
return Suit.DIAMONDS;
}
}
else
{
if( aIsHighSuit )
{
return Suit.SPADES;
}
else
{
return Suit.CLUBS;
}
}
}
@Override
public String toString()
{
return String.format("%s of %s", getRank(), getSuit());
}
}