001package serialization.fixedJavaSoftExample; 002import java.io.*; 003 004 005public class Card2 implements Serializable{ 006 private static final long serialVersionUID = 2008L; 007 private int suit = UNASSIGNED; 008 private int number = UNASSIGNED; 009 010 public final static int UNASSIGNED = -1; 011 012 public final static int DIAMONDS = 1; 013 public final static int CLUBS = 2; 014 public final static int HEARTS = 3; 015 public final static int SPADES = 4; 016 017 public final static int ACE = 1; 018 public final static int KING = 13; 019 020 public Card2(int number, int suit) { 021 if (isValidNumber(number)) { 022 this.number = number; 023 } else { 024 //Error 025 } 026 027 if (isValidSuit(suit)) { 028 this.suit = suit; 029 } else { 030 //Error 031 } 032 } 033 034 public int getSuit() { 035 return suit; 036 } 037 038 public int getNumber() { 039 return number; 040 } 041 042 public static boolean isValidNumber(int number) { 043 if (number >= ACE && number <= KING) { 044 return true; 045 } else { 046 return false; 047 } 048 } 049 public static boolean isValidSuit(int suit) { 050 if (suit >= DIAMONDS && suit <= SPADES) { 051 return true; 052 } else { 053 return false; 054 } 055 } 056 057 public boolean equals(Object obj) { 058 if (obj instanceof Card2) { 059 Card2 o = (Card2) obj; 060 if (o.getNumber() == this.number && o.getSuit() == this.suit) { 061 return true; 062 } else { 063 return false; 064 } 065 } else { 066 return false; 067 } 068 } 069 public int hashCode() { 070 return number * suit; 071 } 072 public String toString() { 073 return numberToString(this.number) + " of " 074 + suitToString(this.suit); 075 } 076 077 public static String numberToString(int number) { 078 String result = ""; 079 switch (number) { 080 case ACE: result = "Ace"; break; 081 case 2: result = "Two"; break; 082 case 3: result = "Three"; break; 083 case 4: result = "Four"; break; 084 case 5: result = "Five"; break; 085 case 6: result = "Six"; break; 086 case 7: result = "Seven"; break; 087 case 8: result = "Eight"; break; 088 case 9: result = "Nine"; break; 089 case 10: result = "Ten"; break; 090 case 11: result = "Jack"; break; 091 case 12: result = "Queen"; break; 092 case KING: result = "King"; break; 093 case UNASSIGNED: result = "Invalid Number"; break; 094 } 095 return result; 096 } 097 098 public static String suitToString(int suit) { 099 String result = ""; 100 switch (suit) { 101 case DIAMONDS: result = "Diamonds"; break; 102 case CLUBS: result = "Clubs"; break; 103 case HEARTS: result = "Hearts"; break; 104 case SPADES: result = "Spades"; break; 105 case UNASSIGNED: result = "Invalid Suit"; break; 106 } 107 return result; 108 } 109 private void writeObject(ObjectOutputStream s) throws IOException { 110 // s.defaultWriteObject(); 111 System.out.println("customized write"); 112 s.writeInt(suit); 113 s.writeInt(number); 114 } 115 116 private void readObject(ObjectInputStream s) throws IOException { 117 // try{ 118 // s.defaultReadObject(); 119 // System.out.println("customized read"); 120 // } catch (ClassNotFoundException e) {throw new IOException(); } 121 System.out.println("customized write"); 122 suit = s.readInt(); 123 number = s.readInt(); 124 } 125 126 127 128} 129 130 131