001package algs34;
002import stdlib.*;
003import java.util.HashSet;
004/* ***********************************************************************
005 *  Compilation:  javac PhoneNumber.java
006 *  Execution:    java PhoneNumber
007 *  Dependencies:
008 *
009 *  Immutable data type for US phone numbers.
010 *
011 *************************************************************************/
012
013public final class XPhoneNumber {
014        private final int area;   // area code (3 digits)
015        private final int exch;   // exchange  (3 digits)
016        private final int ext;    // extension (4 digits)
017
018        public XPhoneNumber(int area, int exch, int ext) {
019                this.area = area;
020                this.exch = exch;
021                this.ext  = ext;
022        }
023
024        // how you're supposed to implement equals
025        public boolean equals(Object y) {
026                if (y == this) return true;
027                if (y == null) return false;
028                if (y.getClass() != this.getClass()) return false;
029                XPhoneNumber that = (XPhoneNumber) y;
030                if (!((this.area == that.area) && (this.exch == that.exch) && (this.ext == that.ext))) return false;
031                return true;
032        }
033
034        // satisfies the hashCode contract
035        public int hashCode() {
036                int h = 17;
037                h = ext + 31 * h;
038                h = exch + 31 * h;
039                h = area + 31 * h;
040                return h;
041        }
042
043        // 0 for padding with leading 0s
044        public String toString() {
045                return String.format("(%03d) %03d-%04d", area, exch, ext);
046        }
047
048        public static void main(String[] args) {
049                XPhoneNumber a = new XPhoneNumber(609, 258, 4455);
050                XPhoneNumber b = new XPhoneNumber(609, 876, 5309);
051                XPhoneNumber c = new XPhoneNumber(609, 003, 5309);
052                XPhoneNumber d = new XPhoneNumber(215, 876, 5309);
053                XPhoneNumber e = new XPhoneNumber(609, 876, 5309);
054                StdOut.format("a = %s [hashcode=%d]\n", a, a.hashCode ());
055                StdOut.format("b = %s [hashcode=%d]\n", b, b.hashCode ());
056                StdOut.format("c = %s [hashcode=%d]\n", c, c.hashCode ());
057                StdOut.format("d = %s [hashcode=%d]\n", d, d.hashCode ());
058                StdOut.format("e = %s [hashcode=%d]\n", e, e.hashCode ());
059
060                HashSet<XPhoneNumber> set = new HashSet<>();
061                set.add(a);
062                set.add(b);
063                set.add(c);
064                StdOut.println("Added a, b, and c");
065                StdOut.println("contains a:  " + set.contains(a));
066                StdOut.println("contains b:  " + set.contains(b));
067                StdOut.println("contains c:  " + set.contains(c));
068                StdOut.println("contains d:  " + set.contains(d));
069                StdOut.println("contains e:  " + set.contains(e));
070                StdOut.println("b == e:      " + (b == e));
071                StdOut.println("b.equals(e): " + (b.equals(e)));
072        }
073
074
075
076}