001// Exercise 1.2.13 2.1.21 (Solution published at http://algs4.cs.princeton.edu/) 002// Exercise 1.2.13 003package algs12; 004import stdlib.*; 005import java.util.Arrays; 006import java.util.Comparator; 007/* *********************************************************************** 008 * Compilation: javac Transaction.java 009 * Execution: java Transaction 010 * 011 * Data type for commercial transactions. 012 * 013 *************************************************************************/ 014public class Transaction implements Comparable<Transaction> { 015 private final String who; // customer 016 private final Date when; // date 017 private final double amount; // amount 018 019 public Transaction(String who, Date when, double amount) { 020 this.who = who; 021 this.when = when; 022 this.amount = amount; 023 } 024 025 // create new transaction by parsing string of the form: name, 026 // date, real number, separated by whitespace 027 public Transaction(String transaction) { 028 String[] a = transaction.split("\\s+"); 029 who = a[0]; 030 when = new Date(a[1]); 031 amount = Double.parseDouble(a[2]); 032 } 033 034 // accessor methods 035 public String who() { return who; } 036 public Date when() { return when; } 037 public double amount() { return amount; } 038 039 public String toString() { 040 return String.format("%-10s %10s %8.2f", who, when, amount); 041 } 042 043 public int compareTo(Transaction that) { 044 if (this.amount < that.amount) return -1; 045 else if (this.amount > that.amount) return +1; 046 else return 0; 047 } 048 049 // is this Transaction equal to x? 050 public boolean equals(Object x) { 051 if (x == this) return true; 052 if (x == null) return false; 053 if (x.getClass() != this.getClass()) return false; 054 Transaction that = (Transaction) x; 055 return (this.amount == that.amount) && (this.who.equals(that.who)) 056 && (this.when.equals(that.when)); 057 } 058 059 060 061 062 public int hashCode() { 063 int hash = 17; 064 hash = 31*hash + who.hashCode(); 065 hash = 31*hash + when.hashCode(); 066 hash = 31*hash + ((Double) amount).hashCode(); 067 return hash; 068 } 069 070 public static final Comparator<Transaction> WHO_ORDER = new WhoOrder(); 071 public static final Comparator<Transaction> WHEN_ORDER = new WhenOrder(); 072 public static final Comparator<Transaction> HOW_MUCH_ORDER = new HowMuchOrder(); 073 // ascending order of account number 074 private static class WhoOrder implements Comparator<Transaction> { 075 public int compare(Transaction v, Transaction w) { 076 return v.who.compareTo(w.who); 077 } 078 } 079 080 // ascending order of time 081 private static class WhenOrder implements Comparator<Transaction> { 082 public int compare(Transaction v, Transaction w) { 083 return v.when.compareTo(w.when); 084 } 085 } 086 087 // ascending order of amount 088 private static class HowMuchOrder implements Comparator<Transaction> { 089 public int compare(Transaction v, Transaction w) { 090 if (v.amount < w.amount) return -1; 091 else if (v.amount > w.amount) return +1; 092 else return 0; 093 } 094 } 095 096 097 // test client 098 public static void main(String[] args) { 099 Transaction[] a = new Transaction[4]; 100 a[0] = new Transaction("Turing 6/17/1990 644.08"); 101 a[1] = new Transaction("Tarjan 3/26/2002 4121.85"); 102 a[2] = new Transaction("Knuth 6/14/1999 288.34"); 103 a[3] = new Transaction("Dijkstra 8/22/2007 2678.40"); 104 105 StdOut.println("Unsorted"); 106 for (Transaction element : a) 107 StdOut.println(element); 108 StdOut.println(); 109 110 StdOut.println("Sort by date"); 111 Arrays.sort(a, new WhenOrder()); 112 for (Transaction element : a) 113 StdOut.println(element); 114 StdOut.println(); 115 116 StdOut.println("Sort by customer"); 117 Arrays.sort(a, new WhoOrder()); 118 for (Transaction element : a) 119 StdOut.println(element); 120 StdOut.println(); 121 122 StdOut.println("Sort by amount"); 123 Arrays.sort(a, new HowMuchOrder()); 124 for (Transaction element : a) 125 StdOut.println(element); 126 StdOut.println(); 127 } 128 129} 130