用我们对Date的实现作为模板实现Transaction类型。
class Transaction implements Comparable<Transaction> { private final String name; private final SmartDate date; private final double amount; public Transaction(String who, SmartDate when, double amount) { this.name = who; this.date = when; this.amount = amount; } public Transaction(String transaction) { String[] s = transaction.split("\s+"); if (s.length != 3) throw new IllegalArgumentException("Argument illegal " + transaction); this.name = s[0]; this.date = new SmartDate(s[1]); this.amount = Double.parseDouble(s[2]); } public String who() { return this.name; } public SmartDate when() { return this.date; } public double amount() { return this.amount; } public String toString() { return name + " " + date + " " + amount; } public boolean equals(Object that) { if (this == that) return true; if (that == null) return false; if (this.getClass() != that.getClass()) return false; Transaction t = (Transaction) that; if (!this.name.equals(t.name)) return false; if (!this.date.equals(t.date)) return false; if (this.amount != t.amount) return false; return true; } public int hashCode() { int hash = 1; hash = hash * 31 + this.name.hashCode(); hash = hash * 31 + this.date.hashCode(); hash = hash * 31 + ((Double) this.amount).hashCode(); return hash; } @Override public int compareTo(Transaction o) { if (this.amount > o.amount) return 1; else if (this.amount < o.amount) return -1; else return 0; } }
/** * Description : * Author : mn@furzoom.com * Date : Sep 27, 2016 11:09:47 AM * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved. */ package com.furzoom.lab.algs.ch102; import java.util.Arrays; /** * ClassName : E10213 <br> * Function : TODO ADD FUNCTION. <br> * date : Sep 27, 2016 11:09:47 AM <br> * * @version */ public class E10213 { public static void main(String[] args) { Transaction[] t = new Transaction[4]; t[0] = new Transaction("Turing 6/17/1990 644.08"); t[1] = new Transaction("Tarjan 3/26/2002 4121.85"); t[2] = new Transaction("Knuth 6/14/1999 288.34"); t[3] = new Transaction("Dijkstra 8/22/2007 2678.40"); System.out.println("Unsorted:"); for (int i = 0; i < t.length; i++) { System.out.println(t[i]); } System.out.println(); System.out.println("Sorted:"); Arrays.sort(t); for (int i = 0; i < t.length; i++) { System.out.println(t[i]); } System.out.println(); } }
完善了SmartDate类:
/** * Description : * Author : mn@furzoom.com * Date : Sep 27, 2016 9:45:00 AM * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved. */ package com.furzoom.lab.algs.ch102; class SmartDate { private final int month; private final int day; private final int year; private static final int[] months = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; private static final int[] days = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; private static final int[] daysLeap = {0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; private static final String[] weekdays = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; public SmartDate(int m, int d, int y) { if (!validate(m, d, y)) throw new IllegalArgumentException("Argument illegal " + m + "/" + d + "/" + y); this.month = m; this.day = d; this.year = y; } public SmartDate(String date) { String[] s = date.split("\/"); if (s.length != 3) throw new IllegalArgumentException("Argument illegal " + date); int m = Integer.parseInt(s[0]); int d = Integer.parseInt(s[1]); int y = Integer.parseInt(s[2]); if (!validate(m, d, y)) throw new IllegalArgumentException("Argument illegal " + m + "/" + d + "/" + y); this.month = m; this.day = d; this.year = y; } public int month() { return month; } public int day() { return day; } public int year() { return year; } public String dayOfTheWeek() { // based on 1/1/2000 int totalDays; if (isLeapYear()) { totalDays = daysLeap[month] + day; } else { totalDays = days[month] + day; } for (int i = 2000; i < year; i++) { if (isLeapYear(i)) totalDays += 366; else totalDays += 365; } // 1/1/2000 is Saturday return weekdays[((totalDays - 1) % 7 + 6) % 7]; } public String toString() { return month + "/" + day + "/" + year; } public boolean equals(Object x) { if (this == x) return true; if (x == null) return false; if (this.getClass() != x.getClass()) return false; SmartDate that = (SmartDate)x; if (this.day != that.day) return false; if (this.month != that.month) return false; if (this.year != that.year) return false; return true; } @Override public int hashCode() { int hash = 17; hash = hash * 31 + month; hash = hash * 31 + day; hash = hash * 31 + year; return hash; } private boolean validate(int m, int d, int y) { if (y == 0 || y < -1000 || y > 10000) return false; if (m < 1 || m > 12) return false; if (d < 1 || d > 31) return false; if (d > months[m]) return false; if (!isLeapYear() && d > 28) return false; return true; } private boolean isLeapYear(int y) { if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) { return true; } else { return false; } } private boolean isLeapYear() { return isLeapYear(year); } }