Card.java

by shwcsmack on December 30th, 2009
No notes
Syntax: Java
Show lines - Hide lines - Show in textbox - Download
  1.  
  2. package pokerapp;
  3.  
  4. /**
  5.  *A class that represents a single playing card.
  6.  * @author Shayne Stewart
  7.  * @version 1
  8.  */
  9. public class Card {
  10.  
  11. private String color;
  12. private String suite;
  13. private int value;
  14.  
  15.  
  16. Card(){
  17. value = 0;
  18. suite = "";
  19. color = "";
  20. }
  21.  
  22. Card(String cardin){
  23. String[] parts = cardin.split(" ");
  24. String valuein = parts[0];
  25. value = this.convertValue(valuein);
  26. String suitein = parts[1];
  27. suite = this.convertSuitetoBig(suitein);
  28. color = this.setColor(suite);
  29. }
  30.  
  31.  
  32. public int getValue(){
  33. return value;
  34. }
  35.  
  36. public String getSuite(){
  37. return suite;
  38. }
  39.  
  40. public String getColor(){
  41. return color;
  42. }
  43.  
  44. private int convertValue(String valuein){
  45. int valueout = 0;
  46. if (valuein.equalsIgnoreCase("A")){
  47. valueout = 14;
  48. } else if (valuein.equalsIgnoreCase("K")){
  49. valueout = 13;
  50. } else if (valuein.equalsIgnoreCase("Q")){
  51. valueout = 12;
  52. } else if (valuein.equalsIgnoreCase("J")){
  53. valueout = 11;
  54. } else if (valuein.equalsIgnoreCase("T")){
  55. valueout = 10;
  56. } else {
  57. valueout = Integer.parseInt(valuein);
  58. }
  59. return valueout;
  60. }
  61.  
  62. private String convertValue(int valuein){
  63. String valueout = "";
  64. if (valuein == 14){
  65. valueout = "A";
  66. } else if (valuein == 13){
  67. valueout = "K";
  68. } else if (valuein == 12){
  69. valueout = "Q";
  70. } else if (valuein == 11){
  71. valueout = "J";
  72. } else if (valuein == 10){
  73. valueout = "10";
  74. } else {
  75. valueout = String.valueOf(valuein);
  76. }
  77. return valueout;
  78. }
  79.  
  80. private String convertSuitetoBig(String suitein){
  81. String suiteout = "";
  82. if (suitein.equalsIgnoreCase("h")){
  83. suiteout = "Hearts";
  84. } else if (suitein.equalsIgnoreCase("d")) {
  85. suiteout = "Diamonds";
  86. } else if (suitein.equalsIgnoreCase("s")) {
  87. suiteout = "Spades";
  88. } else if (suitein.equalsIgnoreCase("c")) {
  89. suiteout = "Clubs";
  90. } else {
  91.  
  92. }
  93. return suiteout;
  94. }
  95.  
  96. private String convertSuitetoSmall(String suitein){
  97. String suiteout = "";
  98. if (suitein.equalsIgnoreCase("Hearts")){
  99. suiteout = "h";
  100. } else if (suitein.equalsIgnoreCase("Diamonds")) {
  101. suiteout = "d";
  102. } else if (suitein.equalsIgnoreCase("Spades")) {
  103. suiteout = "s";
  104. } else if (suitein.equalsIgnoreCase("Clubs")) {
  105. suiteout = "c";
  106. } else {
  107.  
  108. }
  109. return suiteout;
  110. }
  111.  
  112. private String setColor(String suitein){
  113. String colorout = "";
  114. if (suitein.equalsIgnoreCase("Hearts") || suitein.equalsIgnoreCase("Diamonds")){
  115. colorout = "Red";
  116. } else if (suitein.equalsIgnoreCase("Spades") || suitein.equalsIgnoreCase("Clubs")) {
  117. colorout = "Black";
  118. } else {
  119.  
  120. }
  121. return colorout;
  122. }
  123.  
  124. public boolean isBetterThen(Card cardin){
  125. boolean out = false;
  126. int cinv = cardin.getValue();
  127. if (cinv < value) {
  128. out = true;
  129. }
  130. return out;
  131. }
  132.  
  133. public boolean isWorseThen(Card cardin){
  134. boolean out = false;
  135. int cinv = cardin.getValue();
  136. if (cinv > value) {
  137. out = true;
  138. }
  139. return out;
  140. }
  141.  
  142. public boolean isEqualTo(Card cardin){
  143. boolean out = false;
  144. int cinv = cardin.getValue();
  145. if (cinv == value) {
  146. out = true;
  147. }
  148. return out;
  149. }
  150.  
  151. @Override
  152. public String toString(){
  153. String valueout = this.convertValue(value);
  154. String suiteout = this.convertSuitetoSmall(suite);
  155. String out = valueout + suiteout;
  156. return out;
  157. }
  158.  
  159. public void printCard(){
  160. String valueout = this.convertValue(value);
  161. System.out.println(valueout + " of " + suite);
  162. }
  163. }
  164.  

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS