Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Card implements Comparable<Object> {
- private Suit suit;
- private Rank rank;
- public Card(Suit suit, Rank rank) {
- this.suit = suit;
- this.rank = rank;
- }
- public Suit getSuit() {
- return suit;
- }
- public void setSuit(Suit suit) {
- this.suit = suit;
- }
- public Rank getRank() {
- return rank;
- }
- public void setRank(Rank rank) {
- this.rank = rank;
- }
- @Override
- public String toString() {
- return getSuit() + " " + getRank();
- }
- @Override
- public int compareTo(Object otherObject) {
- Card other = (Card) otherObject;
- if (rank.TWO < other.rank.THREE) {
- return -1;
- }
- if (rank.TWO > other.rank.THREE) {
- return 1;
- }
- return 0;
- }
- }
- public class Hand {
- private ArrayList<Card> cards = new ArrayList<>();
- public boolean add(Card c) {
- if (linearSearchList(cards, c) == false) { // if the card do NOT exist in the arraylist
- cards.add(c);
- return true;
- }
- return false;
- }
- public boolean remove(Card c) {
- if (linearSearchList(cards, c) == true) { // if the card already in the arraylist
- cards.remove(c);
- return true;
- }
- return false;
- }
- public ArrayList<Card> getCards() {
- return new ArrayList<>(cards);
- }
- public static boolean linearSearchList(ArrayList<Card> cardsList, Card c) {
- boolean found = false;
- int i = 0;
- while (!found && i < cardsList.size()) {
- if (cardsList.get(i).equals(c)) {
- found = true;
- } else {
- i++;
- }
- }
- if (found) {
- return true;
- } else {
- return false;
- }
- }
- @Override
- public String toString() {
- Collections.sort(cards);
- return cards.toString();
- }
- }
- public enum Suit {
- DIAMOND, CLUB, HEART, SPADE;
- }
- public enum Rank {
- TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE;
- }
- public class MainApp {
- public static void main(String[] args) {
- Card c1 = new Card(Suit.DIAMOND, Rank.QUEEN);
- Card c2 = new Card(Suit.HEART, Rank.EIGHT);
- Card c3 = new Card(Suit.DIAMOND, Rank.KING);
- Card c4 = new Card(Suit.CLUB, Rank.TWO);
- Card c5 = new Card(Suit.SPADE, Rank.ACE);
- Hand hand1 = new Hand();
- hand1.add(c1);
- hand1.add(c2);
- hand1.add(c3);
- hand1.add(c4);
- hand1.add(c5);
- Collections.sort(hand1.getCards());
- for (Card card : hand1.getCards()) {
- System.out.println(card);
- }
- }
- }
- public class Card implements Comparable<Card> {
- @Override
- public int compareTo(Card otherCard) {
- if (this.rank == otherCard.rank) {
- return this.suite.compareTo(otherCard.suite);
- }
- return this.rank.compareTo(otherCard.rank);
- }
Add Comment
Please, Sign In to add comment